Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

chore: improve the new schema form accessibility (#46201)

Gildas Garcia2a3db2f4f0fd
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/SchemaEditor.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/SchemaEditor.tsx
index bcd6e2af1d7..f9cb5829531 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/SchemaEditor.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/SchemaEditor.tsx
@@ -1,7 +1,23 @@
-import { useEffect, useState } from 'react'
+import { zodResolver } from '@hookform/resolvers/zod'
+import { useEffect } from 'react'
+import { SubmitHandler, useForm } from 'react-hook-form'
import { toast } from 'sonner'
-import { Input, SidePanel } from 'ui'
+import {
+ Button,
+ Dialog,
+ DialogContent,
+ DialogFooter,
+ DialogHeader,
+ DialogSection,
+ DialogSectionSeparator,
+ DialogTitle,
+ Form,
+ FormControl,
+ FormField,
+ Input,
+} from 'ui'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
+import * as z from 'zod'
import { useSchemaCreateMutation } from '@/data/database/schema-create-mutation'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
@@ -12,73 +28,95 @@ interface SchemaEditorProps {
closePanel: () => void
}
+const formSchema = z.object({
+ name: z.string().min(1, 'Please provide a name for your schema'),
+})
+
+type FormSchema = z.infer<typeof formSchema>
+
export const SchemaEditor = ({ visible, onSuccess, closePanel }: SchemaEditorProps) => {
const { data: project } = useSelectedProjectQuery()
- const [errors, setErrors] = useState<{ name?: string }>({ name: undefined })
- const [name, setName] = useState('')
-
const { mutateAsync: createSchema, isPending } = useSchemaCreateMutation()
- useEffect(() => {
- if (visible) {
- setName('')
- setErrors({ name: undefined })
- }
- }, [visible])
-
- const onSaveChanges = async () => {
- const errors: any = {}
- if (name.length === 0) errors.name = 'Please provide a name for your schema'
- if (Object.keys(errors).length > 0) {
- return setErrors(errors)
- }
-
+ const onSubmit: SubmitHandler<FormSchema> = async (values) => {
if (project === undefined) return console.error('Project is required')
try {
await createSchema({
projectRef: project.ref,
connectionString: project.connectionString,
- name,
+ name: values.name,
})
- onSuccess(name)
- toast.success(`Successfully created schema "${name}"`)
+ onSuccess(values.name)
+ toast.success(`Successfully created schema "${values.name}"`)
} catch (error) {
toast.error(`Failed to create schema: ${error}`)
}
}
+ const form = useForm<FormSchema>({
+ resolver: zodResolver(formSchema),
+ defaultValues: {
+ name: '',
+ },
+ })
+
+ const { reset } = form
+
+ useEffect(() => {
+ if (visible) {
+ reset()
+ }
+ }, [reset, visible])
+
+ const formId = 'schema-form'
+
return (
- <SidePanel
- size="medium"
- key="SchemaEditor"
- visible={visible}
- header={'Create a new schema'}
- className="transition-all duration-100 ease-in"
- onCancel={closePanel}
- onConfirm={onSaveChanges}
- loading={isPending}
- cancelText="Cancel"
- confirmText="Save"
- >
- <>
- <SidePanel.Content>
- <div className="space-y-10 py-6">
- <FormItemLayout
- isReactForm={false}
- layout="vertical"
- label="Schema name"
- error={errors?.name}
- >
- <Input
- type="text"
- value={name}
- onChange={(event: any) => setName(event.target.value)}
+ <Dialog open={visible} onOpenChange={closePanel}>
+ <DialogContent className="sm:max-w-[425px]">
+ <DialogHeader>
+ <DialogTitle>Create a new schema</DialogTitle>
+ </DialogHeader>
+ <DialogSectionSeparator />
+ <DialogSection>
+ <Form {...form}>
+ <form id={formId} onSubmit={form.handleSubmit(onSubmit)} className="grow px-0">
+ <FormField
+ control={form.control}
+ name="name"
+ render={({ field }) => (
+ <FormItemLayout layout="vertical" label="Schema name">
+ <FormControl>
+ <Input {...field} />
+ </FormControl>
+ </FormItemLayout>
+ )}
/>
- </FormItemLayout>
- </div>
- </SidePanel.Content>
- </>
- </SidePanel>
+ </form>
+ </Form>
+ </DialogSection>
+ <DialogFooter>
+ <Button
+ type="default"
+ onClick={() => {
+ form.reset()
+ closePanel()
+ }}
+ disabled={isPending}
+ >
+ Cancel
+ </Button>
+ <Button
+ type="primary"
+ form={formId}
+ htmlType="submit"
+ loading={isPending}
+ disabled={isPending}
+ >
+ Save
+ </Button>
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
)
}