@@ -22,59 +22,45 @@ npm install --save react-tag-autocomplete
2222
2323## Usage
2424
25- Here's a sample implementation that initializes the component with a list of preselected ` tags ` and a ` suggestions ` list . For further customization details, see [ options] ( #options ) .
25+ Here's a sample implementation that initializes the component with an empty list of ` tags ` and a pre-populated list of ` suggestions ` . For further customization details, see [ options] ( #options ) .
2626
2727``` js
28- import React from ' react'
29- import ReactDOM from ' react-dom'
28+ import React , { useCallback , useRef , useState } from ' react'
3029import ReactTags from ' react-tag-autocomplete'
3130
32- class App extends React .Component {
33- constructor (props ) {
34- super (props)
35-
36- this .state = {
37- tags: [
38- { id: 1 , name: " Apples" },
39- { id: 2 , name: " Pears" }
40- ],
41- suggestions: [
42- { id: 3 , name: " Bananas" },
43- { id: 4 , name: " Mangos" },
44- { id: 5 , name: " Lemons" },
45- { id: 6 , name: " Apricots" }
46- ]
47- }
48-
49- this .reactTags = React .createRef ()
50- }
31+ function App () {
32+ const [tags , setTags ] = useState ([])
5133
52- onDelete (i ) {
53- const tags = this .state .tags .slice (0 )
54- tags .splice (i, 1 )
55- this .setState ({ tags })
56- }
34+ const [suggestions , setSuggestions ] = useState ([
35+ { id: 1 , name: " Apples" },
36+ { id: 2 , name: " Pears" }
37+ { id: 3 , name: " Bananas" },
38+ { id: 4 , name: " Mangos" },
39+ { id: 5 , name: " Lemons" },
40+ { id: 6 , name: " Apricots" }
41+ ])
5742
58- onAddition (tag ) {
59- const tags = [].concat (this .state .tags , tag)
60- this .setState ({ tags })
61- }
43+ const reactTags = useRef ()
6244
63- render () {
64- return (
65- < ReactTags
66- ref= {this .reactTags }
67- tags= {this .state .tags }
68- suggestions= {this .state .suggestions }
69- onDelete= {this .onDelete .bind (this )}
70- onAddition= {this .onAddition .bind (this )} / >
71- )
72- }
73- }
45+ const onDelete = useCallback ((tagIndex ) => {
46+ setTags (tags .filter ((_ , i ) => i !== tagIndex))
47+ }, [tags])
7448
75- ReactDOM .render (< App / > , document .getElementById (' app' ))
76- ```
49+ const onAddition = useCallback ((newTag ) => {
50+ setTags ([... tags, newTag])
51+ }, [tags])
7752
53+ return (
54+ < ReactTags
55+ ref= {reactTags}
56+ tags= {tags}
57+ suggestions= {suggestions}
58+ onDelete= {onDelete}
59+ onAddition= {onAddition}
60+ / >
61+ )
62+ }
63+ ```
7864
7965### Options
8066
@@ -218,9 +204,10 @@ Override the default class names used by the component. Defaults to:
218204Function called when the user wants to add a tag. Receives the tag.
219205
220206``` js
221- function onAddition (tag ) {
222- const tags = [... this .state .tags , tag]
223- this .setState ({ tags })
207+ const [tags , setTags ] = useState ([])
208+
209+ function onAddition (newTag ) {
210+ setTags ([... tags, newTag])
224211}
225212```
226213
@@ -229,10 +216,10 @@ function onAddition(tag) {
229216Function called when the user wants to delete a tag. Receives the tag index.
230217
231218``` js
232- function onDelete ( i ) {
233- const tags = this . state . tags . slice ( 0 )
234- tags . splice (i, 1 )
235- this . setState ({ tags } )
219+ const [ tags , setTags ] = useState ([])
220+
221+ function onDelete ( tagIndex ) {
222+ setTags ( tags . filter (( _ , i ) => i !== tagIndex) )
236223}
237224```
238225
@@ -241,12 +228,14 @@ function onDelete(i) {
241228Optional event handler when the input value changes. Receives the current query.
242229
243230``` js
244- function onInput (query ) {
245- if (! this .state .busy ) {
246- this .setState ({ busy: true })
231+ const [isBusy , setIsBusy ] = useState (false )
232+
233+ function onInput (query ) {
234+ if (! isBusy) {
235+ setIsBusy (true )
247236
248- return fetch (` query=${ query} ` ).then ((result ) => {
249- this . setState ({ busy : false } )
237+ return fetch (` ? query=${ query} ` ).then ((result ) => {
238+ setIsBusy ( false )
250239 })
251240 }
252241}
@@ -265,7 +254,7 @@ Optional callback function for when focus on the input is lost. Receives no argu
265254Optional validation function that determines if tag should be added. Receives the tag object and must return a boolean.
266255
267256``` js
268- function onValidate (tag ) {
257+ function onValidate (tag ) {
269258 return tag .name .length >= 5 ;
270259}
271260```
@@ -287,9 +276,9 @@ Enable users to delete selected tags when backspace is pressed while focussed on
287276Provide a custom tag component to render. Receives the tag, button text, and delete callback as props. Defaults to ` null ` .
288277
289278``` jsx
290- function TagComponent ({ tag, removeButtonText, onDelete }) {
279+ function TagComponent ({ tag, removeButtonText, onDelete }) {
291280 return (
292- < button type= ' button' title= {removeButtonText} onClick= {onDelete}>
281+ < button type= ' button' title= {` ${ removeButtonText} : ${ tag . name } ` } onClick= {onDelete}>
293282 {tag .name }
294283 < / button>
295284 )
@@ -301,7 +290,7 @@ function TagComponent({ tag, removeButtonText, onDelete }) {
301290Provide a custom suggestion component to render. Receives the suggestion and current query as props. Defaults to ` null ` .
302291
303292``` jsx
304- function SuggestionComponent ({ item, query }) {
293+ function SuggestionComponent ({ item, query }) {
305294 return (
306295 < span id= {item .id } className= {item .name === query ? ' match' : ' no-match' }>
307296 {item .name }
0 commit comments