-
-
Notifications
You must be signed in to change notification settings - Fork 772
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Innei <i@innei.in>
- Loading branch information
Showing
4 changed files
with
155 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { useCallback, useEffect } from 'react' | ||
import { produce } from 'immer' | ||
import { useAtomValue, useStore } from 'jotai' | ||
import { selectAtom } from 'jotai/utils' | ||
import type { Field } from './types' | ||
|
||
import { useForm } from './FormContext' | ||
|
||
const useAssetFormContext = () => { | ||
const FormCtx = useForm() | ||
if (!FormCtx) throw new Error('FormInput must be used inside <FormContext />') | ||
return FormCtx | ||
} | ||
export const useFormErrorMessage = (name: string) => { | ||
const FormCtx = useAssetFormContext() | ||
|
||
const { fields } = FormCtx | ||
|
||
return useAtomValue( | ||
selectAtom( | ||
fields, | ||
useCallback( | ||
(atomValue) => { | ||
if (!name) return | ||
return atomValue[name]?.rules.find((rule) => rule.status === 'error') | ||
?.message | ||
}, | ||
[name], | ||
), | ||
), | ||
) | ||
} | ||
|
||
export const useAddField = ({ | ||
rules, | ||
transform, | ||
$ref, | ||
name, | ||
}: Field & { name: string }) => { | ||
const FormCtx = useAssetFormContext() | ||
|
||
const { addField, removeField } = FormCtx | ||
useEffect(() => { | ||
if (!rules) return | ||
if (!name) return | ||
|
||
addField(name, { | ||
rules, | ||
$ref, | ||
transform, | ||
}) | ||
|
||
return () => { | ||
removeField(name) | ||
} | ||
}, [$ref, addField, name, removeField, rules, transform]) | ||
} | ||
|
||
export const useResetFieldStatus = (name: string) => { | ||
const jotaiStore = useStore() | ||
const FormCtx = useAssetFormContext() | ||
const { fields } = FormCtx | ||
return useCallback(() => { | ||
jotaiStore.set(fields, (p) => { | ||
return produce(p, (draft) => { | ||
if (!name) return | ||
draft[name].rules.forEach((rule) => { | ||
if (rule.status === 'error') rule.status = 'success' | ||
}) | ||
}) | ||
}) | ||
}, [fields, jotaiStore, name]) | ||
} | ||
|
||
export const useCheckFieldStatus = (name: string) => { | ||
const jotaiStore = useStore() | ||
const FormCtx = useAssetFormContext() | ||
const { fields } = FormCtx | ||
return useCallback(() => { | ||
jotaiStore.set(fields, (p) => { | ||
return produce(p, (draft) => { | ||
if (!name) return | ||
const value = draft[name].$ref?.value | ||
if (!value) return | ||
draft[name].rules.some((rule) => { | ||
const result = rule.validator(value) | ||
if (!result) { | ||
rule.status = 'error' | ||
|
||
return true | ||
} | ||
}) | ||
}) | ||
}) | ||
}, [fields, jotaiStore, name]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters