mirror of
https://github.com/supabase/supabase.git
synced 2026-09-09 11:30:17 +08:00
## 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>
110 lines
3.0 KiB
TypeScript
110 lines
3.0 KiB
TypeScript
import { zodResolver } from '@hookform/resolvers/zod'
|
|
import { useEffect } from 'react'
|
|
import { SubmitHandler, useForm } from 'react-hook-form'
|
|
import {
|
|
Button,
|
|
Dialog,
|
|
DialogContent,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogSection,
|
|
DialogSectionSeparator,
|
|
DialogTitle,
|
|
Form,
|
|
FormControl,
|
|
FormField,
|
|
Input,
|
|
Textarea,
|
|
} from 'ui'
|
|
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
|
|
import * as z from 'zod'
|
|
|
|
const formSchema = z.object({
|
|
name: z.string().min(1, 'Required'),
|
|
description: z.string().optional(),
|
|
})
|
|
|
|
type SavedQuery = z.infer<typeof formSchema>
|
|
|
|
export interface UpdateSavedQueryProps {
|
|
header: string
|
|
visible: boolean
|
|
onCancel: () => void
|
|
onSubmit: SubmitHandler<SavedQuery>
|
|
initialValues: SavedQuery
|
|
}
|
|
|
|
export const UpdateSavedQueryModal = ({
|
|
header,
|
|
visible,
|
|
onCancel,
|
|
onSubmit,
|
|
initialValues,
|
|
}: UpdateSavedQueryProps) => {
|
|
const form = useForm<SavedQuery>({
|
|
resolver: zodResolver(formSchema),
|
|
defaultValues: { ...initialValues, description: initialValues.description ?? '' },
|
|
})
|
|
const { reset, formState } = form
|
|
const { isDirty, isSubmitting } = formState
|
|
|
|
useEffect(() => {
|
|
if (isDirty) return
|
|
reset({ ...initialValues, description: initialValues.description ?? '' })
|
|
}, [isDirty, initialValues, reset])
|
|
|
|
const handleCancel = () => {
|
|
form.reset()
|
|
onCancel()
|
|
}
|
|
|
|
return (
|
|
<Dialog open={visible} onOpenChange={handleCancel}>
|
|
<DialogContent size="medium">
|
|
<DialogHeader>
|
|
<DialogTitle>{header}</DialogTitle>
|
|
</DialogHeader>
|
|
<DialogSectionSeparator />
|
|
<Form {...form}>
|
|
<form onSubmit={form.handleSubmit(onSubmit)} noValidate>
|
|
<DialogSection>
|
|
<FormField
|
|
control={form.control}
|
|
name="name"
|
|
render={({ field }) => (
|
|
<FormItemLayout layout="vertical" label="Name">
|
|
<FormControl>
|
|
<Input {...field} placeholder="Enter text" />
|
|
</FormControl>
|
|
</FormItemLayout>
|
|
)}
|
|
/>
|
|
</DialogSection>
|
|
<DialogSection>
|
|
<FormField
|
|
control={form.control}
|
|
name="description"
|
|
render={({ field }) => (
|
|
<FormItemLayout layout="vertical" label="Description">
|
|
<FormControl>
|
|
<Textarea {...field} placeholder="Describe query" className="resize-none" />
|
|
</FormControl>
|
|
</FormItemLayout>
|
|
)}
|
|
/>
|
|
</DialogSection>
|
|
<DialogFooter>
|
|
<Button type="reset" variant="default" onClick={handleCancel} disabled={isSubmitting}>
|
|
Cancel
|
|
</Button>
|
|
<Button type="submit" loading={isSubmitting} disabled={isSubmitting || !isDirty}>
|
|
Save query
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</Form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|