taskcafe/frontend/src/shared/components/List/index.tsx

126 lines
3.4 KiB
TypeScript
Raw Normal View History

2020-04-10 04:40:22 +02:00
import React, { useState, useRef } from 'react';
import useOnEscapeKeyDown from 'shared/hooks/onEscapeKeyDown';
import { Plus, Ellipsis } from 'shared/icons';
2020-04-10 04:40:22 +02:00
import {
Container,
Wrapper,
Header,
HeaderName,
HeaderEditTarget,
AddCardContainer,
AddCardButton,
AddCardButtonText,
ListCards,
ListExtraMenuButtonWrapper,
2020-04-10 04:40:22 +02:00
} from './Styles';
type Props = {
children: React.ReactNode;
id: string;
name: string;
onSaveName: (name: string) => void;
isComposerOpen: boolean;
onOpenComposer: (id: string) => void;
wrapperProps?: any;
headerProps?: any;
2021-05-01 05:55:37 +02:00
isPublic: boolean;
2020-04-10 04:40:22 +02:00
index?: number;
2020-05-27 02:53:31 +02:00
onExtraMenuOpen: (taskGroupID: string, $targetRef: React.RefObject<HTMLElement>) => void;
2020-04-10 04:40:22 +02:00
};
const List = React.forwardRef(
(
{
id,
name,
onSaveName,
isComposerOpen,
onOpenComposer,
children,
2021-05-01 05:55:37 +02:00
isPublic,
wrapperProps,
headerProps,
onExtraMenuOpen,
}: Props,
2020-04-10 04:40:22 +02:00
$wrapperRef: any,
) => {
const [listName, setListName] = useState(name);
const [isEditingTitle, setEditingTitle] = useState(false);
const $listNameRef = useRef<HTMLTextAreaElement>(null);
const $extraActionsRef = useRef<HTMLDivElement>(null);
2020-04-10 04:40:22 +02:00
const onClick = () => {
setEditingTitle(true);
if ($listNameRef && $listNameRef.current) {
2020-04-10 04:40:22 +02:00
$listNameRef.current.select();
}
};
const onBlur = () => {
setEditingTitle(false);
2021-02-07 21:54:36 +01:00
onSaveName(listName);
2020-04-10 04:40:22 +02:00
};
const onEscape = () => {
if ($listNameRef && $listNameRef.current) {
$listNameRef.current.blur();
}
2020-04-10 04:40:22 +02:00
};
const onChange = (event: React.FormEvent<HTMLTextAreaElement>): void => {
setListName(event.currentTarget.value);
};
const onKeyDown = (e: React.KeyboardEvent) => {
if (e.key === 'Enter') {
e.preventDefault();
if ($listNameRef && $listNameRef.current) {
$listNameRef.current.blur();
}
}
};
const handleExtraMenuOpen = () => {
if ($extraActionsRef && $extraActionsRef.current) {
2020-05-27 02:53:31 +02:00
onExtraMenuOpen(id, $extraActionsRef);
2020-04-10 04:40:22 +02:00
}
};
useOnEscapeKeyDown(isEditingTitle, onEscape);
return (
<Container ref={$wrapperRef} {...wrapperProps}>
<Wrapper>
<Header {...headerProps} isEditing={isEditingTitle}>
2021-05-01 05:55:37 +02:00
{!isPublic && <HeaderEditTarget onClick={onClick} isHidden={isEditingTitle} />}
2020-04-10 04:40:22 +02:00
<HeaderName
ref={$listNameRef}
2021-05-01 05:55:37 +02:00
disabled={isPublic}
2020-04-10 04:40:22 +02:00
onBlur={onBlur}
onChange={onChange}
onKeyDown={onKeyDown}
spellCheck={false}
value={listName}
/>
2021-05-01 05:55:37 +02:00
{!isPublic && (
<ListExtraMenuButtonWrapper ref={$extraActionsRef} onClick={handleExtraMenuOpen}>
<Ellipsis vertical={false} size={16} color="#c2c6dc" />
2021-05-01 05:55:37 +02:00
</ListExtraMenuButtonWrapper>
)}
2020-04-10 04:40:22 +02:00
</Header>
{children && children}
2021-05-01 05:55:37 +02:00
{!isPublic && (
<AddCardContainer hidden={isComposerOpen}>
<AddCardButton onClick={() => onOpenComposer(id)}>
<Plus width={12} height={12} />
<AddCardButtonText>Add another card</AddCardButtonText>
</AddCardButton>
</AddCardContainer>
)}
2020-04-10 04:40:22 +02:00
</Wrapper>
</Container>
);
},
);
List.displayName = 'List';
export default List;
export { ListCards };