@@ -15,64 +15,101 @@ import Label from 'components/Form/Label/Label';
15
15
import ThemedReactSelect from './ThemedReactSelect' ;
16
16
import styles from './Select.module.css' ;
17
17
18
- Select . propTypes = {
19
- field : shape ( {
20
- name : string . isRequired ,
21
- value : oneOfType ( [ string , number , bool , arrayOf ( string ) , arrayOf ( number ) , arrayOf ( bool ) ] )
22
- . isRequired ,
23
- } ) . isRequired ,
24
- form : shape ( {
25
- // TODO: Resolve why multiselects can end up with touched: { key: array }
26
- // see ThemedReactSelect as well
27
- // touched: objectOf(bool).isRequired,
28
- touched : object . isRequired ,
29
- errors : objectOf ( string ) . isRequired ,
30
- setFieldTouched : func . isRequired ,
31
- setFieldValue : func . isRequired ,
32
- } ) . isRequired ,
33
- hasValidationStyling : bool ,
34
- id : oneOfType ( [ string , number ] ) ,
35
- isLabelHidden : bool ,
36
- isMulti : bool ,
37
- isSearchable : bool ,
38
- label : string . isRequired ,
39
- options : arrayOf ( shape ( { label : string . isRequired , value : string . isRequired } ) . isRequired )
40
- . isRequired ,
18
+ type FieldType = {
19
+ name : string ;
20
+ value : string | string [ ] | number | number [ ] | boolean | boolean [ ] ;
41
21
} ;
42
22
43
- Select . defaultProps = {
44
- hasValidationStyling : true ,
45
- id : undefined ,
46
- isLabelHidden : false ,
47
- isMulti : false ,
48
- isSearchable : true ,
23
+ type TouchedType = Record < string , any > ;
24
+
25
+ type ErrorsType = {
26
+ [ key : string ] : string ;
27
+ } ;
28
+
29
+ type FormType = {
30
+ touched : TouchedType ;
31
+ errors : ErrorsType ;
32
+ setFieldTouched : ( name : string ) => void ;
33
+ setFieldValue : ( name : string , value : string | string [ ] ) => void ;
34
+ } ;
35
+
36
+ type SelectOptionType = {
37
+ label : string ;
38
+ value : string ;
39
+ } ;
40
+
41
+ type SelectOptionsType = SelectOptionType [ ] ;
42
+
43
+ export type SelectPropsType = {
44
+ field : FieldType ;
45
+ form : FormType ;
46
+ options : SelectOptionsType ;
47
+ label : string ;
48
+ hasValidationStyling ?: boolean ;
49
+ id ?: string ;
50
+ isLabelHidden ?: boolean ;
51
+ isMulti ?: boolean ;
52
+ isSearchable ?: boolean ;
49
53
} ;
50
54
55
+ // Select.propTypes = {
56
+ // field: shape({
57
+ // name: string.isRequired,
58
+ // value: oneOfType([string, number, bool, arrayOf(string), arrayOf(number), arrayOf(bool)])
59
+ // .isRequired,
60
+ // }).isRequired,
61
+ // form: shape({
62
+ // // TODO: Resolve why multiselects can end up with touched: { key: array }
63
+ // // see ThemedReactSelect as well
64
+ // // touched: objectOf(bool).isRequired,
65
+ // touched: object.isRequired,
66
+ // errors: objectOf(string).isRequired,
67
+ // setFieldTouched: func.isRequired,
68
+ // setFieldValue: func.isRequired,
69
+ // }).isRequired,
70
+ // hasValidationStyling: bool,
71
+ // id: oneOfType([string, number]),
72
+ // isLabelHidden: bool,
73
+ // isMulti: bool,
74
+ // isSearchable: bool,
75
+ // label: string.isRequired,
76
+ // options: arrayOf(shape({ label: string.isRequired, value: string.isRequired }).isRequired)
77
+ // .isRequired,
78
+ // };
79
+
80
+ // Select.defaultProps = {
81
+ // hasValidationStyling: true,
82
+ // id: undefined,
83
+ // isLabelHidden: false,
84
+ // isMulti: false,
85
+ // isSearchable: true,
86
+ // };
87
+
51
88
export default function Select ( {
52
89
field : { name, value : fieldValue } ,
53
90
form : { errors, setFieldTouched, setFieldValue, touched } ,
54
- hasValidationStyling,
91
+ hasValidationStyling = true ,
55
92
id,
56
- isLabelHidden,
57
- isMulti,
58
- isSearchable,
93
+ isLabelHidden = false ,
94
+ isMulti = false ,
95
+ isSearchable = true ,
59
96
label,
60
97
options,
61
98
...props // disabled, placeholder, etc.
62
- } ) {
99
+ } : SelectPropsType ) {
63
100
/**
64
101
* @description handle changing of non-multi select
65
102
* @param {string } selected
66
103
*/
67
- const onChangeSingle = selected => {
104
+ const onChangeSingle = ( selected : SelectOptionType ) => {
68
105
setFieldValue ( name , selected === null ? '' : selected . value ) ;
69
106
} ;
70
107
71
108
/**
72
109
* @description handle changing of multi select
73
110
* @param {string[] } selectedArray
74
111
*/
75
- const onChangeMulti = selectedArray => {
112
+ const onChangeMulti = ( selectedArray : SelectOptionsType ) => {
76
113
if ( selectedArray ) {
77
114
setFieldValue (
78
115
name ,
@@ -96,7 +133,17 @@ export default function Select({
96
133
* @returns {{ label: string; value: string; }[] }
97
134
*/
98
135
const getValueFromMulti = ( ) => {
99
- return options . filter ( option => fieldValue . includes ( option . value ) ) ;
136
+ if ( Array . isArray ( fieldValue ) ) {
137
+ return fieldValue
138
+ . map (
139
+ value =>
140
+ options . find ( ( option : SelectOptionType ) => option . value === value ) as SelectOptionType ,
141
+ )
142
+ . filter ( Boolean ) ;
143
+ } else {
144
+ return [ ] ;
145
+ }
146
+ // return options.filter(option => fieldValue.includes(option.value));
100
147
} ;
101
148
102
149
const handleBlur = ( ) => {
@@ -126,13 +173,13 @@ export default function Select({
126
173
isSearchable = { isSearchable }
127
174
name = { name }
128
175
onBlur = { handleBlur }
129
- onChange = { onChangeHandler }
176
+ onChange = { ( ) => onChangeHandler }
130
177
options = { options }
131
178
value = { value || '' }
132
179
/>
133
180
134
181
< ErrorMessage name = { name } >
135
- { message => {
182
+ { ( message : string ) => {
136
183
return hasErrors ? (
137
184
< Alert className = { styles . errorMessage } type = "error" >
138
185
{ message }
0 commit comments