Skip to content

Commit 94f1e9e

Browse files
authored
Add Subform container for Enter handling, drop Combobox onEnter (#3422)
The diff is unreadable unless you [hide whitespace changes](https://github.com/oxidecomputer/console/pull/3422/changes?w=1) because it adds a wrapper to existing code. This is an alternative to #3417 that deletes the sad logic inside Combobox instead of fixing it. We get rid of `onEnter`, and instead of having to keep track of whether the list is open with `isOpenRef` and decide when to `preventDefault`, Combobox never calls `preventDefault` (unless it's done for it by Headless UI when selecting an option). But in subforms we still need to capture those enter keypresses before they hit the parent form, so we wrap subforms in a `Subform` component with an `onSubmit` prop. The advantage is we no longer have to remember to do this for every field we add to a subform (and we have messed this up before). This approach fixes the same problem cases as #3417, all of which are noops on `main`. Enter in a closed combobox now: - Adds the protocol filter from the ICMP type combobox, like Enter in the ICMP code field next to it - Applies the alert class filter (#3393) - Submits standalone forms from their pickers, like link silo, link pool, attach disk, create snapshot, and instance create's boot disk and image pickers `Subform.browser.spec.tsx` tests Enter in a combobox, text input, and button inside a subform, and in a combobox outside a subform. In the e2e tests, Enter adds a firewall rule target, port and ICMP type, a webhook secret, and a transit IP. Enter in a closed combobox applies the alert class filter, attaches a disk, and creates an instance from the image picker (these tests fail on main).
1 parent d8b40df commit 94f1e9e

13 files changed

Lines changed: 520 additions & 352 deletions
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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 http://mozilla.org/MPL/2.0/.
5+
*
6+
* Copyright Oxide Computer Company
7+
*/
8+
import { useState } from 'react'
9+
import { afterEach, beforeEach, expect, test, vi } from 'vitest'
10+
import { render, type RenderResult } from 'vitest-browser-react'
11+
import { commands, userEvent } from 'vitest/browser'
12+
13+
import { Combobox, toComboboxItems } from '~/ui/lib/Combobox'
14+
15+
import { Subform } from './Subform'
16+
17+
// Combobox's onClose behaves differently when the document is unfocused, and
18+
// the test iframe's focus state is nondeterministic. See Combobox.browser.spec.tsx.
19+
beforeEach(() => {
20+
vi.spyOn(document, 'hasFocus').mockReturnValue(true)
21+
})
22+
23+
afterEach(() => {
24+
vi.restoreAllMocks()
25+
})
26+
27+
const items = toComboboxItems([{ name: 'disk-3' }, { name: 'disk-4' }])
28+
29+
// Filling alone doesn't reliably leave the first option highlighted (Firefox
30+
// CI pressed Enter with nothing active), so open the list and Home to it
31+
// explicitly, like Combobox.browser.spec.tsx does
32+
async function highlightFirstOption(screen: RenderResult, label: string) {
33+
const combobox = screen.getByRole('combobox', { name: label })
34+
await combobox.fill('disk')
35+
await commands.pressComboboxKey(label, 'ArrowDown')
36+
const option = screen.getByRole('option', { name: 'disk-3' })
37+
await expect.element(option).toBeVisible()
38+
await commands.pressComboboxKey(label, 'Home')
39+
await expect
40+
.element(combobox)
41+
.toHaveAttribute('aria-activedescendant', option.element().id)
42+
}
43+
44+
function Harness() {
45+
const [inner, setInner] = useState('')
46+
const [outer, setOuter] = useState('')
47+
const [formSubmits, setFormSubmits] = useState(0)
48+
const [subformSubmits, setSubformSubmits] = useState(0)
49+
50+
return (
51+
<form
52+
aria-label="Parent form"
53+
onSubmit={(event) => {
54+
event.preventDefault()
55+
setFormSubmits((count) => count + 1)
56+
}}
57+
>
58+
<Combobox
59+
items={items}
60+
label="Outer disk"
61+
selectedItemValue={outer}
62+
onChange={setOuter}
63+
/>
64+
<Subform
65+
onSubmit={() => setSubformSubmits((count) => count + 1)}
66+
onClear={() => {}}
67+
addButtonLabel="Add"
68+
disabled={false}
69+
>
70+
<Combobox
71+
items={items}
72+
label="Inner disk"
73+
selectedItemValue={inner}
74+
onChange={setInner}
75+
/>
76+
<input aria-label="Port" />
77+
</Subform>
78+
<button type="submit">Submit</button>
79+
<output>Inner: {inner || '(empty)'}</output>
80+
<output>Outer: {outer || '(empty)'}</output>
81+
<output>Form submits: {formSubmits}</output>
82+
<output>Subform submits: {subformSubmits}</output>
83+
</form>
84+
)
85+
}
86+
87+
test('Enter selects while open, then submits the subform once closed', async () => {
88+
const screen = await render(<Harness />)
89+
90+
await highlightFirstOption(screen, 'Inner disk')
91+
await commands.pressComboboxKey('Inner disk', 'Enter')
92+
await expect.element(screen.getByText('Inner: disk-3')).toBeVisible()
93+
await expect.element(screen.getByText('Subform submits: 0')).toBeVisible()
94+
95+
await commands.pressComboboxKey('Inner disk', 'Enter')
96+
await expect.element(screen.getByText('Subform submits: 1')).toBeVisible()
97+
await expect.element(screen.getByText('Form submits: 0')).toBeVisible()
98+
})
99+
100+
test('Enter in a text input submits the subform', async () => {
101+
const screen = await render(<Harness />)
102+
103+
await screen.getByRole('textbox', { name: 'Port' }).fill('80')
104+
await userEvent.keyboard('{Enter}')
105+
await expect.element(screen.getByText('Subform submits: 1')).toBeVisible()
106+
await expect.element(screen.getByText('Form submits: 0')).toBeVisible()
107+
})
108+
109+
test('Enter on the Add button submits the subform once', async () => {
110+
const screen = await render(<Harness />)
111+
112+
const button = screen.getByRole('button', { name: 'Add' })
113+
// focus directly because WebKit doesn't Tab to buttons
114+
button.element().focus()
115+
await expect.element(button).toHaveFocus()
116+
await userEvent.keyboard('{Enter}')
117+
await expect.element(screen.getByText('Subform submits: 1')).toBeVisible()
118+
await expect.element(screen.getByText('Form submits: 0')).toBeVisible()
119+
})
120+
121+
test('outside a subform, Enter in a closed combobox submits the form', async () => {
122+
const screen = await render(<Harness />)
123+
124+
await highlightFirstOption(screen, 'Outer disk')
125+
await commands.pressComboboxKey('Outer disk', 'Enter')
126+
await expect.element(screen.getByText('Outer: disk-3')).toBeVisible()
127+
await expect.element(screen.getByText('Form submits: 0')).toBeVisible()
128+
129+
await commands.pressComboboxKey('Outer disk', 'Enter')
130+
await expect.element(screen.getByText('Form submits: 1')).toBeVisible()
131+
await expect.element(screen.getByText('Subform submits: 0')).toBeVisible()
132+
})

‎app/components/form/Subform.tsx‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
import cn from 'classnames'
9+
import type { ReactNode } from 'react'
10+
11+
import { Button } from '~/ui/lib/Button'
12+
import { KEYS } from '~/ui/util/keys'
13+
14+
type SubformProps = {
15+
/** Called on Enter in one of the subform's inputs and on the Add button */
16+
onSubmit: () => void
17+
onClear: () => void
18+
addButtonLabel: string
19+
/** Disables the Clear and Add buttons */
20+
disabled: boolean
21+
className?: string
22+
children: ReactNode
23+
}
24+
25+
/**
26+
* Container for the fields of a subform: a nested `useForm` rendered inside an
27+
* outer form, like the firewall rule targets or webhook secrets, followed by
28+
* Clear and Add buttons for adding the result to a list. A subform is
29+
* not a DOM form, so Enter in one of its inputs would submit the outer form.
30+
* This catches the Enter as it bubbles and submits the subform instead, so the
31+
* fields themselves don't need any Enter handling.
32+
*
33+
* A combobox with its options open handles Enter itself (selecting the
34+
* highlighted option) and stops propagation, so that Enter never gets here.
35+
*/
36+
export function Subform({
37+
onSubmit,
38+
onClear,
39+
addButtonLabel,
40+
disabled,
41+
className,
42+
children,
43+
}: SubformProps) {
44+
return (
45+
// The div only listens for Enter bubbling up from the inputs inside it
46+
// oxlint-disable-next-line jsx-a11y/no-static-element-interactions
47+
<div
48+
className={cn('flex flex-col gap-3', className)}
49+
onKeyDown={(e) => {
50+
if (
51+
e.key === KEYS.enter &&
52+
// a field already handled this Enter
53+
!e.defaultPrevented &&
54+
// Enter that commits an IME composition isn't a submit
55+
!e.nativeEvent.isComposing &&
56+
// buttons (Add, Clear, listbox triggers) handle Enter themselves
57+
e.target instanceof HTMLInputElement
58+
) {
59+
e.preventDefault() // prevent full form submission
60+
onSubmit()
61+
}
62+
}}
63+
>
64+
<div className="ox-subform">{children}</div>
65+
<div className="flex justify-end gap-2.5">
66+
<Button variant="ghost" size="sm" onClick={onClear} disabled={disabled}>
67+
Clear
68+
</Button>
69+
<Button size="sm" onClick={onSubmit} disabled={disabled}>
70+
{addButtonLabel}
71+
</Button>
72+
</div>
73+
</div>
74+
)
75+
}

0 commit comments

Comments
 (0)