mirror of
https://github.com/plankanban/planka.git
synced 2025-07-19 13:19:44 +02:00
ref: Refactoring
This commit is contained in:
parent
2067a1ac58
commit
4749ea1f34
1 changed files with 30 additions and 21 deletions
|
@ -13,6 +13,8 @@ const DEFAULT_DATA = {
|
|||
name: '',
|
||||
};
|
||||
|
||||
const MULTIPLE_REGEX = /\s*\r?\n\s*/;
|
||||
|
||||
const Add = React.forwardRef(({ children, onCreate }, ref) => {
|
||||
const [t] = useTranslation();
|
||||
const [isOpened, setIsOpened] = useState(false);
|
||||
|
@ -29,7 +31,8 @@ const Add = React.forwardRef(({ children, onCreate }, ref) => {
|
|||
setIsOpened(false);
|
||||
}, []);
|
||||
|
||||
const submit = useCallback(() => {
|
||||
const submit = useCallback(
|
||||
(isMultiple = false) => {
|
||||
const cleanData = {
|
||||
...data,
|
||||
name: data.name.trim(),
|
||||
|
@ -40,11 +43,22 @@ const Add = React.forwardRef(({ children, onCreate }, ref) => {
|
|||
return;
|
||||
}
|
||||
|
||||
if (isMultiple) {
|
||||
cleanData.name.split(MULTIPLE_REGEX).forEach((name) => {
|
||||
onCreate({
|
||||
...cleanData,
|
||||
name,
|
||||
});
|
||||
});
|
||||
} else {
|
||||
onCreate(cleanData);
|
||||
}
|
||||
|
||||
setData(DEFAULT_DATA);
|
||||
focusNameField();
|
||||
}, [onCreate, data, setData, focusNameField]);
|
||||
},
|
||||
[onCreate, data, setData, focusNameField],
|
||||
);
|
||||
|
||||
useImperativeHandle(
|
||||
ref,
|
||||
|
@ -61,17 +75,12 @@ const Add = React.forwardRef(({ children, onCreate }, ref) => {
|
|||
|
||||
const handleFieldKeyDown = useCallback(
|
||||
(event) => {
|
||||
if (event.ctrlKey && event.key === 'Enter') {
|
||||
if (event.key === 'Enter') {
|
||||
event.preventDefault();
|
||||
const lines = data.name.split('\n').filter(line => line.trim() !== '');
|
||||
lines.forEach(line => onCreate({ name: line.trim() }));
|
||||
setData({ ...data, name: '' });
|
||||
} else if (event.key === 'Enter') {
|
||||
event.preventDefault();
|
||||
submit();
|
||||
submit(event.ctrlKey);
|
||||
}
|
||||
},
|
||||
[data, onCreate, submit],
|
||||
[submit],
|
||||
);
|
||||
|
||||
const [handleFieldBlur, handleControlMouseOver, handleControlMouseOut] = useClosableForm(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue