Skip to content

Commit cbe00f2

Browse files
committed
feat: SelectNames component now has ability to receive comma separated names
1 parent 5ba241e commit cbe00f2

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

components/SelectNames.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface SelectNamesProps {
1010
}
1111

1212
const SelectNames: React.FC<SelectNamesProps> = ({ onSelect, initialValue }) => {
13-
let initialOptions = initialValue ? initialValue.split(", ").map((val: any) => ({ label: val, value: val })) : [];
13+
const initialOptions = initialValue ? initialValue.split(",").map(val => ({ label: val.trim(), value: val.trim() })) : [];
1414
const [selectedLabels, setSelectedLabels] = React.useState(initialOptions);
1515
const { myVariable } = useMyVariable();
1616
const options = myVariable.names || [
@@ -20,7 +20,7 @@ const SelectNames: React.FC<SelectNamesProps> = ({ onSelect, initialValue }) =>
2020
];
2121

2222
React.useEffect(() => {
23-
let initialOptions = initialValue ? initialValue.split(", ").map(val => ({ label: val, value: val })) : [];
23+
const initialOptions = initialValue ? initialValue.split(",").map(val => ({ label: val.trim(), value: val.trim() })) : [];
2424
setSelectedLabels(initialOptions);
2525
}, [initialValue]);
2626

@@ -31,12 +31,24 @@ const SelectNames: React.FC<SelectNamesProps> = ({ onSelect, initialValue }) =>
3131
onSelect(labs.join(", ")); // Update parent component's state
3232
}
3333

34+
const handlePaste = (event: React.ClipboardEvent<HTMLDivElement>) => {
35+
event.preventDefault();
36+
const pastedText = event.clipboardData.getData('text');
37+
const pastedNames = pastedText.split(',').map(name => name.trim()).filter(Boolean);
38+
const newOptions = pastedNames.map(name => ({ label: name, value: name }));
39+
setSelectedLabels(prevLabels => [...prevLabels, ...newOptions]);
40+
handleInputChange([...selectedLabels, ...newOptions]);
41+
};
42+
3443
return (
35-
<div title="When you type, hit enter to add item and start typing again to add another or select from the dropdown">
44+
<div
45+
title="When you type, hit enter to add item and start typing again to add another or select from the dropdown. You can also paste a comma-separated list of names."
46+
onPaste={handlePaste}
47+
>
3648
<CreatableSelect
3749
isMulti
3850
options={options}
39-
value={selectedLabels} // Make it a controlled component
51+
value={selectedLabels}
4052
onChange={(selected) => {
4153
handleInputChange(selected || []);
4254
}}
@@ -74,4 +86,4 @@ const SelectNames: React.FC<SelectNamesProps> = ({ onSelect, initialValue }) =>
7486
);
7587
};
7688

77-
export default SelectNames;
89+
export default SelectNames;

0 commit comments

Comments
 (0)