1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 05:09:43 +02:00
planka/client/src/components/List/List.jsx

156 lines
5.2 KiB
React
Raw Normal View History

import React, { useCallback, useEffect, useRef, useState } from 'react';
2019-08-31 04:07:25 +05:00
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { useTranslation } from 'react-i18next';
import { Draggable, Droppable } from 'react-beautiful-dnd';
import { Button, Icon } from 'semantic-ui-react';
import DroppableTypes from '../../constants/DroppableTypes';
import CardContainer from '../../containers/CardContainer';
import NameEdit from './NameEdit';
import CardAdd from './CardAdd';
2019-08-31 04:07:25 +05:00
import ActionsPopup from './ActionsPopup';
import { ReactComponent as PlusMathIcon } from '../../assets/images/plus-math-icon.svg';
import styles from './List.module.scss';
2019-08-31 04:07:25 +05:00
const List = React.memo(
({ id, index, name, isPersisted, cardIds, onUpdate, onDelete, onCardCreate }) => {
2019-08-31 04:07:25 +05:00
const [t] = useTranslation();
const [isAddCardOpened, setIsAddCardOpened] = useState(false);
2019-08-31 04:07:25 +05:00
const nameEdit = useRef(null);
const listWrapper = useRef(null);
2019-08-31 04:07:25 +05:00
const handleHeaderClick = useCallback(() => {
if (isPersisted) {
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 handleAddCardClick = useCallback(() => {
setIsAddCardOpened(true);
}, []);
const handleAddCardClose = useCallback(() => {
setIsAddCardOpened(false);
}, []);
2019-08-31 04:07:25 +05:00
const handleNameEdit = useCallback(() => {
nameEdit.current.open();
2019-08-31 04:07:25 +05:00
}, []);
const handleCardAdd = useCallback(() => {
setIsAddCardOpened(true);
2019-08-31 04:07:25 +05:00
}, []);
useEffect(() => {
if (isAddCardOpened) {
listWrapper.current.scrollTop = listWrapper.current.scrollHeight;
}
}, [cardIds, isAddCardOpened]);
2019-08-31 04:07:25 +05:00
const cardsNode = (
<Droppable
droppableId={`list:${id}`}
type={DroppableTypes.CARD}
isDropDisabled={!isPersisted}
>
{({ innerRef, droppableProps, placeholder }) => (
// eslint-disable-next-line react/jsx-props-no-spreading
<div {...droppableProps} ref={innerRef}>
<div className={styles.cards}>
{cardIds.map((cardId, cardIndex) => (
<CardContainer key={cardId} id={cardId} index={cardIndex} />
))}
{placeholder}
<CardAdd
isOpened={isAddCardOpened}
onCreate={onCardCreate}
onClose={handleAddCardClose}
/>
2019-08-31 04:07:25 +05:00
</div>
</div>
)}
</Droppable>
);
return (
<Draggable draggableId={`list:${id}`} index={index} isDragDisabled={!isPersisted}>
{({ innerRef, draggableProps, dragHandleProps }) => (
// eslint-disable-next-line react/jsx-props-no-spreading
<div {...draggableProps} data-drag-scroller ref={innerRef} className={styles.wrapper}>
{/* eslint-disable jsx-a11y/click-events-have-key-events,
jsx-a11y/no-static-element-interactions,
react/jsx-props-no-spreading */}
<div {...dragHandleProps} className={styles.header} onClick={handleHeaderClick}>
{/* eslint-enable jsx-a11y/click-events-have-key-events,
jsx-a11y/no-static-element-interactions,
react/jsx-props-no-spreading */}
<NameEdit ref={nameEdit} defaultValue={name} onUpdate={handleNameUpdate}>
2019-08-31 04:07:25 +05:00
<div className={styles.headerName}>{name}</div>
</NameEdit>
2019-08-31 04:07:25 +05:00
{isPersisted && (
<ActionsPopup
onNameEdit={handleNameEdit}
onCardAdd={handleCardAdd}
onDelete={onDelete}
>
<Button className={classNames(styles.headerButton, styles.target)}>
<Icon fitted name="pencil" size="small" />
</Button>
</ActionsPopup>
)}
</div>
<div
ref={listWrapper}
className={classNames(styles.listWrapper, isAddCardOpened && styles.listWrapperFull)}
>
<div className={styles.list}>{cardsNode}</div>
</div>
{!isAddCardOpened && (
<button
type="button"
disabled={!isPersisted}
className={styles.addCardButton}
onClick={handleAddCardClick}
>
<PlusMathIcon className={styles.addCardButtonIcon} />
<span className={styles.addCardButtonText}>
{cardIds.length > 0 ? t('action.addAnotherCard') : t('action.addCard')}
</span>
</button>
)}
2019-08-31 04:07:25 +05:00
</div>
)}
</Draggable>
);
},
);
List.propTypes = {
id: PropTypes.string.isRequired,
2019-08-31 04:07:25 +05:00
index: PropTypes.number.isRequired,
name: PropTypes.string.isRequired,
isPersisted: PropTypes.bool.isRequired,
cardIds: PropTypes.array.isRequired, // eslint-disable-line react/forbid-prop-types
onUpdate: PropTypes.func.isRequired,
onDelete: PropTypes.func,
2019-08-31 04:07:25 +05:00
onCardCreate: PropTypes.func.isRequired,
};
List.defaultProps = {
onDelete: undefined,
};
2019-08-31 04:07:25 +05:00
export default List;