Skip to content
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
51 changes: 42 additions & 9 deletions app/components/hook-form/SideModalForm.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
import type { FormEventHandler, ReactNode } from 'react'
import type { ReactNode } from 'react'
import { useEffect } from 'react'
import type { Control, FieldValues, UseFormProps } from 'react-hook-form'
import { useForm } from 'react-hook-form'

import { Error12Icon } from '@oxide/ui'
import { Button, SideModal } from '@oxide/ui'

type SideModalFormProps = {
type SideModalFormProps<TFieldValues extends FieldValues> = {
id: string
children: ReactNode
formOptions: UseFormProps<TFieldValues>
/**
* A function that returns the fields.
*
* Implemented as a function so we can pass `control` to the fields in the
* calling code. We could do that internally with `cloneElement` instead, but
* then in the calling code, the field would not infer `TFieldValues` and
* constrain the `name` prop to paths in the values object.
*/
children: (control: Control<TFieldValues>) => ReactNode
isOpen: boolean
onDismiss: () => void
submitDisabled?: boolean
error?: Error
title: string
onSubmit: FormEventHandler<HTMLFormElement>
onSubmit: (values: TFieldValues) => void
submitLabel?: string
}

export function SideModalForm({
export function SideModalForm<TFieldValues extends FieldValues>({
id,
formOptions,
children,
onDismiss,
isOpen,
Expand All @@ -25,25 +38,45 @@ export function SideModalForm({
title,
onSubmit,
submitLabel,
}: SideModalFormProps) {
}: SideModalFormProps<TFieldValues>) {
// TODO: RHF docs warn about the performance impact of validating on every
// change
const {
control,
formState: { isDirty, isValid },
handleSubmit,
reset,
} = useForm({ mode: 'all', ...formOptions })
const canSubmit = isDirty && isValid

// TODO: calling useForm all the way up here means it's always mounted whether
// the side modal is open or not, which means form state hangs around even
// when the modal is closed. Using useEffect like this is a code smell, so I
// would like this to work differently. Ideally useForm would be called one
// level lower, inside SideModal, so it only gets rendered when the form is
// open.
useEffect(() => {
if (!isOpen) reset()
}, [reset, isOpen])

return (
<SideModal onDismiss={onDismiss} isOpen={isOpen} title={title}>
<SideModal.Body>
<form
id={id}
className="ox-form is-side-modal"
autoComplete="off"
onSubmit={onSubmit}
onSubmit={handleSubmit(onSubmit)}
>
{children}
{children(control)}
</form>
</SideModal.Body>
<SideModal.Footer>
<div className="flex w-full items-center gap-[0.625rem] children:shrink-0">
<Button variant="ghost" color="secondary" size="sm" onClick={onDismiss}>
Cancel
</Button>
<Button type="submit" size="sm" disabled={submitDisabled} form={id}>
<Button type="submit" size="sm" disabled={submitDisabled || !canSubmit} form={id}>
{submitLabel || title}
</Button>
{error && (
Expand Down
35 changes: 9 additions & 26 deletions app/forms/org-create.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { useEffect } from 'react'
import { useForm } from 'react-hook-form'
import { useNavigate } from 'react-router-dom'

import type { Organization, OrganizationCreate } from '@oxide/api'
Expand Down Expand Up @@ -45,38 +43,23 @@ export function CreateOrgSideModalForm({
onError,
})

// TODO: we don't want to have manually specify this every time we make a
// form. might have to make a wrapper hook with different defaults
//
// TODO: the docs warn against the performance implications of validating on
// every change
const form = useForm({ defaultValues, mode: 'all' })
const canSubmit = form.formState.isDirty && form.formState.isValid

// TODO: calling useForm all the way up here means it's always mounted whether
// the side modal is open or not, which means form state hangs around even
// when the modal is closed. Using useEffect like this is a code smell, (and
// error-prone, and would have to be wrapped up in a custom useForm in order
// to get consistent correct behavior everywhere) so I would like this to work
// differently. Ideally useForm would be called one level lower, e.g., in a
// component that wraps name and description in the children of SideModalForm
// so it only gets rendered when the form is open.
useEffect(() => {
if (!isOpen) form.reset()
}, [form, isOpen])

return (
<SideModalForm
id="create-org-form"
formOptions={{ defaultValues }}
title={title}
onDismiss={onDismiss}
onSubmit={form.handleSubmit((values) => createOrg.mutate({ body: values }))}
submitDisabled={createOrg.isLoading || !canSubmit}
onSubmit={(values) => createOrg.mutate({ body: values })}
submitDisabled={createOrg.isLoading}
error={createOrg.error?.error as Error | undefined}
isOpen={isOpen}
>
<NameField name="name" control={form.control} />
<DescriptionField name="description" control={form.control} />
{(control) => (
<>
<NameField name="name" control={control} />
<DescriptionField name="description" control={control} />
</>
)}
</SideModalForm>
)
}
28 changes: 10 additions & 18 deletions app/forms/org-edit.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { useEffect } from 'react'
import { useForm } from 'react-hook-form'

import type { Organization, OrganizationCreate } from '@oxide/api'
import { useApiMutation, useApiQueryClient } from '@oxide/api'
import { Success16Icon } from '@oxide/ui'

import { Form } from 'app/components/form'
import { DescriptionField, NameField, SideModalForm } from 'app/components/hook-form'
import type { EditSideModalFormProps } from 'app/components/hook-form'
import { useToast } from 'app/hooks'
Expand Down Expand Up @@ -38,33 +34,29 @@ export function EditOrgSideModalForm({
onError,
})

// see comments in org-create.tsx
const form = useForm({ defaultValues, mode: 'onChange' })
const canSubmit = form.formState.isDirty && form.formState.isValid

useEffect(() => {
if (!isOpen) form.reset()
}, [form, isOpen])

return (
<SideModalForm
id="edit-org-form"
formOptions={{ defaultValues }}
title={title}
onDismiss={onDismiss}
onSubmit={form.handleSubmit(({ name, description }) =>
onSubmit={({ name, description }) =>
updateOrg.mutate({
path: { orgName: defaultValues.name },
body: { name, description },
})
)}
submitDisabled={updateOrg.isLoading || !canSubmit}
}
submitDisabled={updateOrg.isLoading}
error={updateOrg.error?.error as Error | undefined}
submitLabel="Save changes"
isOpen={isOpen}
>
<NameField name="name" control={form.control} />
<DescriptionField name="description" control={form.control} />
<Form.Submit>Save changes</Form.Submit>
{(control) => (
<>
<NameField name="name" control={control} />
<DescriptionField name="description" control={control} />
</>
)}
</SideModalForm>
)
}