Tagify provides a React wrapper that is dependency-light and JSX-free. Note that Tagify is not a controlled component.
To use it, import the component from @yaireo/tagify/react and ensure you also import the Tagify CSS. If using SCSS, it is preferable to use @import with dart-sass.
Important onChange behavior: The onChange event argument e includes a detail parameter. To access the original DOM input element, use e.detail.tagify.DOM.originalInput.
import { useCallback, useRef } from 'react'
import Tags from '@yaireo/tagify/react' // React-wrapper file
import '@yaireo/tagify/dist/tagify.css' // Tagify CSS
const App = () => {
// on tag add/edit/remove
const onChange = useCallback((e) => {
console.log("CHANGED:"
, e.detail.tagify.value // Array where each tag includes tagify's (needed) extra properties
, e.detail.tagify.getCleanValue() // Same as above, without the extra properties
, e.detail.value // a string representing the tags
)
}, [])
return (
<Tags
whitelist={['item 1', 'another item', 'item 3']}
placeholder='Add some tags'
settings={{
blacklist: ["xxx"],
maxTags: 4,
dropdown: {
enabled: 0 // always show suggestions dropdown
}
}}
defaultValue="a,b,c" // initial value
onChange={onChange}
/>
)
}