|
1 | | -import React from 'react' |
| 1 | +import React, { useCallback, useRef, useState } from 'react' |
2 | 2 | import ReactDOM from 'react-dom' |
3 | 3 | import ReactTags from '../lib/ReactTags' |
4 | 4 | import suggestions from './countries' |
5 | 5 |
|
6 | 6 | /** |
7 | 7 | * Demo 1 - Country selector |
8 | 8 | */ |
9 | | -class CountrySelector extends React.Component { |
10 | | - constructor (props) { |
11 | | - super(props) |
12 | 9 |
|
13 | | - this.state = { |
14 | | - tags: [ |
15 | | - { id: 184, name: 'Thailand' }, |
16 | | - { id: 86, name: 'India' } |
17 | | - ], |
18 | | - suggestions |
19 | | - } |
| 10 | +function CountrySelector () { |
| 11 | + const [tags, setTags] = useState([]) |
20 | 12 |
|
21 | | - this.reactTags = React.createRef() |
22 | | - } |
| 13 | + const reactTags = useRef() |
23 | 14 |
|
24 | | - onDelete (i) { |
25 | | - const tags = this.state.tags.slice(0) |
26 | | - tags.splice(i, 1) |
27 | | - this.setState({ tags }) |
28 | | - } |
| 15 | + const onDelete = useCallback((tagIndex) => { |
| 16 | + setTags(tags.filter((_, i) => i !== tagIndex)) |
| 17 | + }, [tags]) |
29 | 18 |
|
30 | | - onAddition (tag) { |
31 | | - const tags = [].concat(this.state.tags, tag) |
32 | | - this.setState({ tags }) |
33 | | - } |
| 19 | + const onAddition = useCallback((newTag) => { |
| 20 | + setTags([...tags, newTag]) |
| 21 | + }, [tags]) |
34 | 22 |
|
35 | | - render () { |
36 | | - return ( |
37 | | - <> |
38 | | - <p>Select the countries you have visited below:</p> |
39 | | - <ReactTags |
40 | | - ref={this.reactTags} |
41 | | - tags={this.state.tags} |
42 | | - suggestions={this.state.suggestions} |
43 | | - noSuggestionsText='No matching countries' |
44 | | - onDelete={this.onDelete.bind(this)} |
45 | | - onAddition={this.onAddition.bind(this)} |
46 | | - /> |
47 | | - <p><b>Output:</b></p> |
48 | | - <pre><code>{JSON.stringify(this.state.tags, null, 2)}</code></pre> |
49 | | - </> |
50 | | - ) |
51 | | - } |
| 23 | + return ( |
| 24 | + <> |
| 25 | + <p>Select the countries you have visited below:</p> |
| 26 | + <ReactTags |
| 27 | + ref={reactTags} |
| 28 | + tags={tags} |
| 29 | + suggestions={suggestions} |
| 30 | + noSuggestionsText='No matching countries' |
| 31 | + onDelete={onDelete} |
| 32 | + onAddition={onAddition} |
| 33 | + /> |
| 34 | + <p><b>Output:</b></p> |
| 35 | + <pre><code>{JSON.stringify(tags, null, 2)}</code></pre> |
| 36 | + </> |
| 37 | + ) |
52 | 38 | } |
53 | 39 |
|
54 | 40 | ReactDOM.render(<CountrySelector />, document.getElementById('demo-1')) |
55 | 41 |
|
56 | 42 | /** |
57 | 43 | * Demo 2 - Custom tags |
58 | 44 | */ |
59 | | -class CustomTags extends React.Component { |
60 | | - constructor (props) { |
61 | | - super(props) |
62 | 45 |
|
63 | | - this.state = { |
64 | | - tags: [], |
65 | | - suggestions: [] |
66 | | - } |
| 46 | +function CustomTags () { |
| 47 | + const [tags, setTags] = useState([]) |
67 | 48 |
|
68 | | - this.reactTags = React.createRef() |
69 | | - } |
| 49 | + const reactTags = useRef() |
70 | 50 |
|
71 | | - onDelete (i) { |
72 | | - const tags = this.state.tags.slice(0) |
73 | | - tags.splice(i, 1) |
74 | | - this.setState({ tags }) |
75 | | - } |
| 51 | + const onDelete = useCallback((tagIndex) => { |
| 52 | + setTags(tags.filter((_, i) => i !== tagIndex)) |
| 53 | + }, [tags]) |
76 | 54 |
|
77 | | - onAddition (tag) { |
78 | | - const tags = [].concat(this.state.tags, tag) |
79 | | - this.setState({ tags }) |
80 | | - } |
| 55 | + const onAddition = useCallback((newTag) => { |
| 56 | + setTags([...tags, newTag]) |
| 57 | + }, [tags]) |
81 | 58 |
|
82 | | - onValidate (tag) { |
83 | | - return /^[a-z]{3,12}$/i.test(tag.name) |
84 | | - } |
| 59 | + const onValidate = useCallback((newTag) => { |
| 60 | + return /^[a-z]{3,12}$/i.test(newTag.name) |
| 61 | + }) |
85 | 62 |
|
86 | | - render () { |
87 | | - return ( |
88 | | - <> |
89 | | - <p>Enter new tags meeting the requirements below:</p> |
90 | | - <ReactTags |
91 | | - allowNew |
92 | | - newTagText='Create new tag:' |
93 | | - ref={this.reactTags} |
94 | | - tags={this.state.tags} |
95 | | - suggestions={this.state.suggestions} |
96 | | - onDelete={this.onDelete.bind(this)} |
97 | | - onAddition={this.onAddition.bind(this)} |
98 | | - onValidate={this.onValidate.bind(this)} |
99 | | - /> |
100 | | - <p style={{ margin: '0.25rem 0', color: 'gray' }}><small><em>Tags must be 3–12 characters in length and only contain the letters A-Z</em></small></p> |
101 | | - <p><b>Output:</b></p> |
102 | | - <pre><code>{JSON.stringify(this.state.tags, null, 2)}</code></pre> |
103 | | - </> |
104 | | - ) |
105 | | - } |
| 63 | + return ( |
| 64 | + <> |
| 65 | + <p>Enter new tags meeting the requirements below:</p> |
| 66 | + <ReactTags |
| 67 | + allowNew |
| 68 | + newTagText='Create new tag:' |
| 69 | + ref={reactTags} |
| 70 | + tags={tags} |
| 71 | + suggestions={[]} |
| 72 | + onDelete={onDelete} |
| 73 | + onAddition={onAddition} |
| 74 | + onValidate={onValidate} |
| 75 | + /> |
| 76 | + <p style={{ margin: '0.25rem 0', color: 'gray' }}> |
| 77 | + <small><em>Tags must be 3–12 characters in length and only contain the letters A-Z</em></small> |
| 78 | + </p> |
| 79 | + <p><b>Output:</b></p> |
| 80 | + <pre><code>{JSON.stringify(tags, null, 2)}</code></pre> |
| 81 | + </> |
| 82 | + ) |
106 | 83 | } |
107 | 84 |
|
108 | 85 | ReactDOM.render(<CustomTags />, document.getElementById('demo-2')) |
0 commit comments