import React, { useCallback, useEffect, useRef, useState } from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import camelCase from 'lodash/camelCase'; import { useTranslation } from 'react-i18next'; import upperFirst from 'lodash/upperFirst'; import { Draggable, Droppable } from 'react-beautiful-dnd'; import { Button, Icon } from 'semantic-ui-react'; import { usePopup } from '../../lib/popup'; import DroppableTypes from '../../constants/DroppableTypes'; import CardContainer from '../../containers/CardContainer'; import CardAdd from './CardAdd'; import NameEdit from './NameEdit'; import ActionsStep from './ActionsStep'; import { ReactComponent as PlusMathIcon } from '../../assets/images/plus-math-icon.svg'; import styles from './List.module.scss'; import globalStyles from '../../styles.module.scss'; const List = React.memo( ({ id, index, name, color, isPersisted, cardIds, canEdit, onUpdate, onDelete, onSort, onCardCreate, }) => { const [t] = useTranslation(); const [isAddCardOpened, setIsAddCardOpened] = useState(false); const nameEdit = useRef(null); const cardsWrapper = useRef(null); const handleHeaderClick = useCallback(() => { if (isPersisted && canEdit) { nameEdit.current.open(); } }, [isPersisted, canEdit]); const handleNameUpdate = useCallback( (newName) => { onUpdate({ name: newName, }); }, [onUpdate], ); const handleColorEdit = useCallback( (newColor) => { onUpdate({ color: newColor, }); }, [onUpdate], ); const handleAddCardClick = useCallback(() => { setIsAddCardOpened(true); }, []); const handleAddCardClose = useCallback(() => { setIsAddCardOpened(false); }, []); const handleNameEdit = useCallback(() => { nameEdit.current.open(); }, []); const handleCardAdd = useCallback(() => { setIsAddCardOpened(true); }, []); useEffect(() => { if (isAddCardOpened) { cardsWrapper.current.scrollTop = cardsWrapper.current.scrollHeight; } }, [cardIds, isAddCardOpened]); const ActionsPopup = usePopup(ActionsStep); const cardsNode = ( {({ innerRef, droppableProps, placeholder }) => ( // eslint-disable-next-line react/jsx-props-no-spreading
{cardIds.map((cardId, cardIndex) => ( ))} {placeholder} {canEdit && ( )}
)}
); return ( {({ innerRef, draggableProps, dragHandleProps }) => (
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */}
{color && ( )} {name}
{isPersisted && canEdit && ( )}
{cardsNode}
{!isAddCardOpened && canEdit && ( )}
)}
); }, ); List.propTypes = { id: PropTypes.string.isRequired, index: PropTypes.number.isRequired, name: PropTypes.string.isRequired, color: PropTypes.string, isPersisted: PropTypes.bool.isRequired, cardIds: PropTypes.array.isRequired, // eslint-disable-line react/forbid-prop-types canEdit: PropTypes.bool.isRequired, onUpdate: PropTypes.func.isRequired, onSort: PropTypes.func.isRequired, onDelete: PropTypes.func.isRequired, onCardCreate: PropTypes.func.isRequired, }; List.defaultProps = { color: undefined, }; export default List;