From 4749ea1f34b6269fbadfafd50ae706aa654ae95b Mon Sep 17 00:00:00 2001 From: Maksim Eltyshev Date: Sun, 27 Oct 2024 20:55:47 +0100 Subject: [PATCH] ref: Refactoring --- client/src/components/CardModal/Tasks/Add.jsx | 51 +++++++++++-------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/client/src/components/CardModal/Tasks/Add.jsx b/client/src/components/CardModal/Tasks/Add.jsx index 1547285d..0f8d8651 100755 --- a/client/src/components/CardModal/Tasks/Add.jsx +++ b/client/src/components/CardModal/Tasks/Add.jsx @@ -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,22 +31,34 @@ const Add = React.forwardRef(({ children, onCreate }, ref) => { setIsOpened(false); }, []); - const submit = useCallback(() => { - const cleanData = { - ...data, - name: data.name.trim(), - }; + const submit = useCallback( + (isMultiple = false) => { + const cleanData = { + ...data, + name: data.name.trim(), + }; - if (!cleanData.name) { - nameField.current.ref.current.select(); - return; - } + if (!cleanData.name) { + nameField.current.ref.current.select(); + return; + } - onCreate(cleanData); + if (isMultiple) { + cleanData.name.split(MULTIPLE_REGEX).forEach((name) => { + onCreate({ + ...cleanData, + name, + }); + }); + } else { + onCreate(cleanData); + } - setData(DEFAULT_DATA); - focusNameField(); - }, [onCreate, data, setData, focusNameField]); + setData(DEFAULT_DATA); + 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') { - 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') { + if (event.key === 'Enter') { event.preventDefault(); - submit(); + submit(event.ctrlKey); } }, - [data, onCreate, submit], + [submit], ); const [handleFieldBlur, handleControlMouseOver, handleControlMouseOut] = useClosableForm(