i-like-robots/react-tags

Adding tags only once

Closed this issue · 2 comments

Hi,
how can I add only unique tags?

I sort them like this: handleValidate = tag => tags.indexOf(tag) === -1;

But it still in suggestions, should I handle it also, or there is another way how to handle this?

Thanks,
Best regards

For future generations, expected behavior can be achieved by filtering suggestions by already added tags. Both collections are in control of the user, so it is easily doable without any changes in this very library.

For future generations v2, in addition to what @jacekk said, this is a working example used in Formik

suggestionsFilter={(suggestion) =>
  !isLabelAdded(suggestion, props.values.labels)
}
 handleAddition={(label) => {
   if (!isLabelAdded(label, props.values.labels)) {
     const tempLabels = [].concat(props.values.labels, label)
     props.setFieldValue('labels', tempLabels)
   }
 }}

being isLabelAdded a helper function

  const isLabelAdded = (test, labelList) => {
    return labelList.find((label) => label.name === test.name)
  }

Thanks for this fantastic library