Ensured TagsSelector does not allow duplicated tags, and allows adding multiple coma-separated tags at once

This commit is contained in:
Alejandro Celaya 2021-08-15 09:45:14 +02:00
parent cbe5f98aa3
commit b8a7dccf92

View file

@ -44,13 +44,18 @@ const TagsSelector = (colorGenerator: ColorGenerator) => (
addOnBlur
placeholderText={placeholder}
minQueryLength={1}
delimiters={[ 'Enter', 'Tab', ',' ]}
onDelete={(removedTagIndex) => {
const tagsCopy = [ ...selectedTags ];
tagsCopy.splice(removedTagIndex, 1);
onChange(tagsCopy);
}}
onAddition={({ name: newTag }) => onChange([ ...selectedTags, newTag.toLowerCase() ])}
onAddition={({ name: newTag }) => onChange(
// * Avoid duplicated tags (thanks to the Set),
// * Split any of the new tags by comma, allowing to paste multiple comma-separated tags at once.
[ ...new Set([ ...selectedTags, ...newTag.toLowerCase().split(',') ]) ],
)}
/>
);
};