1
1
import React , { useCallback } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
4
- import { getValueByPath } from '../utils/getters-setters' ;
5
- import { normalizeOptions , booleanOrFunction } from './helpers/form-helpers' ;
4
+ import {
5
+ normalizeOptions ,
6
+ booleanOrFunction ,
7
+ serializeValue ,
8
+ getSelectedOption ,
9
+ getOptionsType ,
10
+ } from './helpers/form-helpers' ;
6
11
import { useFormControl } from './helpers/useFormControl' ;
7
12
8
13
export function FormSelect ( {
@@ -32,7 +37,7 @@ export function FormSelect({
32
37
< select
33
38
{ ...attrs }
34
39
className = "custom-select"
35
- onChange = { handleOnChange }
40
+ onChange = { ( e ) => handleOnChange ( e , getOptionsType ( normalizedOptions ) ) }
36
41
value = { getSelectedOption ( value , normalizedOptions , trackBy ) }
37
42
ref = { registerRef }
38
43
>
@@ -52,6 +57,7 @@ FormSelect.propTypes = {
52
57
PropTypes . arrayOf (
53
58
PropTypes . oneOfType ( [
54
59
PropTypes . string ,
60
+ PropTypes . number ,
55
61
PropTypes . shape ( { value : PropTypes . any . isRequired , label : PropTypes . string . isRequired } ) ,
56
62
] )
57
63
) ,
@@ -68,27 +74,3 @@ function renderOptions(options, trackBy) {
68
74
</ option >
69
75
) ) ;
70
76
}
71
-
72
- function getSelectedOption ( value , options , trackBy ) {
73
- let selectedValue = value ;
74
-
75
- if ( trackBy ) {
76
- const selectedOption = options . find (
77
- ( option ) => getValueByPath ( option . value , trackBy ) === getValueByPath ( value , trackBy )
78
- ) ;
79
-
80
- if ( selectedOption ) {
81
- selectedValue = selectedOption . value ;
82
- }
83
- }
84
-
85
- return serializeValue ( selectedValue ) ;
86
- }
87
-
88
- function serializeValue ( value ) {
89
- if ( typeof value !== 'object' ) {
90
- return value ;
91
- }
92
-
93
- return JSON . stringify ( value ) ;
94
- }
0 commit comments