Skip to content

Commit

Permalink
Clean up logic in example
Browse files Browse the repository at this point in the history
  • Loading branch information
rolfheij-sil committed Nov 21, 2024
1 parent beea93f commit f1956b1
Showing 1 changed file with 8 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ export default function ComboBoxExamples({ direction }: HasDirection) {
const [comboBox4Value, setComboBox4Value] = useState<string[] | undefined>(undefined);

const handleComboBox4Change = (newValue: string) => {
if (!comboBox4Value || comboBox4Value.length === 0) {
setComboBox4Value([newValue]);
return;
}
setComboBox4Value((prevValue) => {
if (!prevValue || prevValue.length === 0) {
return [newValue];
}

let newSelection: string[] = [];

if (comboBox4Value.includes(newValue)) {
newSelection = comboBox4Value.filter((value) => value !== newValue);
} else {
newSelection = [...comboBox4Value, newValue];
}
setComboBox4Value(newSelection);
return prevValue.includes(newValue)
? prevValue.filter((value) => value !== newValue)
: [...prevValue, newValue];
});
};

return (
Expand Down

0 comments on commit f1956b1

Please sign in to comment.