cheogram-www
created pr with
cheogram-www/button-grid.1
added cheogram-www/button-grid.2
1: 76d4c03 ! 1: 58cc44c Render single selection form as button grid
added cheogram-www/button-grid.3
1: 58cc44c ! 1: dd6d21a Render single selection form as button grid
-: ------- > 2: 9eabb7a Support icons on list options
added cheogram-www/button-grid.4
1: dd6d21a = 1: 6df9e5d Render single selection form as button grid
2: 9eabb7a = 2: 94950ef Support icons on list options
commented
src/components/Form.tsx:158 — is this more like selected rather than default?
commented
src/components/Form.tsx:222 — value here is whatever's currently selected? so we prefill the custom value with that? does that make sense if someone has picked custom value?
commented
src/components/Form.tsx:544 — Inside FormField we switch based on field type. Isn't this kind of like a field type? why have to logic out here instead of inside FormField. Could still render the ButtonGridField component from in there
cmds
checkout latest patchset:
ssh pr.pico.sh pull cheogram-www:button-grid | git am -3checkout specific patchset revision:
ssh pr.pico.sh pull cheogram-www:button-grid [rev] | git am -3add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh cheogram-www:button-gridadd comment to patch request:
ssh pr.pico.sh comment cheogram-www:button-grid "lgtm!"
Patchset
cheogram-www/button-grid.3
Render single selection form as button grid
Stephen Paul Weber
2026-11-12T04:40:26ZTap an option and off to the next one rather than select from drop down and click next.
Semantic diff summary
0 added,
10 modified,
0 signature changed,
0 removed
across 3 analyzed files
(1 file skipped: unsupported file type)
+4
-0
src/App.scss
#
| ... | ... | @@ -388,6 +388,10 @@ li.MuiListSubheader-root { | |
| 388 | 388 | margin: auto; | |
| 389 | 389 | max-width: 100%; | |
| 390 | 390 | ||
| 391 | + | &.button-grid-form { | |
| 392 | + | width: min(100%, 40rem); | |
| 393 | + | } | |
| 394 | + | ||
| 391 | 395 | > section:first-of-type > .MuiStack-root > h1:first-of-type { | |
| 392 | 396 | display: none; /* Shown in tab already */ | |
| 393 | 397 | } |
+23
-7
src/components/CommandSession.tsx
#
| ... | ... | @@ -4,7 +4,7 @@ import { Suspense, useRef, useState } from "react"; | |
| 4 | 4 | import { useAsyncValue, Await } from "react-router"; | |
| 5 | 5 | import { CircularProgress } from "@mui/material"; | |
| 6 | 6 | ||
| 7 | - | import { FormSection } from "./Form"; | |
| 7 | + | import { FormSection, getButtonGridField } from "./Form"; | |
| 8 | 8 | import { ios, webkit } from "../util"; | |
| 9 | 9 | ||
| 10 | 10 | const CommandSessionInner = ({ |
| ... | ... | @@ -42,14 +42,21 @@ const CommandSessionInner = ({ | |
| 42 | 42 | if (action !== "cancel") logStep(form, data ?? new FormData()); | |
| 43 | 43 | }; | |
| 44 | 44 | ||
| 45 | + | const buttonGridField = form.isResult() | |
| 46 | + | ? null | |
| 47 | + | : getButtonGridField(form, session.actions); | |
| 48 | + | const actions = buttonGridField | |
| 49 | + | ? session.actions.filter((action) => | |
| 50 | + | ["cancel", "prev"].includes(action.value), | |
| 51 | + | ) | |
| 52 | + | : session.actions; | |
| 53 | + | ||
| 45 | 54 | const commandActions = () => { | |
| 46 | 55 | return ( | |
| 47 | 56 | <div className="command-actions"> | |
| 48 | - | {session.actions.toReversed().map((action) => ( | |
| 57 | + | {actions.toReversed().map((action) => ( | |
| 49 | 58 | <Button | |
| 50 | - | type={ | |
| 51 | - | action.value === session.actions[0]?.value ? "submit" : "button" | |
| 52 | - | } | |
| 59 | + | type={action.value === actions[0]?.value ? "submit" : "button"} | |
| 53 | 60 | key={action.value} | |
| 54 | 61 | onClick={() => execute(action.value)} | |
| 55 | 62 | > |
| ... | ... | @@ -80,8 +87,17 @@ const CommandSessionInner = ({ | |
| 80 | 87 | {commandActions()} | |
| 81 | 88 | </> | |
| 82 | 89 | ) : ( | |
| 83 | - | <form method="dialog" ref={formRef}> | |
| 84 | - | <FormSection section={form} isResult={form.isResult()} /> | |
| 90 | + | <form | |
| 91 | + | method="dialog" | |
| 92 | + | ref={formRef} | |
| 93 | + | className={buttonGridField ? "button-grid-form" : undefined} | |
| 94 | + | > | |
| 95 | + | <FormSection | |
| 96 | + | section={form} | |
| 97 | + | isResult={form.isResult()} | |
| 98 | + | buttonGridField={buttonGridField?.name} | |
| 99 | + | onSubmit={() => execute(null)} | |
| 100 | + | /> | |
| 85 | 101 | {commandActions()} | |
| 86 | 102 | </form> | |
| 87 | 103 | )} |
+156
-0
src/components/Form.test.tsx
#
| ... | ... | @@ -0,0 +1,156 @@ | |
| 1 | + | // @vitest-environment jsdom | |
| 2 | + | ||
| 3 | + | import type * as borogove from "borogove"; | |
| 4 | + | import { cleanup, fireEvent, render, screen } from "@testing-library/react"; | |
| 5 | + | import { afterEach, expect, it, vi } from "vitest"; | |
| 6 | + | import { FormSection, getButtonGridField } from "./Form"; | |
| 7 | + | ||
| 8 | + | afterEach(cleanup); | |
| 9 | + | ||
| 10 | + | const field = ( | |
| 11 | + | overrides: Partial<borogove.FormField> = {}, | |
| 12 | + | ): borogove.FormField => ({ | |
| 13 | + | name: "choice", | |
| 14 | + | label: "Choice", | |
| 15 | + | desc: null, | |
| 16 | + | value: [], | |
| 17 | + | required: false, | |
| 18 | + | type: "list-single", | |
| 19 | + | datatype: "xs:string", | |
| 20 | + | options: [], | |
| 21 | + | open: false, | |
| 22 | + | rangeMin: null, | |
| 23 | + | rangeMax: null, | |
| 24 | + | regex: null, | |
| 25 | + | ...overrides, | |
| 26 | + | }); | |
| 27 | + | ||
| 28 | + | const section = (fields: borogove.FormField[]): borogove.FormSection => ({ | |
| 29 | + | title: () => null, | |
| 30 | + | items: () => | |
| 31 | + | fields.map( | |
| 32 | + | (item) => | |
| 33 | + | ({ | |
| 34 | + | text: null, | |
| 35 | + | field: item, | |
| 36 | + | section: null, | |
| 37 | + | status: null, | |
| 38 | + | tableHeader: null, | |
| 39 | + | tableRows: null, | |
| 40 | + | }) as borogove.FormItem, | |
| 41 | + | ), | |
| 42 | + | }); | |
| 43 | + | ||
| 44 | + | const choice = field({ | |
| 45 | + | options: [ | |
| 46 | + | { label: "One", value: "one" }, | |
| 47 | + | { label: "Two", value: "two" }, | |
| 48 | + | ], | |
| 49 | + | }); | |
| 50 | + | ||
| 51 | + | it("renders a single-choice field as buttons and submits the selected option", () => { | |
| 52 | + | const onSubmit = vi.fn(); | |
| 53 | + | const { container } = render( | |
| 54 | + | <form> | |
| 55 | + | <FormSection | |
| 56 | + | section={section([choice])} | |
| 57 | + | buttonGridField={choice.name} | |
| 58 | + | onSubmit={onSubmit} | |
| 59 | + | /> | |
| 60 | + | </form>, | |
| 61 | + | ); | |
| 62 | + | ||
| 63 | + | fireEvent.click(screen.getByRole("button", { name: "Two" })); | |
| 64 | + | ||
| 65 | + | expect( | |
| 66 | + | (container.querySelector('input[name="choice"]') as HTMLInputElement).value, | |
| 67 | + | ).toBe("two"); | |
| 68 | + | expect(onSubmit).toHaveBeenCalledOnce(); | |
| 69 | + | }); | |
| 70 | + | ||
| 71 | + | it("synthesizes No and Yes buttons for a boolean field", () => { | |
| 72 | + | const enabled = field({ | |
| 73 | + | name: "enabled", | |
| 74 | + | label: "Enabled", | |
| 75 | + | type: "boolean", | |
| 76 | + | }); | |
| 77 | + | const { container } = render( | |
| 78 | + | <form> | |
| 79 | + | <FormSection | |
| 80 | + | section={section([enabled])} | |
| 81 | + | buttonGridField={enabled.name} | |
| 82 | + | /> | |
| 83 | + | </form>, | |
| 84 | + | ); | |
| 85 | + | ||
| 86 | + | expect(screen.getByRole("button", { name: "No" })).toBeDefined(); | |
| 87 | + | fireEvent.click(screen.getByRole("button", { name: "Yes" })); | |
| 88 | + | ||
| 89 | + | expect( | |
| 90 | + | (container.querySelector('input[name="enabled"]') as HTMLInputElement) | |
| 91 | + | .value, | |
| 92 | + | ).toBe("true"); | |
| 93 | + | }); | |
| 94 | + | ||
| 95 | + | it("does not use a button grid for a boolean that already has a value", () => { | |
| 96 | + | const enabled = field({ | |
| 97 | + | name: "enabled", | |
| 98 | + | label: "Enabled", | |
| 99 | + | type: "boolean", | |
| 100 | + | value: ["true"], | |
| 101 | + | }); | |
| 102 | + | const { container } = render( | |
| 103 | + | <form> | |
| 104 | + | <FormSection section={section([enabled])} /> | |
| 105 | + | </form>, | |
| 106 | + | ); | |
| 107 | + | ||
| 108 | + | expect(screen.queryByRole("button", { name: "Yes" })).toBeNull(); | |
| 109 | + | expect(container.querySelector('input[type="checkbox"]')).not.toBeNull(); | |
| 110 | + | }); | |
| 111 | + | ||
| 112 | + | it("allows an open button-grid field to submit a custom value", () => { | |
| 113 | + | const onSubmit = vi.fn(); | |
| 114 | + | const openChoice = field({ | |
| 115 | + | open: true, | |
| 116 | + | options: [{ label: "One", value: "one" }], | |
| 117 | + | }); | |
| 118 | + | const { container } = render( | |
| 119 | + | <form> | |
| 120 | + | <FormSection | |
| 121 | + | section={section([openChoice])} | |
| 122 | + | buttonGridField={openChoice.name} | |
| 123 | + | onSubmit={onSubmit} | |
| 124 | + | /> | |
| 125 | + | </form>, | |
| 126 | + | ); | |
| 127 | + | ||
| 128 | + | fireEvent.click(screen.getByRole("button", { name: "other / custom" })); | |
| 129 | + | fireEvent.change(screen.getByRole("textbox"), { | |
| 130 | + | target: { value: "custom" }, | |
| 131 | + | }); | |
| 132 | + | fireEvent.click(screen.getByRole("button", { name: "Go" })); | |
| 133 | + | ||
| 134 | + | expect( | |
| 135 | + | (container.querySelector('input[name="choice"]') as HTMLInputElement).value, | |
| 136 | + | ).toBe("custom"); | |
| 137 | + | expect(onSubmit).toHaveBeenCalledOnce(); | |
| 138 | + | }); | |
| 139 | + | ||
| 140 | + | it("selects a button grid field only when there is one eligible field", () => { | |
| 141 | + | const only = field({ | |
| 142 | + | options: [{ label: "One", value: "one" }], | |
| 143 | + | }); | |
| 144 | + | expect(getButtonGridField(section([only]), [])).toBe(only); | |
| 145 | + | ||
| 146 | + | const enabled = field({ | |
| 147 | + | name: "enabled", | |
| 148 | + | type: "boolean", | |
| 149 | + | value: ["true"], | |
| 150 | + | }); | |
| 151 | + | expect(getButtonGridField(section([enabled]), [])).toBeNull(); | |
| 152 | + | ||
| 153 | + | expect( | |
| 154 | + | getButtonGridField(section([only, field({ name: "other" })]), []), | |
| 155 | + | ).toBeNull(); | |
| 156 | + | }); |
+186
-6
src/components/Form.tsx
#
| ... | ... | @@ -2,9 +2,17 @@ import * as borogove from "borogove"; | |
| 2 | 2 | import { | |
| 3 | 3 | Alert, | |
| 4 | 4 | Autocomplete, | |
| 5 | + | Box, | |
| 6 | + | Button, | |
| 7 | + | Dialog, | |
| 8 | + | DialogActions, | |
| 9 | + | DialogContent, | |
| 10 | + | DialogTitle, | |
| 5 | 11 | Switch, | |
| 6 | 12 | FormControl, | |
| 7 | 13 | FormControlLabel, | |
| 14 | + | FormHelperText, | |
| 15 | + | FormLabel, | |
| 8 | 16 | MenuItem, | |
| 9 | 17 | IconButton, | |
| 10 | 18 | TextField, |
| ... | ... | @@ -16,11 +24,12 @@ import { | |
| 16 | 24 | useMaterialReactTable, | |
| 17 | 25 | } from "material-react-table"; | |
| 18 | 26 | import { | |
| 27 | + | forwardRef, | |
| 19 | 28 | useId, | |
| 20 | - | useState, | |
| 21 | - | useMemo, | |
| 22 | 29 | useImperativeHandle, | |
| 23 | - | forwardRef, | |
| 30 | + | useMemo, | |
| 31 | + | useRef, | |
| 32 | + | useState, | |
| 24 | 33 | } from "react"; | |
| 25 | 34 | import Visibility from "@mui/icons-material/Visibility"; | |
| 26 | 35 | import VisibilityOff from "@mui/icons-material/VisibilityOff"; |
| ... | ... | @@ -57,6 +66,168 @@ const openOptionValue = (v: borogove.FormOption | string | null): string => { | |
| 57 | 66 | return v.value ?? ""; | |
| 58 | 67 | }; | |
| 59 | 68 | ||
| 69 | + | const isFillable = (field: borogove.FormField) => | |
| 70 | + | field.name !== "" && field.type !== "hidden" && field.type !== "fixed"; | |
| 71 | + | ||
| 72 | + | const countProceed = (actions: borogove.FormOption[]) => | |
| 73 | + | actions.filter((action) => !["cancel", "prev"].includes(action.value)).length; | |
| 74 | + | ||
| 75 | + | const flattenFields = (items: borogove.FormItem[]): borogove.FormField[] => | |
| 76 | + | items.flatMap((item) => [ | |
| 77 | + | ...(item.field ? [item.field] : []), | |
| 78 | + | ...(item.section ? flattenFields(item.section.items()) : []), | |
| 79 | + | ]); | |
| 80 | + | ||
| 81 | + | const fillableFields = (section: borogove.FormSection) => | |
| 82 | + | flattenFields(section.items()).filter(isFillable); | |
| 83 | + | ||
| 84 | + | export const getButtonGridField = ( | |
| 85 | + | section: borogove.FormSection, | |
| 86 | + | actions: borogove.FormOption[], | |
| 87 | + | ): borogove.FormField | null => { | |
| 88 | + | const fields = fillableFields(section); | |
| 89 | + | if (fields.length !== 1 || countProceed(actions) > 1) return null; | |
| 90 | + | const field = fields[0]; | |
| 91 | + | if (field.type === "boolean") return field.value.length === 0 ? field : null; | |
| 92 | + | if (field.type === "list-single" && field.options.length < 50) return field; | |
| 93 | + | return null; | |
| 94 | + | }; | |
| 95 | + | ||
| 96 | + | const ButtonGridField = ({ | |
| 97 | + | field, | |
| 98 | + | onSubmit, | |
| 99 | + | }: { | |
| 100 | + | field: borogove.FormField; | |
| 101 | + | onSubmit?: () => void; | |
| 102 | + | }) => { | |
| 103 | + | const [value, setValue] = useState(field.value[0] ?? ""); | |
| 104 | + | const [open, setOpen] = useState(false); | |
| 105 | + | const [customValue, setCustomValue] = useState(""); | |
| 106 | + | const hiddenInput = useRef<HTMLInputElement>(null); | |
| 107 | + | ||
| 108 | + | const options: borogove.FormOption[] = | |
| 109 | + | field.type === "boolean" | |
| 110 | + | ? [ | |
| 111 | + | { label: "No", value: "false" }, | |
| 112 | + | { label: "Yes", value: "true" }, | |
| 113 | + | ] | |
| 114 | + | : field.options; | |
| 115 | + | ||
| 116 | + | const defaultOption = !value | |
| 117 | + | ? null | |
| 118 | + | : (options.find((option) => option.value === value) ?? { | |
| 119 | + | label: value, | |
| 120 | + | value, | |
| 121 | + | }); | |
| 122 | + | ||
| 123 | + | const submit = (next: string) => { | |
| 124 | + | setValue(next); | |
| 125 | + | if (hiddenInput.current) { | |
| 126 | + | hiddenInput.current.value = next; | |
| 127 | + | hiddenInput.current.disabled = false; | |
| 128 | + | } | |
| 129 | + | onSubmit?.(); | |
| 130 | + | }; | |
| 131 | + | ||
| 132 | + | return ( | |
| 133 | + | <> | |
| 134 | + | <FormControl fullWidth> | |
| 135 | + | {field.label && <FormLabel>{field.label}</FormLabel>} | |
| 136 | + | <input | |
| 137 | + | ref={hiddenInput} | |
| 138 | + | type="hidden" | |
| 139 | + | name={field.name} | |
| 140 | + | value={value} | |
| 141 | + | disabled={!value} | |
| 142 | + | /> | |
| 143 | + | {defaultOption && ( | |
| 144 | + | <> | |
| 145 | + | <Button | |
| 146 | + | type="button" | |
| 147 | + | variant="contained" | |
| 148 | + | sx={{ | |
| 149 | + | alignSelf: "center", | |
| 150 | + | my: 2, | |
| 151 | + | }} | |
| 152 | + | onClick={() => submit(defaultOption.value)} | |
| 153 | + | > | |
| 154 | + | {defaultOption.label || defaultOption.value} | |
| 155 | + | </Button> | |
| 156 | + | <FormHelperText sx={{ textAlign: "center" }}>or</FormHelperText> | |
| 157 | + | </> | |
| 158 | + | )} | |
| 159 | + | <Box | |
| 160 | + | sx={{ | |
| 161 | + | display: "grid", | |
| 162 | + | gridTemplateColumns: | |
| 163 | + | "repeat(auto-fit, minmax(min(100%, 7.5rem), 1fr))", | |
| 164 | + | gridAutoRows: "1fr", | |
| 165 | + | gap: 1, | |
| 166 | + | mt: 1, | |
| 167 | + | }} | |
| 168 | + | > | |
| 169 | + | {options | |
| 170 | + | .filter((option) => option.value !== defaultOption?.value) | |
| 171 | + | .map((option) => ( | |
| 172 | + | <Button | |
| 173 | + | key={option.value} | |
| 174 | + | type="button" | |
| 175 | + | variant="outlined" | |
| 176 | + | sx={{ minWidth: 0, whiteSpace: "normal" }} | |
| 177 | + | onClick={() => submit(option.value)} | |
| 178 | + | > | |
| 179 | + | {option.label || option.value} | |
| 180 | + | </Button> | |
| 181 | + | ))} | |
| 182 | + | </Box> | |
| 183 | + | {field.open && ( | |
| 184 | + | <Button | |
| 185 | + | type="button" | |
| 186 | + | variant="text" | |
| 187 | + | sx={{ mt: 2 }} | |
| 188 | + | onClick={() => { | |
| 189 | + | setCustomValue(value); | |
| 190 | + | setOpen(true); | |
| 191 | + | }} | |
| 192 | + | > | |
| 193 | + | other / custom | |
| 194 | + | </Button> | |
| 195 | + | )} | |
| 196 | + | {field.desc && ( | |
| 197 | + | <FormHelperText sx={{ textAlign: "center" }}> | |
| 198 | + | {field.desc} | |
| 199 | + | </FormHelperText> | |
| 200 | + | )} | |
| 201 | + | </FormControl> | |
| 202 | + | <Dialog open={open} onClose={() => setOpen(false)} fullWidth> | |
| 203 | + | <DialogTitle>{field.label || field.name}</DialogTitle> | |
| 204 | + | <DialogContent> | |
| 205 | + | <TextField | |
| 206 | + | autoFocus | |
| 207 | + | fullWidth | |
| 208 | + | margin="dense" | |
| 209 | + | label={field.desc ?? undefined} | |
| 210 | + | value={customValue} | |
| 211 | + | onChange={(e) => setCustomValue(e.target.value)} | |
| 212 | + | /> | |
| 213 | + | </DialogContent> | |
| 214 | + | <DialogActions> | |
| 215 | + | <Button onClick={() => setOpen(false)}>Cancel</Button> | |
| 216 | + | <Button | |
| 217 | + | variant="contained" | |
| 218 | + | onClick={() => { | |
| 219 | + | setOpen(false); | |
| 220 | + | submit(customValue); | |
| 221 | + | }} | |
| 222 | + | > | |
| 223 | + | Go | |
| 224 | + | </Button> | |
| 225 | + | </DialogActions> | |
| 226 | + | </Dialog> | |
| 227 | + | </> | |
| 228 | + | ); | |
| 229 | + | }; | |
| 230 | + | ||
| 60 | 231 | const ResultField = forwardRef( | |
| 61 | 232 | ({ field }: { field: borogove.FormField }, ref) => { | |
| 62 | 233 | useImperativeHandle(ref, () => ({ focus: () => {}, value: field.value }), [ |
| ... | ... | @@ -308,9 +479,13 @@ export const FormTable = ({ | |
| 308 | 479 | export const FormSection = ({ | |
| 309 | 480 | section, | |
| 310 | 481 | isResult, | |
| 482 | + | buttonGridField, | |
| 483 | + | onSubmit, | |
| 311 | 484 | }: { | |
| 312 | 485 | section: borogove.FormSection; | |
| 313 | 486 | isResult?: boolean; | |
| 487 | + | buttonGridField?: string | null; | |
| 488 | + | onSubmit?: () => void; | |
| 314 | 489 | }) => { | |
| 315 | 490 | return ( | |
| 316 | 491 | <section> |
| ... | ... | @@ -331,9 +506,12 @@ export const FormSection = ({ | |
| 331 | 506 | <Alert severity="error">{item.text}</Alert> | |
| 332 | 507 | )} | |
| 333 | 508 | {item.text && !item.status && <p>{item.text}</p>} | |
| 334 | - | {item.field && ( | |
| 335 | - | <FormField field={item.field} isResult={isResult ?? false} /> | |
| 336 | - | )} | |
| 509 | + | {item.field && | |
| 510 | + | (item.field.name === buttonGridField ? ( | |
| 511 | + | <ButtonGridField field={item.field} onSubmit={onSubmit} /> | |
| 512 | + | ) : ( | |
| 513 | + | <FormField field={item.field} isResult={isResult ?? false} /> | |
| 514 | + | ))} | |
| 337 | 515 | {item.tableHeader && item.tableRows && ( | |
| 338 | 516 | <FormTable header={item.tableHeader} rows={item.tableRows} /> | |
| 339 | 517 | )} |