Skip to content

Commit 2d7f535

Browse files
committed
Altered deserializer format
1 parent 1f8808c commit 2d7f535

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

src/Field.tsx

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ export function Field<T extends object, K extends keyof T, C extends React.Funct
3131
const { value, setValue, state } = useListener(form, props.name);
3232
const onChange = useCallback(
3333
(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));
3636
},
3737
[setValue]
3838
);
@@ -66,7 +66,7 @@ export type SerializeType =
6666
| "range";
6767

6868
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;
7070

7171
export type SerializeProps<V = any> = {
7272
dateAsNumber?: boolean;
@@ -77,7 +77,6 @@ export type SerializeProps<V = any> = {
7777
};
7878

7979
export function defaultSerializer<T>(currentValue: T, props: SerializeProps<T>): boolean | string {
80-
console.log("serialize", currentValue, props);
8180
switch (props.type) {
8281
case "datetime-local":
8382
case "date": {
@@ -113,45 +112,44 @@ export function defaultSerializer<T>(currentValue: T, props: SerializeProps<T>):
113112
}
114113
}
115114

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>) {
118116
switch (props.type) {
119117
case "number": {
120-
return parseFloat(inputValue) as any;
118+
return parseFloat(inputValue as any);
121119
}
122120
case "datetime-local":
123121
case "date": {
124122
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;
127125
} else {
128-
return null as any;
126+
return null;
129127
}
130128
}
131129
case "radio": {
132130
// Enum field
133-
if (inputChecked) {
134-
return props.value as any;
131+
if (inputValue) {
132+
return props.value;
135133
}
136134
return currentValue;
137135
}
138136
case "checkbox": {
139137
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") {
141139
console.error(
142140
"Checkbox using setNullOnUncheck got checked but a value to set was not found, please provide a value to the value prop."
143141
);
144142
}
145-
return inputChecked ? props.value : ((props.setNullOnUncheck ? null : undefined) as any);
143+
return inputValue ? props.value : ((props.setNullOnUncheck ? null : undefined) as any);
146144
} else if (props.value !== undefined) {
147145
// Primitive array field
148146
let arr = Array.isArray(currentValue) ? [...currentValue] : [];
149-
if (inputChecked) arr.push(props.value);
147+
if (inputValue) arr.push(props.value);
150148
else arr.splice(arr.indexOf(props.value), 1);
151149
return arr as any;
152150
} else {
153151
// Boolean field
154-
return inputChecked as any;
152+
return inputValue;
155153
}
156154
}
157155
default: {

src/hooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ export function useArrayForm<
199199
}
200200

201201
/**
202-
* Listen for truthy changes (if a value becomes truthy or falsy) on a form's field. Behaves like useState.
202+
* Listen for truthy changes (if a value becomes truthy or falsy) on a form's field.
203203
* @param form The form to listen on.
204204
* @param name The form's field to listen to.
205205
*/

0 commit comments

Comments
 (0)