Skip to content

Commit

Permalink
feat: Add multiple task creation with Ctrl+Enter (#921)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zananok authored Oct 27, 2024
1 parent 14dff96 commit f372113
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions client/src/components/CardModal/Tasks/Add.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand All @@ -63,8 +77,7 @@ const Add = React.forwardRef(({ children, onCreate }, ref) => {
(event) => {
if (event.key === 'Enter') {
event.preventDefault();

submit();
submit(event.ctrlKey);
}
},
[submit],
Expand Down

0 comments on commit f372113

Please sign in to comment.