@@ -2,31 +2,41 @@ import { useEffect, useState } from 'react'
2
2
import { Label } from 'reactstrap'
3
3
import Select from 'react-select/creatable'
4
4
5
- const EmployerDropDown = ( { arrayList, isEmpty, onChange, value } ) => {
6
- const [ customEntry , setCustomEntry ] = useState ( false )
7
- const [ employersList , setEmployersList ] = useState ( [ ] )
5
+ const EmployerDropDown = ( {
6
+ arrayList : employers ,
7
+ isEmpty,
8
+ onChange,
9
+ value
10
+ } ) => {
11
+ const [ isCustomEntry , setIsCustomEntry ] = useState ( false )
12
+ const [ options , setOptions ] = useState ( [ ] )
13
+ const [ selectedOption , setSelectedOption ] = useState ( )
8
14
9
15
useEffect ( ( ) => {
10
- setEmployersList (
11
- arrayList . map ( ( { _id, name } ) => ( {
16
+ setOptions (
17
+ employers . map ( ( { _id, name } ) => ( {
12
18
value : _id ,
13
19
label : name
14
20
} ) )
15
21
)
16
- } , [ arrayList ] )
22
+ } , [ employers ] )
17
23
18
24
useEffect ( ( ) => {
19
- setCustomEntry (
20
- value !== '' && ! arrayList . some ( ( { name } ) => name === value )
25
+ setIsCustomEntry (
26
+ value !== '' && ! employers . some ( ( { name } ) => name === value )
21
27
)
22
- } , [ arrayList , value ] )
28
+ } , [ employers , value ] )
29
+
30
+ useEffect ( ( ) => {
31
+ setSelectedOption ( options . find ( employer => employer . value === value ) )
32
+ } , [ options , value ] )
23
33
24
34
const handleChange = e =>
25
35
onChange ( {
26
36
target : {
27
37
name : 'employer' ,
28
38
type : 'text' ,
29
- value : e === null ? '' : e . value
39
+ value : e ?. value ?? ''
30
40
}
31
41
} )
32
42
@@ -36,19 +46,19 @@ const EmployerDropDown = ({ arrayList, isEmpty, onChange, value }) => {
36
46
< Select
37
47
className = { isEmpty ? 'is-empty' : '' }
38
48
inputId = "employer"
39
- isSearchable
40
49
isClearable
41
- options = { employersList }
50
+ isSearchable
51
+ name = "employer"
42
52
onChange = { handleChange }
43
53
onCreateOption = { newEmployer => {
44
- setEmployersList ( oldList => insertedInto ( [ ...oldList ] , newEmployer ) )
54
+ setOptions ( oldOptions => insertedInto ( [ ...oldOptions ] , newEmployer ) )
45
55
handleChange ( { value : newEmployer } )
46
56
} }
47
- name = "employer"
57
+ options = { options }
48
58
placeholder = "Type your employer name here"
49
- value = { employersList . find ( employer => employer . value === value ) }
59
+ value = { selectedOption }
50
60
/>
51
- { customEntry && (
61
+ { isCustomEntry && (
52
62
< p className = "reminder" >
53
63
This employer will be added to our list. Make sure you typed it
54
64
correctly.
@@ -58,17 +68,17 @@ const EmployerDropDown = ({ arrayList, isEmpty, onChange, value }) => {
58
68
)
59
69
}
60
70
61
- const insertedInto = ( employers , employer ) => {
62
- const canonical = employer . toLowerCase ( )
63
- const entry = { label : employer , value : employer }
64
- for ( let index = 0 ; index < employers . length ; index ++ ) {
65
- if ( canonical < employers [ index ] . value . toLowerCase ( ) ) {
66
- employers . splice ( index , 0 , entry )
67
- return employers
71
+ const insertedInto = ( options , newEmployer ) => {
72
+ const canonical = newEmployer . toLowerCase ( )
73
+ const entry = { label : newEmployer , value : newEmployer }
74
+ for ( let index = 0 ; index < options . length ; index ++ ) {
75
+ if ( canonical < options [ index ] . value . toLowerCase ( ) ) {
76
+ options . splice ( index , 0 , entry )
77
+ return options
68
78
}
69
79
}
70
- employers . push ( entry )
71
- return employers
80
+ options . push ( entry )
81
+ return options
72
82
}
73
83
74
84
export default EmployerDropDown
0 commit comments