Files
planka/client/src/components/LabelsStep/Item.jsx

82 lines
2.6 KiB
React
Raw Normal View History

import upperFirst from 'lodash/upperFirst';
import camelCase from 'lodash/camelCase';
2019-08-31 04:07:25 +05:00
import React, { useCallback } from 'react';
2023-01-09 12:17:06 +01:00
import ReactDOM from 'react-dom';
2019-08-31 04:07:25 +05:00
import PropTypes from 'prop-types';
import classNames from 'classnames';
2023-01-09 12:17:06 +01:00
import { Draggable } from 'react-beautiful-dnd';
2019-08-31 04:07:25 +05:00
import { Button } from 'semantic-ui-react';
import styles from './Item.module.scss';
import globalStyles from '../../styles.module.scss';
2019-08-31 04:07:25 +05:00
2022-11-10 15:28:43 +01:00
const Item = React.memo(
2023-01-09 12:17:06 +01:00
({ id, index, name, color, isPersisted, isActive, canEdit, onSelect, onDeselect, onEdit }) => {
2022-11-10 15:28:43 +01:00
const handleToggleClick = useCallback(() => {
2023-01-09 12:17:06 +01:00
if (isPersisted) {
if (isActive) {
onDeselect();
} else {
onSelect();
}
2022-11-10 15:28:43 +01:00
}
2023-01-09 12:17:06 +01:00
}, [isPersisted, isActive, onSelect, onDeselect]);
2019-08-31 04:07:25 +05:00
2022-11-10 15:28:43 +01:00
return (
2023-01-09 12:17:06 +01:00
<Draggable draggableId={id} index={index} isDragDisabled={!isPersisted || !canEdit}>
{({ innerRef, draggableProps, dragHandleProps }, { isDragging }) => {
const contentNode = (
// eslint-disable-next-line react/jsx-props-no-spreading
<div {...draggableProps} ref={innerRef} className={styles.wrapper}>
2023-01-09 12:33:49 +01:00
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events,
jsx-a11y/no-static-element-interactions */}
2023-01-09 12:17:06 +01:00
<span
{...dragHandleProps} // eslint-disable-line react/jsx-props-no-spreading
className={classNames(
styles.name,
isActive && styles.nameActive,
globalStyles[`background${upperFirst(camelCase(color))}`],
)}
onClick={handleToggleClick}
>
{name}
</span>
{canEdit && (
<Button
icon="pencil"
size="small"
floated="right"
disabled={!isPersisted}
className={styles.editButton}
onClick={onEdit}
/>
)}
</div>
);
return isDragging ? ReactDOM.createPortal(contentNode, document.body) : contentNode;
}}
</Draggable>
2022-11-10 15:28:43 +01:00
);
},
);
2019-08-31 04:07:25 +05:00
Item.propTypes = {
2023-01-09 12:17:06 +01:00
id: PropTypes.string.isRequired,
index: PropTypes.number.isRequired,
2019-10-09 18:48:19 +05:00
name: PropTypes.string,
2019-08-31 04:07:25 +05:00
color: PropTypes.string.isRequired,
isPersisted: PropTypes.bool.isRequired,
isActive: PropTypes.bool.isRequired,
2022-11-10 15:28:43 +01:00
canEdit: PropTypes.bool.isRequired,
2019-08-31 04:07:25 +05:00
onSelect: PropTypes.func.isRequired,
onDeselect: PropTypes.func.isRequired,
onEdit: PropTypes.func.isRequired,
};
2019-10-09 18:48:19 +05:00
Item.defaultProps = {
name: undefined,
};
2019-08-31 04:07:25 +05:00
export default Item;