2019-08-31 04:07:25 +05:00
|
|
|
import React, { useCallback, useRef } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { Button, Checkbox, Icon } from 'semantic-ui-react';
|
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
import NameEdit from './NameEdit';
|
2019-08-31 04:07:25 +05:00
|
|
|
import ActionsPopup from './ActionsPopup';
|
|
|
|
|
2020-05-29 19:31:19 +05:00
|
|
|
import styles from './Item.module.scss';
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2020-02-03 18:42:31 +05:00
|
|
|
const Item = React.memo(({ name, isCompleted, isPersisted, onUpdate, onDelete }) => {
|
2020-08-04 01:32:46 +05:00
|
|
|
const nameEdit = useRef(null);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
const handleClick = useCallback(() => {
|
|
|
|
if (isPersisted) {
|
2020-08-04 01:32:46 +05:00
|
|
|
nameEdit.current.open();
|
2019-08-31 04:07:25 +05:00
|
|
|
}
|
|
|
|
}, [isPersisted]);
|
|
|
|
|
|
|
|
const handleNameUpdate = useCallback(
|
2020-03-25 00:15:47 +05:00
|
|
|
(newName) => {
|
2019-08-31 04:07:25 +05:00
|
|
|
onUpdate({
|
|
|
|
name: newName,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[onUpdate],
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleToggleChange = useCallback(() => {
|
|
|
|
onUpdate({
|
|
|
|
isCompleted: !isCompleted,
|
|
|
|
});
|
|
|
|
}, [isCompleted, onUpdate]);
|
|
|
|
|
|
|
|
const handleNameEdit = useCallback(() => {
|
2020-08-04 01:32:46 +05:00
|
|
|
nameEdit.current.open();
|
2019-08-31 04:07:25 +05:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={styles.wrapper}>
|
|
|
|
<span className={styles.checkboxWrapper}>
|
|
|
|
<Checkbox
|
|
|
|
checked={isCompleted}
|
|
|
|
disabled={!isPersisted}
|
|
|
|
className={styles.checkbox}
|
|
|
|
onChange={handleToggleChange}
|
|
|
|
/>
|
|
|
|
</span>
|
2020-08-04 01:32:46 +05:00
|
|
|
<NameEdit ref={nameEdit} defaultValue={name} onUpdate={handleNameUpdate}>
|
2019-08-31 04:07:25 +05:00
|
|
|
<div className={styles.content}>
|
|
|
|
{/* eslint-disable jsx-a11y/click-events-have-key-events,
|
|
|
|
jsx-a11y/no-static-element-interactions */}
|
|
|
|
<span className={styles.text} onClick={handleClick}>
|
|
|
|
{/* eslint-enable jsx-a11y/click-events-have-key-events,
|
|
|
|
jsx-a11y/no-static-element-interactions */}
|
|
|
|
<span className={classNames(styles.task, isCompleted && styles.taskCompleted)}>
|
|
|
|
{name}
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
{isPersisted && (
|
|
|
|
<ActionsPopup onNameEdit={handleNameEdit} onDelete={onDelete}>
|
|
|
|
<Button className={classNames(styles.button, styles.target)}>
|
|
|
|
<Icon fitted name="pencil" size="small" />
|
|
|
|
</Button>
|
|
|
|
</ActionsPopup>
|
|
|
|
)}
|
|
|
|
</div>
|
2020-08-04 01:32:46 +05:00
|
|
|
</NameEdit>
|
2019-08-31 04:07:25 +05:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Item.propTypes = {
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
isCompleted: PropTypes.bool.isRequired,
|
|
|
|
isPersisted: PropTypes.bool.isRequired,
|
|
|
|
onUpdate: PropTypes.func.isRequired,
|
|
|
|
onDelete: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Item;
|