cheogram-www
created pr with
cheogram-www/radios-for-five-or-fewer.1
commented
Changes are available at https://tangled.org/mrry.band/cheogram-www radios-for-five-or-fewer
cmds
checkout latest patchset:
ssh pr.pico.sh pull cheogram-www:radios-for-five-or-fewer | git am -3checkout specific patchset revision:
ssh pr.pico.sh pull cheogram-www:radios-for-five-or-fewer [rev] | git am -3add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh cheogram-www:radios-for-five-or-feweradd comment to patch request:
ssh pr.pico.sh comment cheogram-www:radios-for-five-or-fewer "lgtm!"
Patchset
cheogram-www/radios-for-five-or-fewer.1
Show radios for five or fewer
Eric Roberts
2026-11-18T22:10:15ZWhen there aren't many options in a list it's inconvenient to have to open a dropdown. Primarily we also want this for registration flow so we can pick payment method with a radio.
Semantic diff summary
0 added,
3 modified,
0 signature changed,
0 removed
across 2 analyzed files
+35
-0
src/components/Form.test.tsx
#
| ... | ... | @@ -60,6 +60,41 @@ const choice = field({ | |
| 60 | 60 | ], | |
| 61 | 61 | }); | |
| 62 | 62 | ||
| 63 | + | it("renders five choices as radio options", () => { | |
| 64 | + | render( | |
| 65 | + | <FormSection | |
| 66 | + | section={section([ | |
| 67 | + | field({ | |
| 68 | + | options: Array.from({ length: 5 }, (_, i) => ({ | |
| 69 | + | label: `Option ${i}`, | |
| 70 | + | value: String(i), | |
| 71 | + | })), | |
| 72 | + | }), | |
| 73 | + | ])} | |
| 74 | + | />, | |
| 75 | + | ); | |
| 76 | + | expect( | |
| 77 | + | screen.getAllByRole("radio").map((radio) => radio.getAttribute("value")), | |
| 78 | + | ).toEqual(["0", "1", "2", "3", "4"]); | |
| 79 | + | }); | |
| 80 | + | ||
| 81 | + | it("keeps six choices as a dropdown", () => { | |
| 82 | + | render( | |
| 83 | + | <FormSection | |
| 84 | + | section={section([ | |
| 85 | + | field({ | |
| 86 | + | options: Array.from({ length: 6 }, (_, i) => ({ | |
| 87 | + | label: String(i), | |
| 88 | + | value: String(i), | |
| 89 | + | })), | |
| 90 | + | }), | |
| 91 | + | ])} | |
| 92 | + | />, | |
| 93 | + | ); | |
| 94 | + | expect(screen.getByRole("combobox")).toBeDefined(); | |
| 95 | + | expect(screen.queryByRole("radio")).toBeNull(); | |
| 96 | + | }); | |
| 97 | + | ||
| 63 | 98 | it("renders a single-choice field as buttons and submits the selected option", () => { | |
| 64 | 99 | const onSubmit = vi.fn(); | |
| 65 | 100 | const { container } = render( |
+31
-0
src/components/Form.tsx
#
| ... | ... | @@ -462,6 +464,35 @@ const FormField = ({ | |
| 462 | 464 | ); | |
| 463 | 465 | } | |
| 464 | 466 | ||
| 467 | + | if (!readOnly && field.type === "list-single" && field.options.length <= 5) { | |
| 468 | + | return ( | |
| 469 | + | <FormControl fullWidth required={field.required}> | |
| 470 | + | <FormLabel id={selectId}>{field.label}</FormLabel> | |
| 471 | + | <RadioGroup | |
| 472 | + | aria-labelledby={selectId} | |
| 473 | + | aria-describedby={field.desc ? datalistId : undefined} | |
| 474 | + | name={field.name} | |
| 475 | + | defaultValue={field.value[0] ?? ""} | |
| 476 | + | > | |
| 477 | + | {field.options.map((option) => ( | |
| 478 | + | <FormControlLabel | |
| 479 | + | key={option.value} | |
| 480 | + | value={option.value} | |
| 481 | + | control={ | |
| 482 | + | <Radio slotProps={{ input: { required: field.required } }} /> | |
| 483 | + | } | |
| 484 | + | label={option.label ?? option.value} | |
| 485 | + | /> | |
| 486 | + | ))} | |
| 487 | + | </RadioGroup> | |
| 488 | + | {suffix} | |
| 489 | + | {field.desc && ( | |
| 490 | + | <FormHelperText id={datalistId}>{field.desc}</FormHelperText> | |
| 491 | + | )} | |
| 492 | + | </FormControl> | |
| 493 | + | ); | |
| 494 | + | } | |
| 495 | + | ||
| 465 | 496 | if (multiple) { | |
| 466 | 497 | // Terrible hack: https://github.com/mui/material-ui/issues/48678 | |
| 467 | 498 | setTimeout(() => { |