|
1 | 1 | import _ from 'lodash' |
2 | 2 | import faker from 'faker' |
3 | | -import React, { Component } from 'react' |
| 3 | +import React from 'react' |
4 | 4 | import { Search, Grid, Header, Segment } from 'semantic-ui-react' |
5 | 5 |
|
6 | | -const initialState = { isLoading: false, results: [], value: '' } |
7 | | - |
8 | 6 | const source = _.times(5, () => ({ |
9 | 7 | title: faker.company.companyName(), |
10 | 8 | description: faker.company.catchPhrase(), |
11 | 9 | image: faker.internet.avatar(), |
12 | 10 | price: faker.finance.amount(0, 100, 2, '$'), |
13 | 11 | })) |
14 | 12 |
|
15 | | -export default class SearchExampleStandard extends Component { |
16 | | - state = initialState |
| 13 | +const initialState = { |
| 14 | + loading: false, |
| 15 | + results: [], |
| 16 | + value: '', |
| 17 | +} |
| 18 | + |
| 19 | +function exampleReducer(state, action) { |
| 20 | + switch (action.type) { |
| 21 | + case 'CLEAN_QUERY': |
| 22 | + return initialState |
| 23 | + case 'START_SEARCH': |
| 24 | + return { ...state, loading: true, value: action.query } |
| 25 | + case 'FINISH_SEARCH': |
| 26 | + return { ...state, loading: false, results: action.results } |
| 27 | + case 'UPDATE_SELECTION': |
| 28 | + return { ...state, value: action.selection } |
17 | 29 |
|
18 | | - handleResultSelect = (e, { result }) => this.setState({ value: result.title }) |
| 30 | + default: |
| 31 | + throw new Error() |
| 32 | + } |
| 33 | +} |
19 | 34 |
|
20 | | - handleSearchChange = (e, { value }) => { |
21 | | - this.setState({ isLoading: true, value }) |
| 35 | +function SearchExampleStandard() { |
| 36 | + const [state, dispatch] = React.useReducer(exampleReducer, initialState) |
| 37 | + const { loading, results, value } = state |
22 | 38 |
|
23 | | - setTimeout(() => { |
24 | | - if (this.state.value.length < 1) return this.setState(initialState) |
| 39 | + const timeoutRef = React.useRef() |
| 40 | + const handleSearchChange = React.useCallback((e, data) => { |
| 41 | + clearTimeout(timeoutRef.current) |
| 42 | + dispatch({ type: 'START_SEARCH', query: data.value }) |
25 | 43 |
|
26 | | - const re = new RegExp(_.escapeRegExp(this.state.value), 'i') |
| 44 | + timeoutRef.current = setTimeout(() => { |
| 45 | + if (data.value.length === 0) { |
| 46 | + dispatch({ type: 'CLEAN_QUERY' }) |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + const re = new RegExp(_.escapeRegExp(data.value), 'i') |
27 | 51 | const isMatch = (result) => re.test(result.title) |
28 | 52 |
|
29 | | - this.setState({ |
30 | | - isLoading: false, |
| 53 | + dispatch({ |
| 54 | + type: 'FINISH_SEARCH', |
31 | 55 | results: _.filter(source, isMatch), |
32 | 56 | }) |
33 | 57 | }, 300) |
34 | | - } |
| 58 | + }, []) |
| 59 | + React.useEffect(() => { |
| 60 | + return () => { |
| 61 | + clearTimeout(timeoutRef.current) |
| 62 | + } |
| 63 | + }, []) |
35 | 64 |
|
36 | | - render() { |
37 | | - const { isLoading, value, results } = this.state |
| 65 | + return ( |
| 66 | + <Grid> |
| 67 | + <Grid.Column width={6}> |
| 68 | + <Search |
| 69 | + loading={loading} |
| 70 | + onResultSelect={(e, data) => |
| 71 | + dispatch({ type: 'UPDATE_SELECTION', selection: data.result.title }) |
| 72 | + } |
| 73 | + onSearchChange={handleSearchChange} |
| 74 | + results={results} |
| 75 | + value={value} |
| 76 | + /> |
| 77 | + </Grid.Column> |
38 | 78 |
|
39 | | - return ( |
40 | | - <Grid> |
41 | | - <Grid.Column width={6}> |
42 | | - <Search |
43 | | - loading={isLoading} |
44 | | - onResultSelect={this.handleResultSelect} |
45 | | - onSearchChange={_.debounce(this.handleSearchChange, 500, { |
46 | | - leading: true, |
47 | | - })} |
48 | | - results={results} |
49 | | - value={value} |
50 | | - /> |
51 | | - </Grid.Column> |
52 | | - <Grid.Column width={10}> |
53 | | - <Segment> |
54 | | - <Header>State</Header> |
55 | | - <pre style={{ overflowX: 'auto' }}> |
56 | | - {JSON.stringify(this.state, null, 2)} |
57 | | - </pre> |
58 | | - <Header>Options</Header> |
59 | | - <pre style={{ overflowX: 'auto' }}> |
60 | | - {JSON.stringify(source, null, 2)} |
61 | | - </pre> |
62 | | - </Segment> |
63 | | - </Grid.Column> |
64 | | - </Grid> |
65 | | - ) |
66 | | - } |
| 79 | + <Grid.Column width={10}> |
| 80 | + <Segment> |
| 81 | + <Header>State</Header> |
| 82 | + <pre style={{ overflowX: 'auto' }}> |
| 83 | + {JSON.stringify({ loading, results, value }, null, 2)} |
| 84 | + </pre> |
| 85 | + <Header>Options</Header> |
| 86 | + <pre style={{ overflowX: 'auto' }}> |
| 87 | + {JSON.stringify(source, null, 2)} |
| 88 | + </pre> |
| 89 | + </Segment> |
| 90 | + </Grid.Column> |
| 91 | + </Grid> |
| 92 | + ) |
67 | 93 | } |
| 94 | + |
| 95 | +export default SearchExampleStandard |
0 commit comments