cheogram-www

created pr with cheogram-www/button-grid.1 on 2026-11-12T04:41:10Z · by 9e834e0d
added cheogram-www/button-grid.2 on 2026-11-12T04:53:26Z · by 9e834e0d
1: 76d4c03 ! 1: 58cc44c Render single selection form as button grid
added cheogram-www/button-grid.3 on 2026-11-12T05:17:58Z · by 9e834e0d
1: 58cc44c ! 1: dd6d21a Render single selection form as button grid
-: ------- > 2: 9eabb7a Support icons on list options
added cheogram-www/button-grid.4 on 2026-11-13T16:25:21Z · by 3c31bc8c
1: dd6d21a = 1: 6df9e5d Render single selection form as button grid
2: 9eabb7a = 2: 94950ef Support icons on list options
commented on 2026-11-13T16:25:21Z · by 3c31bc8c
src/components/Form.tsx:158 — is this more like selected rather than default?
commented on 2026-11-13T16:25:23Z · by 3c31bc8c
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 on 2026-11-13T16:25:26Z · by 3c31bc8c
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 -3
checkout specific patchset revision:
ssh pr.pico.sh pull cheogram-www:button-grid [rev] | git am -3
add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh cheogram-www:button-grid
add comment to patch request:
ssh pr.pico.sh comment cheogram-www:button-grid "lgtm!"
+58 -0 src/App.scss #
......@@ -132,6 +132,64 @@ input {
132132 font-size: inherit;
133133 }
134134
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+
135193 li.MuiListSubheader-root {
136194 line-height: 35px;
137195 }
+60 -4 src/components/Form.test.tsx #
......@@ -7,9 +7,18 @@ import { FormSection, getButtonGridField } from "./Form";
77
88 afterEach(cleanup);
99
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 => ({
1322 name: "choice",
1423 label: "Choice",
1524 desc: null,
......@@ -17,12 +26,15 @@ const field = (
1726 required: false,
1827 type: "list-single",
1928 datatype: "xs:string",
20- options: [],
2129 open: false,
2230 rangeMin: null,
2331 rangeMax: null,
2432 regex: null,
33+ mediaSelector: false,
34+ prefixLabel: null,
35+ suffixLabel: null,
2536 ...overrides,
37+ options: options.map((option) => ({ ...option, svg: option.svg ?? null })),
2638 });
2739
2840 const section = (fields: borogove.FormField[]): borogove.FormSection => ({
......@@ -137,6 +149,50 @@ it("allows an open button-grid field to submit a custom value", () => {
137149 expect(onSubmit).toHaveBeenCalledOnce();
138150 });
139151
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+
140196 it("selects a button grid field only when there is one eligible field", () => {
141197 const only = field({
142198 options: [{ label: "One", value: "one" }],
+54 -21 src/components/Form.tsx #
......@@ -1,4 +1,5 @@
11 import * as borogove from "borogove";
2+import DOMPurify from "dompurify";
23 import {
34 Alert,
45 Autocomplete,
......@@ -93,6 +94,47 @@ export const getButtonGridField = (
9394 return null;
9495 };
9596
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+
96138 const ButtonGridField = ({
97139 field,
98140 onSubmit,
......@@ -105,7 +147,7 @@ const ButtonGridField = ({
105147 const [customValue, setCustomValue] = useState("");
106148 const hiddenInput = useRef<HTMLInputElement>(null);
107149
108- const options: borogove.FormOption[] =
150+ const options: FormOption[] =
109151 field.type === "boolean"
110152 ? [
111153 { label: "No", value: "false" },
......@@ -145,27 +187,15 @@ const ButtonGridField = ({
145187 <Button
146188 type="button"
147189 variant="contained"
148- sx={{
149- alignSelf: "center",
150- my: 2,
151- }}
190+ className="form-default-option"
152191 onClick={() => submit(defaultOption.value)}
153192 >
154- {defaultOption.label || defaultOption.value}
193+ <OptionButtonContent option={defaultOption} large />
155194 </Button>
156- <FormHelperText sx={{ textAlign: "center" }}>or</FormHelperText>
195+ <FormHelperText className="form-text-center">or</FormHelperText>
157196 </>
158197 )}
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">
169199 {options
170200 .filter((option) => option.value !== defaultOption?.value)
171201 .map((option) => (
......@@ -173,10 +203,13 @@ const ButtonGridField = ({
173203 key={option.value}
174204 type="button"
175205 variant="outlined"
176- sx={{ minWidth: 0, whiteSpace: "normal" }}
206+ className="form-button-grid-option"
177207 onClick={() => submit(option.value)}
178208 >
179- {option.label || option.value}
209+ <OptionButtonContent
210+ option={option}
211+ large={field.mediaSelector}
212+ />
180213 </Button>
181214 ))}
182215 </Box>
......@@ -184,7 +217,7 @@ const ButtonGridField = ({
184217 <Button
185218 type="button"
186219 variant="text"
187- sx={{ mt: 2 }}
220+ className="form-option-open"
188221 onClick={() => {
189222 setCustomValue(value);
190223 setOpen(true);
......@@ -194,7 +227,7 @@ const ButtonGridField = ({
194227 </Button>
195228 )}
196229 {field.desc && (
197- <FormHelperText sx={{ textAlign: "center" }}>
230+ <FormHelperText className="form-text-center">
198231 {field.desc}
199232 </FormHelperText>
200233 )}
Back to top