1
1
import { useRef } from 'react' ;
2
2
import { parseInputArgs } from './parseInputArgs' ;
3
3
import { useInputId } from './useInputId' ;
4
- import { useCache } from './useCache ' ;
4
+ import { useMap , useReferencedCallback , useWarnOnce } from './utils-hooks ' ;
5
5
import { useState } from './useState' ;
6
6
import {
7
7
noop ,
@@ -36,16 +36,9 @@ export default function useFormState(initialState, options) {
36
36
37
37
const formState = useState ( { initialState } ) ;
38
38
const { getIdProp } = useInputId ( formOptions . withIds ) ;
39
- const { set : setDirty , get : isDirty } = useCache ( ) ;
40
- const callbacks = useCache ( ) ;
41
- const devWarnings = useCache ( ) ;
42
-
43
- function warn ( key , type , message ) {
44
- if ( ! devWarnings . has ( `${ type } :${ key } ` ) ) {
45
- devWarnings . set ( `${ type } :${ key } ` , true ) ;
46
- console . warn ( '[useFormState]' , message ) ;
47
- }
48
- }
39
+ const { set : setDirty , get : isDirty } = useMap ( ) ;
40
+ const referencedCallback = useReferencedCallback ( ) ;
41
+ const warn = useWarnOnce ( ) ;
49
42
50
43
const createPropsGetter = type => ( ...args ) => {
51
44
const inputOptions = parseInputArgs ( args ) ;
@@ -69,8 +62,7 @@ export default function useFormState(initialState, options) {
69
62
if ( __DEV__ ) {
70
63
if ( isRaw ) {
71
64
warn (
72
- key ,
73
- 'missingInitialValue' ,
65
+ `missingInitialValue.${ key } ` ,
74
66
`The initial value for input "${ name } " is missing. Custom inputs ` +
75
67
'controlled with raw() are expected to have an initial value ' +
76
68
'provided to useFormState(). To prevent React from treating ' +
@@ -124,8 +116,7 @@ export default function useFormState(initialState, options) {
124
116
if ( __DEV__ ) {
125
117
if ( isRaw && ! [ value , other ] . every ( testIsEqualCompatibility ) ) {
126
118
warn (
127
- key ,
128
- 'missingCompare' ,
119
+ `missingCompare.${ key } ` ,
129
120
`You used a raw input type for "${ name } " without providing a ` +
130
121
'custom compare method. As a result, the pristine value of ' +
131
122
'this input will be calculated using strict equality check ' +
@@ -164,8 +155,7 @@ export default function useFormState(initialState, options) {
164
155
error = e . target . validationMessage ;
165
156
} else if ( __DEV__ ) {
166
157
warn (
167
- key ,
168
- 'missingValidate' ,
158
+ `missingValidate.${ key } ` ,
169
159
`You used a raw input type for "${ name } " without providing a ` +
170
160
'custom validate method. As a result, validation of this input ' +
171
161
'will be set to "true" automatically. If you need to validate ' +
@@ -243,7 +233,7 @@ export default function useFormState(initialState, options) {
243
233
244
234
return hasValueInState ? formState . current . values [ name ] : '' ;
245
235
} ,
246
- onChange : callbacks . getOrSet ( `onChange.${ key } ` , e => {
236
+ onChange : referencedCallback ( `onChange.${ key } ` , e => {
247
237
setDirty ( name , true ) ;
248
238
let value ;
249
239
if ( isRaw ) {
@@ -256,8 +246,7 @@ export default function useFormState(initialState, options) {
256
246
/* istanbul ignore else */
257
247
if ( __DEV__ ) {
258
248
warn (
259
- key ,
260
- 'onChangeUndefined' ,
249
+ `onChangeUndefined.${ key } ` ,
261
250
`You used a raw input type for "${ name } " with an onChange() ` +
262
251
'option without returning a value. The onChange callback ' +
263
252
'of raw inputs, when provided, is used to determine the ' +
@@ -296,7 +285,7 @@ export default function useFormState(initialState, options) {
296
285
297
286
formState . setValues ( partialNewState ) ;
298
287
} ) ,
299
- onBlur : callbacks . getOrSet ( `onBlur.${ key } ` , e => {
288
+ onBlur : referencedCallback ( `onBlur.${ key } ` , e => {
300
289
touch ( e ) ;
301
290
302
291
inputOptions . onBlur ( e ) ;
0 commit comments