Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useFieldProps): enable passing initialValue via options #82

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/hooks/use-field-props/FieldProps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { UseFieldOptions } from "form-atoms";
import { ReactNode } from "react";

import type { ZodField, ZodFieldValue } from "../../fields";

/**
* Common Props to be used on FieldComponents like CheckboxField or NumberField.
*/
export type FieldProps<Field extends ZodField> = {
field: Field;
label?: ReactNode;
required?: boolean;
} & Pick<UseFieldOptions<ZodFieldValue<Field>>, "initialValue">;
1 change: 1 addition & 0 deletions src/hooks/use-field-props/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./useFieldProps";
export * from "./FieldProps";
16 changes: 5 additions & 11 deletions src/hooks/use-field-props/useFieldProps.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { useField } from "form-atoms";
import { UseFieldOptions, useField } from "form-atoms";
import { useAtomValue, useSetAtom } from "jotai";
import { ChangeEvent, ReactNode, useMemo, useTransition } from "react";
import { ChangeEvent, useMemo, useTransition } from "react";

import { ZodField, ZodFieldValue } from "../../fields/zod-field/zodField";

export type FieldProps<Field extends ZodField<any>> = {
field: Field;
label?: ReactNode;
helperText?: ReactNode;
required?: boolean;
};
import { ZodField, ZodFieldValue } from "../../fields";

export function useFieldProps<
Field extends ZodField,
Expand All @@ -23,8 +16,9 @@ export function useFieldProps<
value: ZodFieldValue<Field>,
) => ZodFieldValue<Field>
: never,
options?: UseFieldOptions<ZodFieldValue<Field>>,
) {
const { actions, state } = useField<ZodFieldValue<Field>>(fieldAtom);
const { actions, state } = useField<ZodFieldValue<Field>>(fieldAtom, options);
const field = useAtomValue(fieldAtom);
const name = useAtomValue(field.name);
const required = useAtomValue(field.required);
Expand Down