|
|
@ -1,11 +1,9 @@ |
|
|
|
import { MutableRefObject, useCallback, useMemo, useState } from 'react'; |
|
|
|
import { MutableRefObject, useCallback, useMemo, useState } from 'react'; |
|
|
|
|
|
|
|
|
|
|
|
import buildMapToMessageSetter, { |
|
|
|
|
|
|
|
buildMessageSetter, |
|
|
|
|
|
|
|
} from '../lib/buildMapToMessageSetter'; |
|
|
|
|
|
|
|
import buildObjectStateSetterCallback, { |
|
|
|
import buildObjectStateSetterCallback, { |
|
|
|
buildProtectedObjectStateSetterCallback, |
|
|
|
buildRegExpObjectStateSetterCallback, |
|
|
|
} from '../lib/buildObjectStateSetterCallback'; |
|
|
|
} from '../lib/buildObjectStateSetterCallback'; |
|
|
|
|
|
|
|
import { Message } from '../components/MessageBox'; |
|
|
|
import { MessageGroupForwardedRefContent } from '../components/MessageGroup'; |
|
|
|
import { MessageGroupForwardedRefContent } from '../components/MessageGroup'; |
|
|
|
|
|
|
|
|
|
|
|
const useFormUtils = < |
|
|
|
const useFormUtils = < |
|
|
@ -17,8 +15,19 @@ const useFormUtils = < |
|
|
|
messageGroupRef: MutableRefObject<MessageGroupForwardedRefContent>, |
|
|
|
messageGroupRef: MutableRefObject<MessageGroupForwardedRefContent>, |
|
|
|
): FormUtils<M> => { |
|
|
|
): FormUtils<M> => { |
|
|
|
const [formValidity, setFormValidity] = useState<FormValidity<M>>({}); |
|
|
|
const [formValidity, setFormValidity] = useState<FormValidity<M>>({}); |
|
|
|
const [msgSetterList, setMsgSetterList] = useState<MapToMessageSetter<M>>( |
|
|
|
|
|
|
|
() => buildMapToMessageSetter<U, I, M>(ids, messageGroupRef), |
|
|
|
const setMessage = useCallback( |
|
|
|
|
|
|
|
(key: keyof M, message?: Message) => { |
|
|
|
|
|
|
|
messageGroupRef.current.setMessage?.call(null, String(key), message); |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
[messageGroupRef], |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const setMessageRe = useCallback( |
|
|
|
|
|
|
|
(re: RegExp, message?: Message) => { |
|
|
|
|
|
|
|
messageGroupRef.current.setMessageRe?.call(null, re, message); |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
[messageGroupRef], |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const setValidity = useCallback((key: keyof M, value?: boolean) => { |
|
|
|
const setValidity = useCallback((key: keyof M, value?: boolean) => { |
|
|
@ -28,54 +37,25 @@ const useFormUtils = < |
|
|
|
}, []); |
|
|
|
}, []); |
|
|
|
|
|
|
|
|
|
|
|
const setValidityRe = useCallback((re: RegExp, value?: boolean) => { |
|
|
|
const setValidityRe = useCallback((re: RegExp, value?: boolean) => { |
|
|
|
setFormValidity((previous) => { |
|
|
|
setFormValidity( |
|
|
|
const result: FormValidity<M> = {}; |
|
|
|
buildRegExpObjectStateSetterCallback<FormValidity<M>>(re, value), |
|
|
|
|
|
|
|
); |
|
|
|
Object.keys(previous).forEach((key) => { |
|
|
|
|
|
|
|
const id = key as keyof M; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (re.test(key)) { |
|
|
|
|
|
|
|
if (value !== undefined) { |
|
|
|
|
|
|
|
result[id] = value; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
result[id] = previous[id]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}, []); |
|
|
|
}, []); |
|
|
|
|
|
|
|
|
|
|
|
const setMsgSetter = useCallback( |
|
|
|
const unsetKey = useCallback( |
|
|
|
( |
|
|
|
(key: keyof M) => { |
|
|
|
id: keyof M, |
|
|
|
setMessage(key); |
|
|
|
setter?: MessageSetter, |
|
|
|
setValidity(key); |
|
|
|
{ |
|
|
|
}, |
|
|
|
isOverwrite, |
|
|
|
[setMessage, setValidity], |
|
|
|
isUseFallback = true, |
|
|
|
); |
|
|
|
}: { isOverwrite?: boolean; isUseFallback?: boolean } = {}, |
|
|
|
|
|
|
|
) => { |
|
|
|
|
|
|
|
const fallbackSetter: ObjectStatePropSetter<MapToMessageSetter<M>> = ( |
|
|
|
|
|
|
|
result, |
|
|
|
|
|
|
|
key, |
|
|
|
|
|
|
|
value = buildMessageSetter<M>(String(id), messageGroupRef), |
|
|
|
|
|
|
|
) => { |
|
|
|
|
|
|
|
result[key] = value; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setMsgSetterList( |
|
|
|
const unsetKeyRe = useCallback( |
|
|
|
buildProtectedObjectStateSetterCallback<MapToMessageSetter<M>>( |
|
|
|
(re: RegExp) => { |
|
|
|
id, |
|
|
|
setMessageRe(re); |
|
|
|
setter, |
|
|
|
setValidityRe(re); |
|
|
|
{ |
|
|
|
|
|
|
|
isOverwrite, |
|
|
|
|
|
|
|
set: isUseFallback ? fallbackSetter : undefined, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
}, |
|
|
|
}, |
|
|
|
[messageGroupRef], |
|
|
|
[setMessageRe, setValidityRe], |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const buildFinishInputTestBatchFunction = useCallback( |
|
|
|
const buildFinishInputTestBatchFunction = useCallback( |
|
|
@ -88,10 +68,9 @@ const useFormUtils = < |
|
|
|
const buildInputFirstRenderFunction = useCallback( |
|
|
|
const buildInputFirstRenderFunction = useCallback( |
|
|
|
(key: keyof M) => |
|
|
|
(key: keyof M) => |
|
|
|
({ isValid }: InputFirstRenderFunctionArgs) => { |
|
|
|
({ isValid }: InputFirstRenderFunctionArgs) => { |
|
|
|
setMsgSetter(key); |
|
|
|
|
|
|
|
setValidity(key, isValid); |
|
|
|
setValidity(key, isValid); |
|
|
|
}, |
|
|
|
}, |
|
|
|
[setMsgSetter, setValidity], |
|
|
|
[setValidity], |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const isFormInvalid = useMemo( |
|
|
|
const isFormInvalid = useMemo( |
|
|
@ -104,11 +83,13 @@ const useFormUtils = < |
|
|
|
buildInputFirstRenderFunction, |
|
|
|
buildInputFirstRenderFunction, |
|
|
|
formValidity, |
|
|
|
formValidity, |
|
|
|
isFormInvalid, |
|
|
|
isFormInvalid, |
|
|
|
msgSetters: msgSetterList, |
|
|
|
|
|
|
|
setFormValidity, |
|
|
|
setFormValidity, |
|
|
|
setMsgSetter, |
|
|
|
setMessage, |
|
|
|
|
|
|
|
setMessageRe, |
|
|
|
setValidity, |
|
|
|
setValidity, |
|
|
|
setValidityRe, |
|
|
|
setValidityRe, |
|
|
|
|
|
|
|
unsetKey, |
|
|
|
|
|
|
|
unsetKeyRe, |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|