Skip to content

Commit 3a0b5a9

Browse files
authored
Fix form submit wiring in a few modals (#3421)
While looking at #3417, I found that the Link silo and Link pool modals use a click handler to wire up their submit buttons. This means they don't work like forms are supposed to, e.g., pressing Enter doesn't submit the form. In this PR we hook up the submit buttons through `formId`, which makes them the submit button of the `<form>` tag. This matches other modal forms, like instance resize (#2586). On its own this only makes Enter work from the "Make default" checkbox, because the combobox swallows Enter. #3417 (or the alternative I'm about to PR) makes it work from the picker too. While looking at _that_, I asked 🤖 if any other modal forms are hooked up wrong, and it pointed me to the attach floating IP modal in instance create, which _does_ have a `<form>`, but should not because it technically renders inside the instance create form, and you can't have a form inside a form. This caused an actual bug: On Enter, Headless UI's `ListboxButton` submits the nearest form, and React bubbled that submit through the modal portal to the instance create form, so pressing Enter on the closed listbox submits the whole form. Removing the `<form>` fixes it. In this case we have to keep the submit button a click handler. We could add an explicit enter key handler, but it's not worth it.
1 parent 7b9d2e3 commit 3a0b5a9

5 files changed

Lines changed: 34 additions & 25 deletions

File tree

‎app/forms/instance-create.tsx‎

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,23 +1080,24 @@ const NetworkingSection = ({
10801080
<Modal.Body>
10811081
<Modal.Section>
10821082
<Message variant="info" content={selectedFloatingIpMessage} />
1083-
<form>
1084-
<Listbox
1085-
name="floatingIp"
1086-
items={availableFloatingIps.map((i) => ({
1087-
value: i.name,
1088-
label: <FloatingIpLabel ip={i} />,
1089-
selectedLabel: `${i.name} (${i.ip})`,
1090-
}))}
1091-
label="Floating IP"
1092-
onChange={(name) => {
1093-
setSelectedFloatingIp(availableFloatingIps.find((i) => i.name === name))
1094-
}}
1095-
required
1096-
placeholder="Select a floating IP"
1097-
selected={selectedFloatingIp?.name || ''}
1098-
/>
1099-
</form>
1083+
{/* No <form> here: this modal is inside the instance create form in
1084+
the React tree, so a submit event would bubble through the portal
1085+
and submit the whole instance create form */}
1086+
<Listbox
1087+
name="floatingIp"
1088+
items={availableFloatingIps.map((i) => ({
1089+
value: i.name,
1090+
label: <FloatingIpLabel ip={i} />,
1091+
selectedLabel: `${i.name} (${i.ip})`,
1092+
}))}
1093+
label="Floating IP"
1094+
onChange={(name) => {
1095+
setSelectedFloatingIp(availableFloatingIps.find((i) => i.name === name))
1096+
}}
1097+
required
1098+
placeholder="Select a floating IP"
1099+
selected={selectedFloatingIp?.name || ''}
1100+
/>
11001101
</Modal.Section>
11011102
</Modal.Body>
11021103
<Modal.Footer

‎app/pages/system/networking/IpPoolPage.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import { useQuery } from '@tanstack/react-query'
1010
import { createColumnHelper } from '@tanstack/react-table'
11-
import { useCallback, useMemo, useState } from 'react'
11+
import { useCallback, useId, useMemo, useState } from 'react'
1212
import { useForm, useWatch } from 'react-hook-form'
1313
import { Outlet, useNavigate } from 'react-router'
1414

@@ -484,6 +484,7 @@ const toSiloComboboxItem = ({ name, description }: Silo): ComboboxItem => ({
484484
})
485485

486486
function LinkSiloModal({ onDismiss }: { onDismiss: () => void }) {
487+
const formId = useId()
487488
const { pool } = useIpPoolSelector()
488489
const { data: poolData } = usePrefetchedQuery(ipPoolView({ pool }))
489490
const { control, handleSubmit } = useForm({ defaultValues })
@@ -545,6 +546,7 @@ function LinkSiloModal({ onDismiss }: { onDismiss: () => void }) {
545546
<Modal.Body>
546547
<Modal.Section>
547548
<form
549+
id={formId}
548550
autoComplete="off"
549551
onSubmit={(e) => {
550552
e.stopPropagation()
@@ -580,7 +582,7 @@ function LinkSiloModal({ onDismiss }: { onDismiss: () => void }) {
580582
</Modal.Body>
581583
<Modal.Footer
582584
onDismiss={onDismiss}
583-
onAction={handleSubmit(onSubmit)}
585+
formId={formId}
584586
actionLoading={isPending}
585587
actionText="Link"
586588
/>

‎app/pages/system/networking/SubnetPoolPage.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import { useQuery } from '@tanstack/react-query'
1010
import { createColumnHelper } from '@tanstack/react-table'
11-
import { useCallback, useMemo, useState } from 'react'
11+
import { useCallback, useId, useMemo, useState } from 'react'
1212
import { useForm, useWatch } from 'react-hook-form'
1313
import { Outlet, useNavigate } from 'react-router'
1414

@@ -469,6 +469,7 @@ const toSiloComboboxItem = ({ name, description }: Silo): ComboboxItem => ({
469469
})
470470

471471
function LinkSiloModal({ onDismiss }: { onDismiss: () => void }) {
472+
const formId = useId()
472473
const poolSelector = useSubnetPoolSelector()
473474
const { subnetPool } = poolSelector
474475
const { data: poolData } = usePrefetchedQuery(subnetPoolView(poolSelector))
@@ -530,6 +531,7 @@ function LinkSiloModal({ onDismiss }: { onDismiss: () => void }) {
530531
<Modal.Body>
531532
<Modal.Section>
532533
<form
534+
id={formId}
533535
autoComplete="off"
534536
onSubmit={(e) => {
535537
e.stopPropagation()
@@ -565,7 +567,7 @@ function LinkSiloModal({ onDismiss }: { onDismiss: () => void }) {
565567
</Modal.Body>
566568
<Modal.Footer
567569
onDismiss={onDismiss}
568-
onAction={handleSubmit(onSubmit)}
570+
formId={formId}
569571
actionLoading={isPending}
570572
actionText="Link"
571573
/>

‎app/pages/system/silos/SiloIpPoolsTab.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import { useQuery } from '@tanstack/react-query'
1010
import { createColumnHelper } from '@tanstack/react-table'
11-
import { useCallback, useMemo, useState } from 'react'
11+
import { useCallback, useId, useMemo, useState } from 'react'
1212
import { useForm, useWatch } from 'react-hook-form'
1313

1414
import {
@@ -284,6 +284,7 @@ type LinkPoolFormValues = {
284284
const defaultValues: LinkPoolFormValues = { pool: undefined, isDefault: false }
285285

286286
function LinkPoolModal({ onDismiss }: { onDismiss: () => void }) {
287+
const formId = useId()
287288
const { silo } = useSiloSelector()
288289
const { control, handleSubmit } = useForm({ defaultValues })
289290

@@ -338,6 +339,7 @@ function LinkPoolModal({ onDismiss }: { onDismiss: () => void }) {
338339
<Modal.Body>
339340
<Modal.Section>
340341
<form
342+
id={formId}
341343
autoComplete="off"
342344
onSubmit={(e) => {
343345
e.stopPropagation()
@@ -375,7 +377,7 @@ function LinkPoolModal({ onDismiss }: { onDismiss: () => void }) {
375377
</Modal.Body>
376378
<Modal.Footer
377379
onDismiss={onDismiss}
378-
onAction={handleSubmit(onSubmit)}
380+
formId={formId}
379381
actionText="Link"
380382
actionLoading={isPending}
381383
/>

‎app/pages/system/silos/SiloSubnetPoolsTab.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import { useQuery } from '@tanstack/react-query'
1010
import { createColumnHelper } from '@tanstack/react-table'
11-
import { useCallback, useMemo, useState } from 'react'
11+
import { useCallback, useId, useMemo, useState } from 'react'
1212
import { useForm, useWatch } from 'react-hook-form'
1313

1414
import {
@@ -271,6 +271,7 @@ type LinkPoolFormValues = {
271271
const defaultValues: LinkPoolFormValues = { pool: undefined, isDefault: false }
272272

273273
function LinkPoolModal({ onDismiss }: { onDismiss: () => void }) {
274+
const formId = useId()
274275
const { silo } = useSiloSelector()
275276
const { control, handleSubmit } = useForm({ defaultValues })
276277

@@ -319,6 +320,7 @@ function LinkPoolModal({ onDismiss }: { onDismiss: () => void }) {
319320
<Modal.Body>
320321
<Modal.Section>
321322
<form
323+
id={formId}
322324
autoComplete="off"
323325
onSubmit={(e) => {
324326
e.stopPropagation()
@@ -356,7 +358,7 @@ function LinkPoolModal({ onDismiss }: { onDismiss: () => void }) {
356358
</Modal.Body>
357359
<Modal.Footer
358360
onDismiss={onDismiss}
359-
onAction={handleSubmit(onSubmit)}
361+
formId={formId}
360362
actionText="Link"
361363
actionLoading={isPending}
362364
/>

0 commit comments

Comments
 (0)