|
1 | | -import React from 'react' |
2 | | -import { Button, InputGroup, InputGroupAddon, Input } from 'reactstrap' |
| 1 | +import React, { useRef } from 'react' |
| 2 | +import { Select } from '@oacore/design' |
3 | 3 |
|
4 | | -const SearchField = ({ |
5 | | - size = '', |
6 | | - id = 'search-form-field', |
7 | | - label = 'Search in CORE', |
8 | | - ...fieldProps |
9 | | -}) => ( |
10 | | - <> |
11 | | - <label className="sr-only" htmlFor={id}> |
12 | | - {label} |
13 | | - </label> |
14 | | - <InputGroup size={size}> |
15 | | - <Input type="search" id={id} {...fieldProps} /> |
16 | | - <InputGroupAddon addonType="append"> |
17 | | - <Button color="primary">Search</Button> |
18 | | - </InputGroupAddon> |
19 | | - </InputGroup> |
20 | | - </> |
21 | | -) |
| 4 | +import styles from './styles.module.css' |
| 5 | + |
| 6 | +const options = [ |
| 7 | + { id: 1, icon: '#magnify', value: 'Option A' }, |
| 8 | + { id: 2, icon: '#magnify', value: 'Option B' }, |
| 9 | + { id: 3, icon: '#magnify', value: 'Option C' }, |
| 10 | + { id: 4, icon: '#magnify', value: 'Option D' }, |
| 11 | + { id: 5, icon: '#magnify', value: 'Option E' }, |
| 12 | +] |
| 13 | + |
| 14 | +const SearchAutocompletion = ({ formRef, ...passProps }) => { |
| 15 | + const [suggestions, setSuggestions] = React.useState(options) |
| 16 | + const [value, setValue] = React.useState('') |
| 17 | + |
| 18 | + const handleOnChange = data => { |
| 19 | + if (data.value === '') return |
| 20 | + formRef.current.submit() |
| 21 | + } |
| 22 | + |
| 23 | + const handleOnInput = data => { |
| 24 | + // if id doesn't exists it means user type own text |
| 25 | + // and didn't use suggestion |
| 26 | + if (!data.id) { |
| 27 | + setSuggestions( |
| 28 | + options.slice(0, Math.max(0, options.length - data.value.length)) |
| 29 | + ) |
| 30 | + } |
| 31 | + |
| 32 | + setValue(data.value) |
| 33 | + } |
| 34 | + |
| 35 | + return ( |
| 36 | + <Select |
| 37 | + id="search-select" |
| 38 | + value={value} |
| 39 | + onChange={handleOnChange} |
| 40 | + onInput={handleOnInput} |
| 41 | + prependIcon="#magnify" |
| 42 | + className={styles.searchBox} |
| 43 | + clearButtonClassName={styles.clearButton} |
| 44 | + selectMenuClassName={styles.selectMenu} |
| 45 | + changeOnBlur={false} |
| 46 | + {...passProps} |
| 47 | + > |
| 48 | + {suggestions.map(el => ( |
| 49 | + <Select.Option |
| 50 | + key={el.id} |
| 51 | + id={el.id} |
| 52 | + value={el.value} |
| 53 | + icon={el.icon} |
| 54 | + className={styles.option} |
| 55 | + > |
| 56 | + {el.value} |
| 57 | + </Select.Option> |
| 58 | + ))} |
| 59 | + {value !== '' && ( |
| 60 | + <Select.Option |
| 61 | + key={6} |
| 62 | + id={6} |
| 63 | + value={value} |
| 64 | + icon="#magnify" |
| 65 | + className={`${styles.option} ${styles.lastOption}`} |
| 66 | + > |
| 67 | + {`All results for "${value}"`} |
| 68 | + </Select.Option> |
| 69 | + )} |
| 70 | + </Select> |
| 71 | + ) |
| 72 | +} |
22 | 73 |
|
23 | 74 | const SearchForm = ({ |
24 | 75 | action, |
25 | 76 | method, |
26 | 77 | onSubmit, |
27 | 78 | id = 'search-form', |
28 | 79 | ...fieldProps |
29 | | -}) => ( |
30 | | - <form id={id} action={action} method={method} onSubmit={onSubmit}> |
31 | | - <SearchField id={`${id}-field`} size="lg" {...fieldProps} /> |
32 | | - </form> |
33 | | -) |
| 80 | +}) => { |
| 81 | + const ref = useRef(null) |
| 82 | + return ( |
| 83 | + <form ref={ref} id={id} action={action} method={method} onSubmit={onSubmit}> |
| 84 | + <SearchAutocompletion id={`${id}-field`} {...fieldProps} formRef={ref} /> |
| 85 | + </form> |
| 86 | + ) |
| 87 | +} |
34 | 88 |
|
35 | 89 | export default SearchForm |
0 commit comments