@@ -31,8 +31,8 @@ export function Field<T extends object, K extends keyof T, C extends React.Funct
31
31
const { value, setValue, state } = useListener ( form , props . name ) ;
32
32
const onChange = useCallback (
33
33
( ev : any ) => {
34
- let [ v , c ] = "target" in ev ? [ ev . target . value , ev . target . checked ] : [ ev , typeof ev === "boolean" ? ev : undefined ] ;
35
- setValue ( ( deserializer ?? defaultDeserializer ) ( v , c , value , serializeProps ) ) ;
34
+ let v = "target" in ev ? ( [ "checkbox" , "radio" ] . includes ( props . type ! ) ? ev . target . checked : ev . target . value ) : ev ;
35
+ setValue ( ( deserializer ?? defaultDeserializer ) ( v , value , serializeProps ) ) ;
36
36
} ,
37
37
[ setValue ]
38
38
) ;
@@ -66,7 +66,7 @@ export type SerializeType =
66
66
| "range" ;
67
67
68
68
export type Serializer < T > = ( currentValue : T , props : SerializeProps < T > ) => boolean | string ;
69
- export type Deserializer < T > = ( inputValue : string , inputChecked : boolean , currentValue : T , props : SerializeProps < T > ) => T ;
69
+ export type Deserializer < T > = ( inputValue : string | boolean , currentValue : T , props : SerializeProps < T > ) => T ;
70
70
71
71
export type SerializeProps < V = any > = {
72
72
dateAsNumber ?: boolean ;
@@ -77,7 +77,6 @@ export type SerializeProps<V = any> = {
77
77
} ;
78
78
79
79
export function defaultSerializer < T > ( currentValue : T , props : SerializeProps < T > ) : boolean | string {
80
- console . log ( "serialize" , currentValue , props ) ;
81
80
switch ( props . type ) {
82
81
case "datetime-local" :
83
82
case "date" : {
@@ -113,45 +112,44 @@ export function defaultSerializer<T>(currentValue: T, props: SerializeProps<T>):
113
112
}
114
113
}
115
114
116
- export function defaultDeserializer < T > ( inputValue : string , inputChecked : boolean , currentValue : T , props : SerializeProps < T > ) {
117
- console . log ( "deserialize" , inputValue , inputChecked , props ) ;
115
+ export function defaultDeserializer < T > ( inputValue : string | boolean , currentValue : T , props : SerializeProps < T > ) {
118
116
switch ( props . type ) {
119
117
case "number" : {
120
- return parseFloat ( inputValue ) as any ;
118
+ return parseFloat ( inputValue as any ) ;
121
119
}
122
120
case "datetime-local" :
123
121
case "date" : {
124
122
if ( inputValue ) {
125
- let d = new Date ( inputValue ) ;
126
- return ( props . dateAsNumber ? d . getTime ( ) : d ) as any ;
123
+ let d = new Date ( inputValue as any ) ;
124
+ return props . dateAsNumber ? d . getTime ( ) : d ;
127
125
} else {
128
- return null as any ;
126
+ return null ;
129
127
}
130
128
}
131
129
case "radio" : {
132
130
// Enum field
133
- if ( inputChecked ) {
134
- return props . value as any ;
131
+ if ( inputValue ) {
132
+ return props . value ;
135
133
}
136
134
return currentValue ;
137
135
}
138
136
case "checkbox" : {
139
137
if ( props . setNullOnUncheck || props . setUndefinedOnUncheck ) {
140
- if ( inputChecked && props . value === undefined && process . env . NODE_ENV === "development" ) {
138
+ if ( inputValue && props . value === undefined && process . env . NODE_ENV === "development" ) {
141
139
console . error (
142
140
"Checkbox using setNullOnUncheck got checked but a value to set was not found, please provide a value to the value prop."
143
141
) ;
144
142
}
145
- return inputChecked ? props . value : ( ( props . setNullOnUncheck ? null : undefined ) as any ) ;
143
+ return inputValue ? props . value : ( ( props . setNullOnUncheck ? null : undefined ) as any ) ;
146
144
} else if ( props . value !== undefined ) {
147
145
// Primitive array field
148
146
let arr = Array . isArray ( currentValue ) ? [ ...currentValue ] : [ ] ;
149
- if ( inputChecked ) arr . push ( props . value ) ;
147
+ if ( inputValue ) arr . push ( props . value ) ;
150
148
else arr . splice ( arr . indexOf ( props . value ) , 1 ) ;
151
149
return arr as any ;
152
150
} else {
153
151
// Boolean field
154
- return inputChecked as any ;
152
+ return inputValue ;
155
153
}
156
154
}
157
155
default : {
0 commit comments