fix(striker-ui): add function to set form validity by regex

main
Tsu-ba-me 2 years ago
parent 239138a8b1
commit 4557e27e1b
  1. 23
      striker-ui/hooks/useFormUtils.ts
  2. 3
      striker-ui/types/FormUtils.d.ts

@ -16,12 +16,32 @@ const useFormUtils = <
): FormUtils<M> => {
const [formValidity, setFormValidity] = useState<FormValidity<M>>({});
const setValidity = useCallback((key: keyof M, value: boolean) => {
const setValidity = useCallback((key: keyof M, value?: boolean) => {
setFormValidity(
buildObjectStateSetterCallback<FormValidity<M>>(key, value),
);
}, []);
const setValidityRe = useCallback((re: RegExp, value?: boolean) => {
setFormValidity((previous) => {
const result: FormValidity<M> = {};
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 buildFinishInputTestBatchFunction = useCallback(
(key: keyof M) => (result: boolean) => {
setValidity(key, result);
@ -66,6 +86,7 @@ const useFormUtils = <
setFormValidity,
setMsgSetter,
setValidity,
setValidityRe,
};
};

@ -28,5 +28,6 @@ type FormUtils<M extends MapToInputTestID> = {
setter?: MessageSetterFunction,
isOverwrite?: boolean,
) => void;
setValidity: (key: keyof M, value: boolean) => void;
setValidity: (key: keyof M, value?: boolean) => void;
setValidityRe: (re: RegExp, value?: boolean) => void;
};

Loading…
Cancel
Save