Skip to content

Commit

Permalink
feat: validate field on mount (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
totraev authored Jan 3, 2025
1 parent 257e589 commit 242e540
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-moles-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@babylonlabs-io/bbn-core-ui": patch
---

feat: validate field on mount
3 changes: 2 additions & 1 deletion src/widgets/form/NumberField/NumberField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function NumberField({
prefix,
shouldUnregister,
state,
validateOnMount,
}: NumberFieldProps) {
const {
ref,
Expand All @@ -36,7 +37,7 @@ export function NumberField({
invalid,
onChange,
onBlur,
} = useField({ name, defaultValue, autoFocus, shouldUnregister });
} = useField({ name, defaultValue, autoFocus, shouldUnregister, validateOnMount });

const fieldState = invalid ? "error" : state;
const fieldHint = invalid ? error : hint;
Expand Down
3 changes: 2 additions & 1 deletion src/widgets/form/SelectField/SelectField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function SelectField({
placeholder,
shouldUnregister,
state,
validateOnMount,
onOpen,
onClose,
renderSelectedOption,
Expand All @@ -44,7 +45,7 @@ export function SelectField({
invalid,
onChange,
onBlur,
} = useField({ name, defaultValue, autoFocus, shouldUnregister });
} = useField({ name, defaultValue, autoFocus, shouldUnregister, validateOnMount });

const fieldState = invalid ? "error" : state;
const fieldHint = invalid ? error : hint;
Expand Down
3 changes: 2 additions & 1 deletion src/widgets/form/TextField/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export function TextField({
prefix,
shouldUnregister,
state,
validateOnMount,
}: TextFieldProps) {
const {
ref,
Expand All @@ -34,7 +35,7 @@ export function TextField({
invalid,
onChange,
onBlur,
} = useField({ name, defaultValue, autoFocus, shouldUnregister });
} = useField({ name, defaultValue, autoFocus, shouldUnregister, validateOnMount });

const fieldState = invalid ? "error" : state;
const fieldHint = invalid ? error : hint;
Expand Down
10 changes: 7 additions & 3 deletions src/widgets/form/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface FieldProps<V> {
disabled?: boolean;
autoFocus?: boolean;
shouldUnregister?: boolean;
validateOnMount?: boolean;
}

export function useField<V = string>({
Expand All @@ -15,16 +16,19 @@ export function useField<V = string>({
disabled = false,
autoFocus = false,
shouldUnregister = false,
validateOnMount = true,
}: FieldProps<V>) {
const { setFocus } = useFormContext();
const { setFocus, trigger } = useFormContext();
const { field, fieldState } = useController({ name, defaultValue, disabled, shouldUnregister });
const { invalid, isTouched, error } = fieldState;

useEffect(() => {
if (autoFocus) {
if (validateOnMount) {
trigger(name, { shouldFocus: autoFocus });
} else if (autoFocus) {
setFocus(name);
}
}, [name]);
}, [name, validateOnMount]);

return {
...field,
Expand Down
1 change: 1 addition & 0 deletions src/widgets/form/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export interface FieldProps {
hint?: string | JSX.Element;
shouldUnregister?: boolean;
state?: "default" | "error" | "warning";
validateOnMount?: boolean;
}

0 comments on commit 242e540

Please sign in to comment.