mirror of
https://github.com/plankanban/planka.git
synced 2025-07-24 15:49:46 +02:00
89 lines
2.2 KiB
JavaScript
Executable file
89 lines
2.2 KiB
JavaScript
Executable file
import React, { useCallback, useEffect, useRef } from 'react';
|
|
import PropTypes from 'prop-types';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { Button, Form, Input } from 'semantic-ui-react';
|
|
import { useDidUpdate, useToggle } from '../../lib/hooks';
|
|
|
|
import { useClosableForm, useForm } from '../../hooks';
|
|
|
|
import styles from './AddList.module.css';
|
|
|
|
const DEFAULT_DATA = {
|
|
name: '',
|
|
};
|
|
|
|
const AddList = React.memo(({ onCreate, onClose }) => {
|
|
const [t] = useTranslation();
|
|
const [data, handleFieldChange, setData] = useForm(DEFAULT_DATA);
|
|
const [selectNameFieldState, selectNameField] = useToggle();
|
|
|
|
const nameField = useRef(null);
|
|
|
|
const handleFieldKeyDown = useCallback(
|
|
(event) => {
|
|
if (event.key === 'Escape') {
|
|
onClose();
|
|
}
|
|
},
|
|
[onClose],
|
|
);
|
|
|
|
const [handleFieldBlur, handleControlMouseOver, handleControlMouseOut] = useClosableForm(onClose);
|
|
|
|
const handleSubmit = useCallback(() => {
|
|
const cleanData = {
|
|
...data,
|
|
name: data.name.trim(),
|
|
};
|
|
|
|
if (!cleanData.name) {
|
|
nameField.current.select();
|
|
return;
|
|
}
|
|
|
|
onCreate(cleanData);
|
|
|
|
setData(DEFAULT_DATA);
|
|
selectNameField();
|
|
}, [onCreate, data, setData, selectNameField]);
|
|
|
|
useEffect(() => {
|
|
nameField.current.select();
|
|
}, []);
|
|
|
|
useDidUpdate(() => {
|
|
nameField.current.select();
|
|
}, [selectNameFieldState]);
|
|
|
|
return (
|
|
<Form className={styles.wrapper} onSubmit={handleSubmit}>
|
|
<Input
|
|
ref={nameField}
|
|
name="name"
|
|
value={data.name}
|
|
placeholder={t('common.enterListTitle')}
|
|
className={styles.field}
|
|
onKeyDown={handleFieldKeyDown}
|
|
onChange={handleFieldChange}
|
|
onBlur={handleFieldBlur}
|
|
/>
|
|
<div className={styles.controls}>
|
|
{/* eslint-disable-next-line jsx-a11y/mouse-events-have-key-events */}
|
|
<Button
|
|
positive
|
|
content={t('action.addList')}
|
|
className={styles.submitButton}
|
|
onMouseOver={handleControlMouseOver}
|
|
onMouseOut={handleControlMouseOut}
|
|
/>
|
|
</div>
|
|
</Form>
|
|
);
|
|
});
|
|
|
|
AddList.propTypes = {
|
|
onCreate: PropTypes.func.isRequired,
|
|
onClose: PropTypes.func.isRequired,
|
|
};
|
|
|
|
export default AddList;
|