Skip to content

Commit 0469280

Browse files
authored
Make SideModal submit button copy consistent (#2048)
* Make SideModal button copy consistent * Refactor and remove title from most create forms * Add name of user/group to 'change role' form
1 parent 8c30305 commit 0469280

40 files changed

+119
-89
lines changed

app/components/form/SideModalForm.tsx

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Copyright Oxide Computer Company
77
*/
8-
import { useEffect, type ReactNode } from 'react'
8+
import { useEffect, useId, type ReactNode } from 'react'
99
import type { FieldValues, UseFormReturn } from 'react-hook-form'
1010
import { useNavigationType } from 'react-router-dom'
1111

@@ -14,8 +14,19 @@ import type { ApiError } from '@oxide/api'
1414
import { Button } from '~/ui/lib/Button'
1515
import { SideModal } from '~/ui/lib/SideModal'
1616

17+
type CreateFormProps = {
18+
formType: 'create'
19+
/** Only needed if you need to override the default button text (`Create ${resourceName}`) */
20+
submitLabel?: string
21+
}
22+
23+
type EditFormProps = {
24+
formType: 'edit'
25+
/** Not permitted, as all edit form buttons should read `Update ${resourceName}` */
26+
submitLabel?: never
27+
}
28+
1729
type SideModalFormProps<TFieldValues extends FieldValues> = {
18-
id: string
1930
form: UseFormReturn<TFieldValues>
2031
/**
2132
* A function that returns the fields.
@@ -27,16 +38,17 @@ type SideModalFormProps<TFieldValues extends FieldValues> = {
2738
*/
2839
children: ReactNode
2940
onDismiss: () => void
41+
resourceName: string
3042
/** Must be provided with a reason describing why it's disabled */
3143
submitDisabled?: string
3244
/** Error from the API call */
3345
submitError: ApiError | null
3446
loading?: boolean
35-
title: string
47+
/** Only needed if you need to override the default title (Create/Edit ${resourceName}) */
48+
title?: string
3649
subtitle?: ReactNode
3750
onSubmit?: (values: TFieldValues) => void
38-
submitLabel?: string
39-
}
51+
} & (CreateFormProps | EditFormProps)
4052

4153
/**
4254
* Only animate the modal in when we're navigating by a client-side click.
@@ -49,10 +61,11 @@ export function useShouldAnimateModal() {
4961
}
5062

5163
export function SideModalForm<TFieldValues extends FieldValues>({
52-
id,
5364
form,
65+
formType,
5466
children,
5567
onDismiss,
68+
resourceName,
5669
submitDisabled,
5770
submitError,
5871
title,
@@ -61,6 +74,7 @@ export function SideModalForm<TFieldValues extends FieldValues>({
6174
loading,
6275
subtitle,
6376
}: SideModalFormProps<TFieldValues>) {
77+
const id = useId()
6478
const { isSubmitting } = form.formState
6579

6680
useEffect(() => {
@@ -70,11 +84,16 @@ export function SideModalForm<TFieldValues extends FieldValues>({
7084
}
7185
}, [submitError, form])
7286

87+
const label =
88+
formType === 'edit'
89+
? `Update ${resourceName}`
90+
: submitLabel || title || `Create ${resourceName}`
91+
7392
return (
7493
<SideModal
7594
onDismiss={onDismiss}
7695
isOpen
77-
title={title}
96+
title={title || `${formType === 'edit' ? 'Edit' : 'Create'} ${resourceName}`}
7897
animate={useShouldAnimateModal()}
7998
subtitle={subtitle}
8099
errors={submitError ? [submitError.message] : []}
@@ -111,7 +130,7 @@ export function SideModalForm<TFieldValues extends FieldValues>({
111130
loading={loading || isSubmitting}
112131
form={id}
113132
>
114-
{submitLabel || title}
133+
{label}
115134
</Button>
116135
)}
117136
</SideModal.Footer>

app/forms/access-util.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export type AddRoleModalProps = {
5050
}
5151

5252
export type EditRoleModalProps = AddRoleModalProps & {
53+
name?: string
5354
identityId: string
5455
identityType: IdentityType
5556
defaultValues: { roleName: RoleKey }

app/forms/disk-attach.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ export function AttachDiskSideModalForm({
4646

4747
return (
4848
<SideModalForm
49-
id="form-disk-attach"
50-
title="Attach Disk"
5149
form={form}
50+
formType="create"
51+
resourceName="disk"
52+
title="Attach Disk"
5253
onSubmit={onSubmit}
5354
loading={loading}
5455
submitError={submitError}

app/forms/disk-create.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ export function CreateDiskSideModalForm({
100100

101101
return (
102102
<SideModalForm
103-
id="create-disk-form"
104-
title="Create Disk"
105103
form={form}
104+
formType="create"
105+
resourceName="disk"
106106
onDismiss={() => onDismiss(navigate)}
107107
onSubmit={({ size, ...rest }) => {
108108
const body = { size: size * GiB, ...rest }

app/forms/firewall-rules-create.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,10 @@ export function CreateFirewallRuleForm({
504504

505505
return (
506506
<SideModalForm
507-
id="create-firewall-rule-form"
508-
title="Add firewall rule"
509507
form={form}
508+
formType="create"
509+
resourceName="rule"
510+
title="Add firewall rule"
510511
onDismiss={onDismiss}
511512
onSubmit={(values) => {
512513
// TODO: this silently overwrites existing rules with the current name.

app/forms/firewall-rules-edit.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ export function EditFirewallRuleForm({
6565

6666
return (
6767
<SideModalForm
68-
id="create-firewall-rule-form"
69-
title="Edit rule"
7068
form={form}
69+
formType="edit"
70+
resourceName="rule"
7171
onDismiss={onDismiss}
7272
onSubmit={(values) => {
7373
// note different filter logic from create: filter out the rule with the
@@ -86,7 +86,6 @@ export function EditFirewallRuleForm({
8686
// validateOnBlur
8787
loading={updateRules.isPending}
8888
submitError={updateRules.error}
89-
submitLabel="Update rule"
9089
>
9190
<CommonFields error={updateRules.error} control={form.control} />
9291
</SideModalForm>

app/forms/floating-ip-create.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ export function CreateFloatingIpSideModalForm() {
8383

8484
return (
8585
<SideModalForm
86-
id="create-floating-ip-form"
87-
title="Create Floating IP"
8886
form={form}
87+
formType="create"
88+
resourceName="floating IP"
8989
onDismiss={() => navigate(pb.floatingIps(projectSelector))}
9090
onSubmit={({ ip, ...rest }) => {
9191
createFloatingIp.mutate({

app/forms/floating-ip-edit.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ export function EditFloatingIpSideModalForm() {
5555

5656
return (
5757
<SideModalForm
58-
id="edit-floating-ip-form"
5958
form={form}
60-
title="Edit floating IP"
59+
formType="edit"
60+
resourceName="floating IP"
6161
onDismiss={onDismiss}
6262
onSubmit={({ name, description }) => {
6363
editFloatingIp.mutate({
@@ -68,7 +68,6 @@ export function EditFloatingIpSideModalForm() {
6868
}}
6969
loading={editFloatingIp.isPending}
7070
submitError={editFloatingIp.error}
71-
submitLabel="Save changes"
7271
>
7372
<NameField name="name" control={form.control} />
7473
<DescriptionField name="description" control={form.control} />

app/forms/idp/create.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ export function CreateIdpSideModalForm() {
6161

6262
return (
6363
<SideModalForm
64-
id="create-idp-form"
6564
form={form}
66-
title="Create identity provider"
65+
formType="create"
66+
resourceName="identity provider"
6767
onDismiss={onDismiss}
6868
onSubmit={async ({
6969
signingKeypair,

app/forms/idp/edit.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ export function EditIdpSideModalForm() {
4444

4545
return (
4646
<SideModalForm
47-
id="edit-idp-form"
4847
form={form}
48+
formType="edit"
49+
resourceName="identity provider"
4950
title="Identity provider"
5051
onDismiss={onDismiss}
5152
subtitle={

0 commit comments

Comments
 (0)