|
1 | 1 | import React, { useState } from 'react'
|
2 | 2 | import { Label } from 'reactstrap'
|
3 |
| -import '../index.css' |
| 3 | +import Select from 'react-select' |
4 | 4 |
|
5 | 5 | const EmployerDropDown = ({ employerOnChange, isEmpty, arrayList }) => {
|
6 | 6 | const [value, setValue] = useState('')
|
7 |
| - const filteredEmployers = arrayList.filter(item => { |
8 |
| - const searchTerm = value.toLowerCase() |
9 |
| - const employer = item.name.toLowerCase() |
10 |
| - const matchSearch = employer.includes(searchTerm) |
11 |
| - return searchTerm && matchSearch && employer !== searchTerm |
12 |
| - }) |
13 | 7 | const employerSorter = (a, b) => {
|
14 |
| - const aIndex = a.name.toLowerCase().indexOf(value.toLowerCase()) |
15 |
| - const bIndex = b.name.toLowerCase().indexOf(value.toLowerCase()) |
| 8 | + const aIndex = a.value.toLowerCase().indexOf(value.toLowerCase()) |
| 9 | + const bIndex = b.value.toLowerCase().indexOf(value.toLowerCase()) |
16 | 10 | return aIndex > bIndex ? 1 : aIndex < bIndex ? -1 : 0
|
17 | 11 | }
|
18 |
| - |
19 |
| - const onSearch = searchTerm => { |
20 |
| - setValue(searchTerm) |
21 |
| - employerOnChange(searchTerm) |
22 |
| - } |
| 12 | + const employersList = arrayList |
| 13 | + .map(item => ({ |
| 14 | + value: item.name, |
| 15 | + label: item.name |
| 16 | + })) |
| 17 | + .sort(employerSorter) |
23 | 18 | return (
|
24 | 19 | <div className="form-group">
|
25 |
| - <Label htmlFor="employer">"Who is your employer?"</Label> |
26 |
| - <input |
27 |
| - className={`form-control ${isEmpty && 'is-empty'}`} |
28 |
| - type="text" |
| 20 | + <Label htmlFor="employer">Who is your employer?</Label> |
| 21 | + <Select |
29 | 22 | id="employer"
|
30 |
| - value={value} |
31 |
| - onChange={e => setValue(e.target.value)} |
| 23 | + onInputChange={inputVal => setValue(inputVal)} |
| 24 | + options={employersList} |
| 25 | + onChange={e => employerOnChange(e.value)} |
32 | 26 | placeholder="Type your employer name here"
|
33 |
| - autoComplete="no" |
34 | 27 | />
|
35 |
| - <div className="options"> |
36 |
| - {filteredEmployers.sort(employerSorter).map(item => ( |
37 |
| - <option |
38 |
| - className="employer-option" |
39 |
| - onClick={() => { |
40 |
| - onSearch(item.name) |
41 |
| - }} |
42 |
| - onKeyDown={e => e.key === 'Enter' && onSearch(item.name)} |
43 |
| - key={item._id} |
44 |
| - tabIndex={0} |
45 |
| - > |
46 |
| - {item.name} |
47 |
| - </option> |
48 |
| - ))} |
49 |
| - </div> |
50 | 28 | </div>
|
51 | 29 | )
|
52 | 30 | }
|
|
0 commit comments