@@ -22,59 +22,45 @@ npm install --save react-tag-autocomplete
22
22
23
23
## Usage
24
24
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 ) .
26
26
27
27
``` js
28
- import React from ' react'
29
- import ReactDOM from ' react-dom'
28
+ import React , { useCallback , useRef , useState } from ' react'
30
29
import ReactTags from ' react-tag-autocomplete'
31
30
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 ([])
51
33
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
+ ])
57
42
58
- onAddition (tag ) {
59
- const tags = [].concat (this .state .tags , tag)
60
- this .setState ({ tags })
61
- }
43
+ const reactTags = useRef ()
62
44
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])
74
48
75
- ReactDOM .render (< App / > , document .getElementById (' app' ))
76
- ```
49
+ const onAddition = useCallback ((newTag ) => {
50
+ setTags ([... tags, newTag])
51
+ }, [tags])
77
52
53
+ return (
54
+ < ReactTags
55
+ ref= {reactTags}
56
+ tags= {tags}
57
+ suggestions= {suggestions}
58
+ onDelete= {onDelete}
59
+ onAddition= {onAddition}
60
+ / >
61
+ )
62
+ }
63
+ ```
78
64
79
65
### Options
80
66
@@ -218,9 +204,10 @@ Override the default class names used by the component. Defaults to:
218
204
Function called when the user wants to add a tag. Receives the tag.
219
205
220
206
``` 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])
224
211
}
225
212
```
226
213
@@ -229,10 +216,10 @@ function onAddition(tag) {
229
216
Function called when the user wants to delete a tag. Receives the tag index.
230
217
231
218
``` 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) )
236
223
}
237
224
```
238
225
@@ -241,12 +228,14 @@ function onDelete(i) {
241
228
Optional event handler when the input value changes. Receives the current query.
242
229
243
230
``` 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 )
247
236
248
- return fetch (` query=${ query} ` ).then ((result ) => {
249
- this . setState ({ busy : false } )
237
+ return fetch (` ? query=${ query} ` ).then ((result ) => {
238
+ setIsBusy ( false )
250
239
})
251
240
}
252
241
}
@@ -265,7 +254,7 @@ Optional callback function for when focus on the input is lost. Receives no argu
265
254
Optional validation function that determines if tag should be added. Receives the tag object and must return a boolean.
266
255
267
256
``` js
268
- function onValidate (tag ) {
257
+ function onValidate (tag ) {
269
258
return tag .name .length >= 5 ;
270
259
}
271
260
```
@@ -287,9 +276,9 @@ Enable users to delete selected tags when backspace is pressed while focussed on
287
276
Provide a custom tag component to render. Receives the tag, button text, and delete callback as props. Defaults to ` null ` .
288
277
289
278
``` jsx
290
- function TagComponent ({ tag, removeButtonText, onDelete }) {
279
+ function TagComponent ({ tag, removeButtonText, onDelete }) {
291
280
return (
292
- < button type= ' button' title= {removeButtonText} onClick= {onDelete}>
281
+ < button type= ' button' title= {` ${ removeButtonText} : ${ tag . name } ` } onClick= {onDelete}>
293
282
{tag .name }
294
283
< / button>
295
284
)
@@ -301,7 +290,7 @@ function TagComponent({ tag, removeButtonText, onDelete }) {
301
290
Provide a custom suggestion component to render. Receives the suggestion and current query as props. Defaults to ` null ` .
302
291
303
292
``` jsx
304
- function SuggestionComponent ({ item, query }) {
293
+ function SuggestionComponent ({ item, query }) {
305
294
return (
306
295
< span id= {item .id } className= {item .name === query ? ' match' : ' no-match' }>
307
296
{item .name }
0 commit comments