diff --git a/.changeset/spotty-cooks-unite.md b/.changeset/spotty-cooks-unite.md new file mode 100644 index 000000000..cc7b0b1b1 --- /dev/null +++ b/.changeset/spotty-cooks-unite.md @@ -0,0 +1,6 @@ +--- +'@saas-ui/forms': minor +'@saas-ui/react': minor +--- + +Improved ObjectSchema type definitions to be more strict and inherit correct field type props diff --git a/apps/website/src/pages/docs/components/forms/auto-form/usage.mdx b/apps/website/src/pages/docs/components/forms/auto-form/usage.mdx index e56adc63d..ded36e8e1 100644 --- a/apps/website/src/pages/docs/components/forms/auto-form/usage.mdx +++ b/apps/website/src/pages/docs/components/forms/auto-form/usage.mdx @@ -34,31 +34,8 @@ The default Form component supports the following schema format. The object schema is an object with field props. Array and object fields can have nested fields. -```ts -type FieldTypes = - | 'array' - | 'object' - | 'text' - | 'textarea' - | 'select' - | 'number' - | 'checkbox' - | 'radio' - | 'pin' - | 'phone' - | 'url' - | 'email' - | 'password' - | 'switch' - | 'native-select' - -type ObjectSchema = { - [key: FieldTypes]: FieldProps -} -``` - ```jsx -import { Form } from '@saas-ui/react' +import { Form, ObjectSchema } from '@saas-ui/react' const schema = { title: { @@ -71,7 +48,7 @@ const schema = { label: 'Body', type: 'textarea', }, -} +} as const satisfies ObjectSchema export default function Task() { return ( @@ -90,7 +67,7 @@ export default function Task() { ### Customize submit button ```jsx -import { Form } from '@saas-ui/react' +import { Form, ObjectSchema } from '@saas-ui/react' const schema = { title: { @@ -103,7 +80,7 @@ const schema = { label: 'Body', type: 'textarea', }, -} +} as const satisfies ObjectSchema export default function Post() { return ( @@ -129,7 +106,7 @@ export default function Post() { ### Object field ```jsx -import { Form } from '@saas-ui/react' +import { Form, ObjectSchema } from '@saas-ui/react' const schema = { title: { @@ -154,7 +131,7 @@ const schema = { }, }, }, -} +} as const satisfies ObjectSchema export default function Post() { return ( @@ -184,7 +161,7 @@ export default function Post() { ### Array field ```jsx -import { Form } from '@saas-ui/react' +import { Form, ObjectSchema } from '@saas-ui/react' const schema = { title: { @@ -209,7 +186,7 @@ const schema = { }, }, }, -} +} as const satisfies ObjectSchema export default function Post() { return ( diff --git a/package.json b/package.json index 2efcb9cfc..dcbbb6d8d 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "clean": "turbo run clean", "test": "vitest", "test:watch": "vitest --watch --onlyChanged", - "test:ci": "vitest --color --runInBand --ci", + "test:ci": "vitest --color --run", "lint": "turbo run lint", "lint:staged": "lint-staged --allow-empty --config lint-staged.config.js", "create:pkg": "hygen package chakra-component", diff --git a/packages/saas-ui-forms/src/auto-form.tsx b/packages/saas-ui-forms/src/auto-form.tsx index 602c934de..4e5312c77 100644 --- a/packages/saas-ui-forms/src/auto-form.tsx +++ b/packages/saas-ui-forms/src/auto-form.tsx @@ -27,8 +27,11 @@ interface AutoFormOptions { export interface AutoFormProps< TFieldValues extends FieldValues, - TContext extends object = object -> extends Omit, 'schema' | 'children'>, + TContext extends object = object, +> extends Omit< + FormProps, + 'schema' | 'children' | 'fieldResolver' + >, AutoFormOptions { children?: React.ReactNode } @@ -40,7 +43,7 @@ export interface AutoFormProps< export const AutoForm = forwardRef( < TFieldValues extends FieldValues = FieldValues, - TContext extends object = object + TContext extends object = object, >( props: AutoFormProps, ref: React.ForwardedRef diff --git a/packages/saas-ui-forms/src/default-fields.tsx b/packages/saas-ui-forms/src/default-fields.tsx index c3dee5113..0b494c392 100644 --- a/packages/saas-ui-forms/src/default-fields.tsx +++ b/packages/saas-ui-forms/src/default-fields.tsx @@ -31,9 +31,11 @@ import { NativeSelectProps, SelectButtonProps, SelectListProps, + SelectOption, } from './select' import { createField } from './create-field' +import { FieldOption, FieldOptions } from './types' export interface InputFieldProps extends InputProps { type?: string @@ -95,6 +97,7 @@ export interface SelectFieldProps extends SelectProps { export const SelectField = createField( forwardRef((props, ref) => { const { buttonProps, listProps, ...rest } = props + return (