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
Support icons on list options
Stephen Paul Weber
2026-11-12T05:16:08ZSemantic diff summary
3 added,
8 modified,
0 signature changed,
0 removed
across 2 analyzed files
(1 file skipped: unsupported file type)
+58
-0
src/App.scss
#
| ... | ... | @@ -132,6 +132,64 @@ input { | |
| 132 | 132 | font-size: inherit; | |
| 133 | 133 | } | |
| 134 | 134 | ||
| 135 | + | .form-option-icon { | |
| 136 | + | display: flex; | |
| 137 | + | align-items: center; | |
| 138 | + | justify-content: center; | |
| 139 | + | width: 1.5em; | |
| 140 | + | height: 1.5em; | |
| 141 | + | flex-shrink: 0; | |
| 142 | + | ||
| 143 | + | > svg { | |
| 144 | + | display: block; | |
| 145 | + | width: 100%; | |
| 146 | + | height: 100%; | |
| 147 | + | } | |
| 148 | + | ||
| 149 | + | &.large { | |
| 150 | + | width: 100%; | |
| 151 | + | height: 8rem; | |
| 152 | + | } | |
| 153 | + | } | |
| 154 | + | ||
| 155 | + | .form-option-button { | |
| 156 | + | display: flex; | |
| 157 | + | align-items: center; | |
| 158 | + | justify-content: center; | |
| 159 | + | gap: 0.5rem; | |
| 160 | + | min-width: 0; | |
| 161 | + | ||
| 162 | + | &.large { | |
| 163 | + | flex-direction: column; | |
| 164 | + | } | |
| 165 | + | } | |
| 166 | + | ||
| 167 | + | .form-default-option { | |
| 168 | + | align-self: center; | |
| 169 | + | margin: 1rem 0; | |
| 170 | + | } | |
| 171 | + | ||
| 172 | + | .form-text-center { | |
| 173 | + | text-align: center; | |
| 174 | + | } | |
| 175 | + | ||
| 176 | + | .form-button-grid { | |
| 177 | + | display: grid; | |
| 178 | + | grid-template-columns: repeat(auto-fit, minmax(min(100%, 7.5rem), 1fr)); | |
| 179 | + | grid-auto-rows: 1fr; | |
| 180 | + | gap: 0.5rem; | |
| 181 | + | margin-top: 0.5rem; | |
| 182 | + | ||
| 183 | + | > .form-button-grid-option { | |
| 184 | + | min-width: 0; | |
| 185 | + | white-space: normal; | |
| 186 | + | } | |
| 187 | + | } | |
| 188 | + | ||
| 189 | + | .form-option-open { | |
| 190 | + | margin-top: 1rem; | |
| 191 | + | } | |
| 192 | + | ||
| 135 | 193 | li.MuiListSubheader-root { | |
| 136 | 194 | line-height: 35px; | |
| 137 | 195 | } |
+60
-4
src/components/Form.test.tsx
#
| ... | ... | @@ -7,9 +7,18 @@ import { FormSection, getButtonGridField } from "./Form"; | |
| 7 | 7 | ||
| 8 | 8 | afterEach(cleanup); | |
| 9 | 9 | ||
| 10 | - | const field = ( | |
| 11 | - | overrides: Partial<borogove.FormField> = {}, | |
| 12 | - | ): borogove.FormField => ({ | |
| 10 | + | type TestOption = Omit<borogove.FormOption, "svg"> & { | |
| 11 | + | svg?: string | null; | |
| 12 | + | }; | |
| 13 | + | ||
| 14 | + | type TestField = Omit<Partial<borogove.FormField>, "options"> & { | |
| 15 | + | options?: TestOption[]; | |
| 16 | + | }; | |
| 17 | + | ||
| 18 | + | const field = ({ | |
| 19 | + | options = [], | |
| 20 | + | ...overrides | |
| 21 | + | }: TestField = {}): borogove.FormField => ({ | |
| 13 | 22 | name: "choice", | |
| 14 | 23 | label: "Choice", | |
| 15 | 24 | desc: null, |
| ... | ... | @@ -17,12 +26,15 @@ const field = ( | |
| 17 | 26 | required: false, | |
| 18 | 27 | type: "list-single", | |
| 19 | 28 | datatype: "xs:string", | |
| 20 | - | options: [], | |
| 21 | 29 | open: false, | |
| 22 | 30 | rangeMin: null, | |
| 23 | 31 | rangeMax: null, | |
| 24 | 32 | regex: null, | |
| 33 | + | mediaSelector: false, | |
| 34 | + | prefixLabel: null, | |
| 35 | + | suffixLabel: null, | |
| 25 | 36 | ...overrides, | |
| 37 | + | options: options.map((option) => ({ ...option, svg: option.svg ?? null })), | |
| 26 | 38 | }); | |
| 27 | 39 | ||
| 28 | 40 | const section = (fields: borogove.FormField[]): borogove.FormSection => ({ |
| ... | ... | @@ -137,6 +149,50 @@ it("allows an open button-grid field to submit a custom value", () => { | |
| 137 | 149 | expect(onSubmit).toHaveBeenCalledOnce(); | |
| 138 | 150 | }); | |
| 139 | 151 | ||
| 152 | + | it("renders SVG option icons and scales them for media selectors", () => { | |
| 153 | + | const svg = | |
| 154 | + | '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"><circle cx="5" cy="5" r="4"/></svg>'; | |
| 155 | + | const mediaChoice = field({ | |
| 156 | + | mediaSelector: true, | |
| 157 | + | options: [ | |
| 158 | + | { label: "One", value: "one", svg }, | |
| 159 | + | { label: "Two", value: "two", svg }, | |
| 160 | + | ], | |
| 161 | + | }); | |
| 162 | + | const { container } = render( | |
| 163 | + | <form> | |
| 164 | + | <FormSection | |
| 165 | + | section={section([mediaChoice])} | |
| 166 | + | buttonGridField={mediaChoice.name} | |
| 167 | + | /> | |
| 168 | + | </form>, | |
| 169 | + | ); | |
| 170 | + | ||
| 171 | + | const icons = container.querySelectorAll("svg"); | |
| 172 | + | expect(icons).toHaveLength(2); | |
| 173 | + | expect(icons[0].querySelector("circle")).not.toBeNull(); | |
| 174 | + | }); | |
| 175 | + | ||
| 176 | + | it("keeps external images inside option SVGs", () => { | |
| 177 | + | const imageSvg = | |
| 178 | + | '<svg xmlns="http://www.w3.org/2000/svg"><image href="https://example.com/thumb.png"/></svg>'; | |
| 179 | + | const imageChoice = field({ | |
| 180 | + | options: [{ label: "One", value: "one", svg: imageSvg }], | |
| 181 | + | }); | |
| 182 | + | const { container } = render( | |
| 183 | + | <form> | |
| 184 | + | <FormSection | |
| 185 | + | section={section([imageChoice])} | |
| 186 | + | buttonGridField={imageChoice.name} | |
| 187 | + | /> | |
| 188 | + | </form>, | |
| 189 | + | ); | |
| 190 | + | ||
| 191 | + | expect(container.querySelector("image")?.getAttribute("href")).toBe( | |
| 192 | + | "https://example.com/thumb.png", | |
| 193 | + | ); | |
| 194 | + | }); | |
| 195 | + | ||
| 140 | 196 | it("selects a button grid field only when there is one eligible field", () => { | |
| 141 | 197 | const only = field({ | |
| 142 | 198 | options: [{ label: "One", value: "one" }], |
+54
-21
src/components/Form.tsx
#
| ... | ... | @@ -93,6 +94,47 @@ export const getButtonGridField = ( | |
| 93 | 94 | return null; | |
| 94 | 95 | }; | |
| 95 | 96 | ||
| 97 | + | type FormOption = Omit<borogove.FormOption, "svg"> & { | |
| 98 | + | svg?: string | null; | |
| 99 | + | }; | |
| 100 | + | ||
| 101 | + | const OptionIcon = ({ svg, large }: { svg: string; large: boolean }) => { | |
| 102 | + | const content = useMemo( | |
| 103 | + | () => | |
| 104 | + | DOMPurify.sanitize(svg, { | |
| 105 | + | USE_PROFILES: { svg: true, svgFilters: true }, | |
| 106 | + | }), | |
| 107 | + | [svg], | |
| 108 | + | ); | |
| 109 | + | ||
| 110 | + | return ( | |
| 111 | + | <Box | |
| 112 | + | className={large ? "form-option-icon large" : "form-option-icon"} | |
| 113 | + | dangerouslySetInnerHTML={{ __html: content }} | |
| 114 | + | /> | |
| 115 | + | ); | |
| 116 | + | }; | |
| 117 | + | ||
| 118 | + | const OptionButtonContent = ({ | |
| 119 | + | option, | |
| 120 | + | large, | |
| 121 | + | }: { | |
| 122 | + | option: FormOption; | |
| 123 | + | large: boolean; | |
| 124 | + | }) => { | |
| 125 | + | const label = option.label || option.value; | |
| 126 | + | if (!option.svg) return <>{label}</>; | |
| 127 | + | ||
| 128 | + | return ( | |
| 129 | + | <Box className={large ? "form-option-button large" : "form-option-button"}> | |
| 130 | + | <OptionIcon svg={option.svg} large={large} /> | |
| 131 | + | <Box component="span"> | |
| 132 | + | {label} | |
| 133 | + | </Box> | |
| 134 | + | </Box> | |
| 135 | + | ); | |
| 136 | + | }; | |
| 137 | + | ||
| 96 | 138 | const ButtonGridField = ({ | |
| 97 | 139 | field, | |
| 98 | 140 | onSubmit, |
| ... | ... | @@ -105,7 +147,7 @@ const ButtonGridField = ({ | |
| 105 | 147 | const [customValue, setCustomValue] = useState(""); | |
| 106 | 148 | const hiddenInput = useRef<HTMLInputElement>(null); | |
| 107 | 149 | ||
| 108 | - | const options: borogove.FormOption[] = | |
| 150 | + | const options: FormOption[] = | |
| 109 | 151 | field.type === "boolean" | |
| 110 | 152 | ? [ | |
| 111 | 153 | { label: "No", value: "false" }, |
| ... | ... | @@ -145,27 +187,15 @@ const ButtonGridField = ({ | |
| 145 | 187 | <Button | |
| 146 | 188 | type="button" | |
| 147 | 189 | variant="contained" | |
| 148 | - | sx={{ | |
| 149 | - | alignSelf: "center", | |
| 150 | - | my: 2, | |
| 151 | - | }} | |
| 190 | + | className="form-default-option" | |
| 152 | 191 | onClick={() => submit(defaultOption.value)} | |
| 153 | 192 | > | |
| 154 | - | {defaultOption.label || defaultOption.value} | |
| 193 | + | <OptionButtonContent option={defaultOption} large /> | |
| 155 | 194 | </Button> | |
| 156 | - | <FormHelperText sx={{ textAlign: "center" }}>or</FormHelperText> | |
| 195 | + | <FormHelperText className="form-text-center">or</FormHelperText> | |
| 157 | 196 | </> | |
| 158 | 197 | )} | |
| 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 | - | > | |
| 198 | + | <Box className="form-button-grid"> | |
| 169 | 199 | {options | |
| 170 | 200 | .filter((option) => option.value !== defaultOption?.value) | |
| 171 | 201 | .map((option) => ( |
| ... | ... | @@ -173,10 +203,13 @@ const ButtonGridField = ({ | |
| 173 | 203 | key={option.value} | |
| 174 | 204 | type="button" | |
| 175 | 205 | variant="outlined" | |
| 176 | - | sx={{ minWidth: 0, whiteSpace: "normal" }} | |
| 206 | + | className="form-button-grid-option" | |
| 177 | 207 | onClick={() => submit(option.value)} | |
| 178 | 208 | > | |
| 179 | - | {option.label || option.value} | |
| 209 | + | <OptionButtonContent | |
| 210 | + | option={option} | |
| 211 | + | large={field.mediaSelector} | |
| 212 | + | /> | |
| 180 | 213 | </Button> | |
| 181 | 214 | ))} | |
| 182 | 215 | </Box> |