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

205 lines
6.4 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 camelCase from 'lodash/camelCase';
2019-08-31 04:07:25 +05:00
import { useTranslation } from 'react-i18next';
import upperFirst from 'lodash/upperFirst';
2019-08-31 04:07:25 +05:00
import { Draggable, Droppable } from 'react-beautiful-dnd';
import { Button, Icon } from 'semantic-ui-react';
import { usePopup } from '../../lib/popup';
2019-08-31 04:07:25 +05:00
import DroppableTypes from '../../constants/DroppableTypes';
import CardContainer from '../../containers/CardContainer';
import CardAdd from './CardAdd';
import NameEdit from './NameEdit';
import ActionsStep from './ActionsStep';
2019-08-31 04:07:25 +05:00
import { ReactComponent as PlusMathIcon } from '../../assets/images/plus-math-icon.svg';
import styles from './List.module.scss';
import globalStyles from '../../styles.module.scss';
2019-08-31 04:07:25 +05:00
const List = React.memo(
({
id,
index,
name,
color,
isPersisted,
cardIds,
canEdit,
onUpdate,
onDelete,
onSort,
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 cardsWrapper = useRef(null);
2019-08-31 04:07:25 +05:00
const handleHeaderClick = useCallback(() => {
if (isPersisted && canEdit) {
nameEdit.current.open();
2019-08-31 04:07:25 +05:00
}
}, [isPersisted, canEdit]);
2019-08-31 04:07:25 +05:00
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 handleColorEdit = useCallback(
(newColor) => {
onUpdate({
color: newColor,
});
},
[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) {
cardsWrapper.current.scrollTop = cardsWrapper.current.scrollHeight;
}
}, [cardIds, isAddCardOpened]);
const ActionsPopup = usePopup(ActionsStep);
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}
{canEdit && (
<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 || !canEdit}>
2019-08-31 04:07:25 +05:00
{({ innerRef, draggableProps, dragHandleProps }) => (
<div
{...draggableProps} // eslint-disable-line react/jsx-props-no-spreading
data-drag-scroller
ref={innerRef}
className={styles.innerWrapper}
>
<div className={styles.outerWrapper}>
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 */}
<div
2023-01-09 12:33:49 +01:00
{...dragHandleProps} // eslint-disable-line react/jsx-props-no-spreading
className={classNames(styles.header, canEdit && styles.headerEditable)}
onClick={handleHeaderClick}
>
<NameEdit ref={nameEdit} defaultValue={name} onUpdate={handleNameUpdate}>
<div className={styles.headerName}>
{color && (
<Icon
name="circle"
className={classNames(
styles.headerNameColor,
globalStyles[`color${upperFirst(camelCase(color))}`],
)}
/>
)}
{name}
</div>
</NameEdit>
{isPersisted && canEdit && (
<ActionsPopup
onNameEdit={handleNameEdit}
onCardAdd={handleCardAdd}
onDelete={onDelete}
onSort={onSort}
color={color}
onColorEdit={handleColorEdit}
>
<Button className={classNames(styles.headerButton, styles.target)}>
<Icon fitted name="pencil" size="small" />
</Button>
</ActionsPopup>
)}
</div>
<div ref={cardsWrapper} className={styles.cardsInnerWrapper}>
<div className={styles.cardsOuterWrapper}>{cardsNode}</div>
</div>
{!isAddCardOpened && canEdit && (
<button
type="button"
disabled={!isPersisted}
className={styles.addCardButton}
onClick={handleAddCardClick}
2019-08-31 04:07:25 +05:00
>
<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>
</div>
)}
</Draggable>
);
},
);
List.propTypes = {
id: PropTypes.string.isRequired,
2019-08-31 04:07:25 +05:00
index: PropTypes.number.isRequired,
name: PropTypes.string.isRequired,
color: PropTypes.string,
2019-08-31 04:07:25 +05:00
isPersisted: PropTypes.bool.isRequired,
cardIds: PropTypes.array.isRequired, // eslint-disable-line react/forbid-prop-types
canEdit: PropTypes.bool.isRequired,
2019-08-31 04:07:25 +05:00
onUpdate: PropTypes.func.isRequired,
onSort: PropTypes.func.isRequired,
onDelete: PropTypes.func.isRequired,
2019-08-31 04:07:25 +05:00
onCardCreate: PropTypes.func.isRequired,
};
List.defaultProps = {
color: undefined,
};
2019-08-31 04:07:25 +05:00
export default List;