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

117 lines
2.8 KiB
React
Raw Normal View History

import React, { useCallback, useEffect, useRef } from 'react';
2019-08-31 04:07:25 +05:00
import PropTypes from 'prop-types';
import classNames from 'classnames';
2019-08-31 04:07:25 +05:00
import { useTranslation } from 'react-i18next';
import TextareaAutosize from 'react-textarea-autosize';
import { Button, Form, TextArea } from 'semantic-ui-react';
import { useDidUpdate, useToggle } from '../../lib/hooks';
2019-08-31 04:07:25 +05:00
import { useClosableForm, useForm } from '../../hooks';
2019-08-31 04:07:25 +05:00
import styles from './CardAdd.module.scss';
2019-08-31 04:07:25 +05:00
const DEFAULT_DATA = {
name: '',
};
const CardAdd = React.memo(({ isOpened, onCreate, onClose }) => {
2019-08-31 04:07:25 +05:00
const [t] = useTranslation();
const [data, handleFieldChange, setData] = useForm(DEFAULT_DATA);
const [selectNameFieldState, selectNameField] = useToggle();
const nameField = useRef(null);
const submit = useCallback(() => {
2019-08-31 04:07:25 +05:00
const cleanData = {
...data,
name: data.name.trim(),
};
if (!cleanData.name) {
nameField.current.ref.current.select();
return;
}
onCreate(cleanData);
setData(DEFAULT_DATA);
selectNameField();
}, [onCreate, data, setData, selectNameField]);
const handleFieldKeyDown = useCallback(
2020-03-25 00:15:47 +05:00
(event) => {
2019-08-31 04:07:25 +05:00
switch (event.key) {
case 'Enter':
event.preventDefault();
submit();
break;
case 'Escape':
onClose();
2019-08-31 04:07:25 +05:00
break;
default:
}
},
[onClose, submit],
2019-08-31 04:07:25 +05:00
);
const [handleFieldBlur, handleControlMouseOver, handleControlMouseOut] = useClosableForm(onClose);
2019-08-31 04:07:25 +05:00
const handleSubmit = useCallback(() => {
submit();
}, [submit]);
useEffect(() => {
if (isOpened) {
nameField.current.ref.current.select();
}
}, [isOpened]);
useDidUpdate(() => {
nameField.current.ref.current.select();
}, [selectNameFieldState]);
return (
<Form
className={classNames(styles.wrapper, !isOpened && styles.wrapperClosed)}
onSubmit={handleSubmit}
>
2019-08-31 04:07:25 +05:00
<div className={styles.fieldWrapper}>
<TextArea
ref={nameField}
as={TextareaAutosize}
name="name"
value={data.name}
placeholder={t('common.enterCardTitle')}
minRows={3}
spellCheck={false}
className={styles.field}
onKeyDown={handleFieldKeyDown}
onChange={handleFieldChange}
onBlur={handleFieldBlur}
/>
</div>
<div className={styles.controls}>
{/* eslint-disable-next-line jsx-a11y/mouse-events-have-key-events */}
<Button
positive
content={t('action.addCard')}
className={styles.submitButton}
onMouseOver={handleControlMouseOver}
onMouseOut={handleControlMouseOut}
/>
</div>
</Form>
);
});
CardAdd.propTypes = {
isOpened: PropTypes.bool.isRequired,
2019-08-31 04:07:25 +05:00
onCreate: PropTypes.func.isRequired,
onClose: PropTypes.func.isRequired,
2019-08-31 04:07:25 +05:00
};
export default CardAdd;