Files
supabase/apps/design-system/registry/default/example/form-item-layout-with-select.tsx
Gildas GarciaandIvan Vasilov 96d43099bb chore: refactor Button API so that it can be used a standard button (#46880)
## Problem

Our `<Button>` component breaks the default `button` contract by
redefining the `type` prop to set its variant (`primary`, `default`,
etc) instead of the button type (`submit`, `button`, etc).
This is confusing and forces to write more code when using it with
shadcn components that expect/inject the standard button props.

## Solution

- rename the `type` prop to `variant`
- rename the `htmlType` prop to `type`
- propagate the changes where necessary
- format code

## How to test

As this is just prop renaming, if it builds it's ok

---------

Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2026-06-16 23:59:58 +02:00

72 lines
2.1 KiB
TypeScript

import { zodResolver } from '@hookform/resolvers/zod'
import { useForm } from 'react-hook-form'
import {
Button,
Form,
FormControl,
FormField,
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from 'ui'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import { z } from 'zod'
const FormSchema = z.object({
email: z
.string({
required_error: 'Please select an email to display.',
})
.email(),
})
export default function FormItemLayoutDemo() {
// 1. Define your form.
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
})
// 2. Define a submit handler.
function onSubmit(values: z.infer<typeof FormSchema>) {
// Do something with the form values.
// ✅ This will be type-safe and validated.
console.log(values)
// action('form form.handleSubmit(onSubmit)')(values)
}
return (
<Form {...form}>
<form className="w-96 flex flex-col gap-3" onSubmit={form.handleSubmit(onSubmit)}>
<FormField
name="email"
control={form.control}
render={({ field }) => (
<FormItemLayout
label="Choose email"
description="Choose preferred email"
labelOptional="Optional"
>
<Select onValueChange={field.onChange} defaultValue={field.value}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select a verified email to display" />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="m@example.com">m@example.com</SelectItem>
<SelectItem value="m@google.com">m@google.com</SelectItem>
<SelectItem value="m@support.com">m@support.com</SelectItem>
</SelectContent>
</Select>
</FormItemLayout>
)}
/>
<Button size="small" variant="secondary" type="submit">
Submit
</Button>
</form>
</Form>
)
}