|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + * |
| 6 | + * Copyright Oxide Computer Company |
| 7 | + */ |
| 8 | + |
| 9 | +import { useId, type ReactNode } from 'react' |
| 10 | +import type { FieldValues, UseFormReturn } from 'react-hook-form' |
| 11 | + |
| 12 | +import type { ApiError } from '@oxide/api' |
| 13 | + |
| 14 | +import { Message } from '~/ui/lib/Message' |
| 15 | +import { Modal, type ModalProps } from '~/ui/lib/Modal' |
| 16 | + |
| 17 | +type ModalFormProps<TFieldValues extends FieldValues> = { |
| 18 | + form: UseFormReturn<TFieldValues> |
| 19 | + children: ReactNode |
| 20 | + |
| 21 | + /** Must be provided with a reason describing why it's disabled */ |
| 22 | + submitDisabled?: boolean |
| 23 | + onSubmit: (values: TFieldValues) => void |
| 24 | + submitLabel: string |
| 25 | + |
| 26 | + // require loading and error so we can't forget to hook them up. there are a |
| 27 | + // few forms that don't need them, so we'll use dummy values |
| 28 | + |
| 29 | + /** Error from the API call */ |
| 30 | + submitError: ApiError | null |
| 31 | + loading: boolean |
| 32 | +} & Omit<ModalProps, 'isOpen'> |
| 33 | + |
| 34 | +export function ModalForm<TFieldValues extends FieldValues>({ |
| 35 | + form, |
| 36 | + children, |
| 37 | + onDismiss, |
| 38 | + submitDisabled = false, |
| 39 | + submitError, |
| 40 | + title, |
| 41 | + onSubmit, |
| 42 | + submitLabel = 'Save', |
| 43 | + loading, |
| 44 | + width = 'medium', |
| 45 | + overlay = true, |
| 46 | +}: ModalFormProps<TFieldValues>) { |
| 47 | + const id = useId() |
| 48 | + const { isSubmitting } = form.formState |
| 49 | + |
| 50 | + return ( |
| 51 | + <Modal isOpen onDismiss={onDismiss} title={title} width={width} overlay={overlay}> |
| 52 | + <Modal.Body> |
| 53 | + <Modal.Section> |
| 54 | + {submitError && ( |
| 55 | + <Message variant="error" title="Error" content={submitError.message} /> |
| 56 | + )} |
| 57 | + <form |
| 58 | + id={id} |
| 59 | + className="ox-form" |
| 60 | + autoComplete="off" |
| 61 | + onSubmit={(e) => { |
| 62 | + if (!onSubmit) return |
| 63 | + // This modal being in a portal doesn't prevent the submit event |
| 64 | + // from bubbling up out of the portal. Normally that's not a |
| 65 | + // problem, but sometimes (e.g., instance create) we render the |
| 66 | + // SideModalForm from inside another form, in which case submitting |
| 67 | + // the inner form submits the outer form unless we stop propagation |
| 68 | + e.stopPropagation() |
| 69 | + form.handleSubmit(onSubmit)(e) |
| 70 | + }} |
| 71 | + > |
| 72 | + {children} |
| 73 | + </form> |
| 74 | + </Modal.Section> |
| 75 | + </Modal.Body> |
| 76 | + <Modal.Footer |
| 77 | + onDismiss={onDismiss} |
| 78 | + formId={id} |
| 79 | + actionText={submitLabel} |
| 80 | + disabled={submitDisabled} |
| 81 | + actionLoading={loading || isSubmitting} |
| 82 | + /> |
| 83 | + </Modal> |
| 84 | + ) |
| 85 | +} |
0 commit comments