@@ -2,6 +2,7 @@ import React from "react";
2
2
import { InputHTMLAttributes } from "react" ;
3
3
import { DefaultError , DefaultState , FormState } from "../form" ;
4
4
import { useListener } from "../hooks" ;
5
+ import { SerializeProps , defaultSerializer , defaultDeserializer , Serializer , Deserializer } from "./serializer" ;
5
6
6
7
export const DEFAULT_DIRTY_CLASS = "typed-form-dirty" ;
7
8
export const DEFAULT_ERROR_CLASS = "typed-form-error" ;
@@ -10,122 +11,8 @@ export function getClassName(...args: any) {
10
11
return [ ...args ] . filter ( ( e ) => ! ! e ) . join ( " " ) ;
11
12
}
12
13
13
- export type SerializeType =
14
- | "number"
15
- | "text"
16
- | "password"
17
- | "date"
18
- | "datetime-local"
19
- | "radio"
20
- | "checkbox"
21
- | "color"
22
- | "email"
23
- | "text"
24
- | "month"
25
- | "url"
26
- | "week"
27
- | "time"
28
- | "tel"
29
- | "range" ;
30
-
31
- export type Serializer < T > = ( currentValue : T , props : SerializeProps < T > ) => boolean | string ;
32
-
33
- export type Deserializer < T > = ( inputValue : string , inputChecked : boolean , currentValue : T , props : SerializeProps < T > ) => T ;
34
-
35
- export type SerializeProps < V = any > = {
36
- dateAsNumber ?: boolean ;
37
- setUndefinedOnUncheck ?: boolean ;
38
- setNullOnUncheck ?: boolean ;
39
- type ?: SerializeType ;
40
- value ?: V ;
41
- } ;
42
-
43
14
export type FormInputValue < T extends object , K extends keyof T > = T [ K ] extends any [ ] ? T [ K ] [ keyof T [ K ] ] : T [ K ] ;
44
15
45
- export function defaultSerializer < T > ( currentValue : T , props : SerializeProps < T > ) : boolean | string {
46
- switch ( props . type ) {
47
- case "datetime-local" :
48
- case "date" : {
49
- let dateValue = currentValue as any ;
50
- if ( typeof dateValue === "string" ) {
51
- let ni = parseInt ( dateValue ) ;
52
- if ( ! isNaN ( ni ) ) dateValue = ni ;
53
- }
54
- let date = new Date ( dateValue ) ;
55
- if ( ! isNaN ( date . getTime ( ) ) ) {
56
- return date ?. toISOString ( ) . split ( "T" ) [ 0 ] ?? "" ;
57
- } else {
58
- return "" ;
59
- }
60
- break ;
61
- }
62
- case "radio" : {
63
- return currentValue === props . value ;
64
- }
65
- case "checkbox" : {
66
- if ( props . setNullOnUncheck ) {
67
- return currentValue !== null ;
68
- } else if ( props . setUndefinedOnUncheck ) {
69
- return currentValue !== undefined ;
70
- } else if ( props . value !== undefined ) {
71
- return ( Array . isArray ( currentValue ) ? currentValue : [ ] ) . includes ( props . value as never ) ;
72
- } else {
73
- return ! ! currentValue ;
74
- }
75
- }
76
- default : {
77
- return ( currentValue ?? "" ) + "" ;
78
- }
79
- }
80
- }
81
-
82
- export function defaultDeserializer < T > ( inputValue : string , inputChecked : boolean , currentValue : T , props : SerializeProps < T > ) {
83
- switch ( props . type ) {
84
- case "number" : {
85
- return parseFloat ( inputValue ) as any ;
86
- }
87
- case "datetime-local" :
88
- case "date" : {
89
- if ( inputValue ) {
90
- let d = new Date ( inputValue ) ;
91
- return ( props . dateAsNumber ? d . getTime ( ) : d ) as any ;
92
- } else {
93
- return null as any ;
94
- }
95
- }
96
- case "radio" : {
97
- // Enum field
98
- if ( inputChecked ) {
99
- return props . value as any ;
100
- }
101
- return currentValue ;
102
- }
103
- case "checkbox" : {
104
- if ( props . setNullOnUncheck || props . setUndefinedOnUncheck ) {
105
- if ( inputChecked && props . value === undefined && process . env . NODE_ENV === "development" ) {
106
- console . error (
107
- "Checkbox using setNullOnUncheck got checked but a value to set was not found, please provide a value to the value prop."
108
- ) ;
109
- }
110
- return inputChecked ? props . value : ( ( props . setNullOnUncheck ? null : undefined ) as any ) ;
111
- } else if ( props . value !== undefined ) {
112
- // Primitive array field
113
- let arr = Array . isArray ( currentValue ) ? [ ...currentValue ] : [ ] ;
114
- if ( inputChecked ) arr . push ( props . value ) ;
115
- else arr . splice ( arr . indexOf ( props . value ) , 1 ) ;
116
- return arr as any ;
117
- } else {
118
- // Boolean field
119
- return inputChecked as any ;
120
- }
121
- }
122
- default : {
123
- // String field
124
- return inputValue as any ;
125
- }
126
- }
127
- }
128
-
129
16
export type InputProps = Omit < InputHTMLAttributes < HTMLInputElement > , "name" | "form" | "value" | "type" > ;
130
17
131
18
export type FormInputProps < T extends object , K extends keyof T = keyof T , State = DefaultState , Error extends string = string > = InputProps & {
0 commit comments