Skip to content

Commit d600db6

Browse files
committed
wip(ui): form layout horizontal, TabbedForm layout passthrough, FormPage tabbed sections
Claude-Session: https://claude.ai/code/session_01JQwxbXNVYZR3yMKLPJJvzL Co-authored-by: Claude <noreply@anthropic.com>
1 parent 38490db commit d600db6

3 files changed

Lines changed: 280 additions & 92 deletions

File tree

‎ui/apps/console/src/components/FormPage.tsx‎

Lines changed: 238 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ import {
148148
mapFieldTypeToFormType,
149149
resolveFormWidgetType,
150150
} from '@object-ui/fields';
151+
import { Tabs, TabsContent, TabsList, TabsTrigger, cn } from '@object-ui/components';
151152
import { SchemaRendererContext, usePredicateScope, type ApiFetch } from '@object-ui/react';
152153
import { pickLocalized, useObjectTranslation, useSafeFieldLabel } from '@object-ui/i18n';
153154
import type { FormFieldSpec, FormSectionSpec, FormViewSpec } from '@object-ui/app-shell';
@@ -997,6 +998,46 @@ export function isSectionVisible(
997998
});
998999
}
9991000

1001+
/**
1002+
* The tab value of one section of a `type: 'tabbed'` form view
1003+
* (objectstack-ai/objectstack#26843): its `name`, else its heading, else its
1004+
* position. The same answer `@object-ui/plugin-form`'s `TabbedForm` gives a
1005+
* section, so one authored `defaultTab` opens the same tab on the app's tabbed
1006+
* form and on this page.
1007+
*/
1008+
export function sectionTabKey(section: Pick<RenderableSection, 'name' | 'label'>, index: number): string {
1009+
return section.name || section.label || `tab-${index}`;
1010+
}
1011+
1012+
/**
1013+
* The open tab of a tabbed form view, among the tabs actually DRAWN (a section
1014+
* its predicate hides draws no tab): the user's pick, else the form view's
1015+
* `defaultTab`, else the first drawn tab. A pick or a `defaultTab` naming a
1016+
* tab that is not drawn falls through to the next answer, so the strip never
1017+
* opens onto a panel that is not there.
1018+
*/
1019+
export function resolveActiveTab(
1020+
drawn: readonly string[],
1021+
picked: string | null,
1022+
defaultTab: string | undefined,
1023+
): string | undefined {
1024+
if (picked && drawn.includes(picked)) return picked;
1025+
if (defaultTab && drawn.includes(defaultTab)) return defaultTab;
1026+
return drawn[0];
1027+
}
1028+
1029+
/**
1030+
* One field row under the form view's `layout: 'horizontal'`
1031+
* (objectstack-ai/objectstack#26843): the label in a fixed column beside its
1032+
* control from `sm` up, and stacked as `vertical` draws it below `sm`. The
1033+
* arrangement `@object-ui/components`' form renderer gives a field item under
1034+
* the same key, so a form view reads the same here and in the app's forms:
1035+
* every child but the `<label>` (the control, its help line) takes the second
1036+
* column, and the label sits on the control's first text line.
1037+
*/
1038+
const HORIZONTAL_ROW_CLASS =
1039+
'sm:grid sm:grid-cols-[minmax(0,10rem)_minmax(0,1fr)] sm:items-baseline sm:gap-x-4 sm:[&>:not(label)]:col-start-2';
1040+
10001041
/**
10011042
* The row's EFFECTIVE `{ visible, readonly, required }` — every conditional
10021043
* rule that bears on one control, resolved in one place (objectui#5627).
@@ -2229,6 +2270,12 @@ export function FormPage({ mode, recordPath, cancelPath }: FormPageProps) {
22292270
*/
22302271
const [uploading, setUploading] = useState<Record<string, boolean>>({});
22312272
const isUploading = Object.values(uploading).some(Boolean);
2273+
/**
2274+
* The tab the user opened on a `type: 'tabbed'` form view
2275+
* (objectstack-ai/objectstack#26843). Null until they open one, so the form
2276+
* view's `defaultTab` decides until then; see {@link resolveActiveTab}.
2277+
*/
2278+
const [pickedTab, setPickedTab] = useState<string | null>(null);
22322279
/**
22332280
* The sonner id this form's submit OUTCOME is published under — one id for
22342281
* the confirmation and for the refusal, so the later of the two SUPERSEDES
@@ -2318,6 +2365,17 @@ export function FormPage({ mode, recordPath, cancelPath }: FormPageProps) {
23182365
const submitButton = readFormButton(loaded?.form?.buttons?.submit, language);
23192366
const cancelButton = readFormButton(loaded?.form?.buttons?.cancel, language);
23202367

2368+
/**
2369+
* The form view's `type: 'tabbed'` and `layout`, which this page never read
2370+
* (objectstack-ai/objectstack#26843). Tabbed draws one tab per section; with
2371+
* a single section there is nothing to switch between, so it draws stacked,
2372+
* as the app's tabbed form draws no strip for one. The form's ONE `values`
2373+
* map holds every section's fields whichever tab is open, so a submit
2374+
* carries them all.
2375+
*/
2376+
const tabbedView = loaded?.form?.type === 'tabbed' && sections.length > 1;
2377+
const horizontalRows = loaded?.form?.layout === 'horizontal';
2378+
23212379
const behavior: EffectiveSubmitBehavior = resolveSubmitBehavior(
23222380
mode,
23232381
loaded?.form?.submitBehavior,
@@ -2377,6 +2435,14 @@ export function FormPage({ mode, recordPath, cancelPath }: FormPageProps) {
23772435
isCreateForm,
23782436
predicateScope,
23792437
);
2438+
// On a tabbed form view the refused field may sit on a tab that is not
2439+
// open (objectstack-ai/objectstack#26843): open the tab of the first field
2440+
// the message below names, the way the app's tabbed form jumps to it.
2441+
const refused = faultedVisibleWhen[0] ?? missing[0];
2442+
if (refused && tabbedView) {
2443+
const at = sections.findIndex((s) => s.fields.includes(refused));
2444+
if (at >= 0) setPickedTab(sectionTabKey(sections[at], at));
2445+
}
23802446
// A faulted `visibleWhen` first (objectui#8069): it names the field and
23812447
// the rule, and nothing typed into the form can clear it, so the required
23822448
// check behind it would only ask for work that is refused anyway.
@@ -2576,6 +2642,170 @@ export function FormPage({ mode, recordPath, cancelPath }: FormPageProps) {
25762642
);
25772643
}
25782644

2645+
/**
2646+
* A section's heading (objectui#8813): `objects.{object}._sections.{name}.label`,
2647+
* falling back to the authored string, exactly as `ObjectForm` does on the
2648+
* app path. The KEY is `sec.name`; a section authored without one has no key
2649+
* and keeps its authored heading. Empty for a section with neither.
2650+
*/
2651+
const sectionHeading = (sec: RenderableSection): string =>
2652+
(sec.name ? sectionLabel(loaded.object, sec.name, sec.label ?? '') : sec.label) ?? '';
2653+
2654+
/**
2655+
* One section's description and field grid: what a stacked section draws
2656+
* under its heading, and what a tab of a tabbed form view draws as its panel
2657+
* (objectstack-ai/objectstack#26843).
2658+
*/
2659+
const renderSectionBody = (sec: RenderableSection) => (
2660+
<>
2661+
{/*
2662+
* objectui#12333 — the served description, under the heading.
2663+
* No client-side key is read for it: the signed-out visitor holds
2664+
* no bundle, and the server already localized it.
2665+
*/}
2666+
{sec.description && (
2667+
<p className="mb-3 text-sm text-muted-foreground">{sec.description}</p>
2668+
)}
2669+
<div
2670+
className={
2671+
sec.columns === 1
2672+
? 'grid grid-cols-1 gap-4'
2673+
: sec.columns === 2
2674+
? 'grid grid-cols-1 gap-4 sm:grid-cols-2'
2675+
: sec.columns === 3
2676+
? 'grid grid-cols-1 gap-4 sm:grid-cols-3'
2677+
: 'grid grid-cols-1 gap-4 sm:grid-cols-4'
2678+
}
2679+
>
2680+
{sec.fields.map((f) => {
2681+
// One resolution per row per render, read by all three of the
2682+
// things that depend on it below — the row's presence, the
2683+
// required marker, and the control's own attributes. Computing
2684+
// it three times would be three chances to disagree.
2685+
const state = resolveRowState(f, values, loaded.record, isCreateForm, predicateScope);
2686+
if (!state.visible) return null;
2687+
const spanClass =
2688+
f.colSpan === 2 ? 'sm:col-span-2'
2689+
: f.colSpan === 3 ? 'sm:col-span-3'
2690+
: f.colSpan === 4 ? 'sm:col-span-4'
2691+
: '';
2692+
return (
2693+
<div
2694+
key={f.name}
2695+
// `layout: 'horizontal'` puts the label beside the control — see
2696+
// HORIZONTAL_ROW_CLASS. `vertical` (and no layout) adds nothing.
2697+
className={horizontalRows ? cn(spanClass, HORIZONTAL_ROW_CLASS) : spanClass}
2698+
>
2699+
<label
2700+
// `for` the control, or an `id` the group / display answers
2701+
// to — the widget's declaration decides (objectui#10179).
2702+
{...rowLabelProps(f)}
2703+
className={
2704+
horizontalRows
2705+
? 'mb-1 block text-xs font-medium text-foreground sm:mb-0'
2706+
: 'mb-1 block text-xs font-medium text-foreground'
2707+
}
2708+
>
2709+
{/*
2710+
* objectui#8813 — `fields.{object}.{name}`, the same
2711+
* resolver and the same fallback-to-the-served-label as
2712+
* the app path (`ObjectForm`). On this route the served
2713+
* label is usually ALREADY localized by the server
2714+
* payload, which is why the symptom looked like "field
2715+
* labels translate, section headings do not"; the client
2716+
* lookup is the overlay an app bundle can put on top,
2717+
* not a replacement for that server value.
2718+
*/}
2719+
{fieldLabel(loaded.object, f.name, f.label)}
2720+
{/* Visual-only (objectui#3299): the control's `required` /
2721+
`aria-required` is the announced channel; hiding the `*`
2722+
keeps it out of the control's accessible name. */}
2723+
{state.required && (
2724+
<span className="ml-0.5 text-destructive" aria-hidden="true">*</span>
2725+
)}
2726+
</label>
2727+
<FieldInput
2728+
field={f}
2729+
state={state}
2730+
value={values[f.name]}
2731+
values={values}
2732+
onChange={(v) => setValues((prev) => ({ ...prev, [f.name]: v }))}
2733+
onUploadingChange={(u) =>
2734+
setUploading((prev) => (prev[f.name] === u ? prev : { ...prev, [f.name]: u }))
2735+
}
2736+
/>
2737+
{f.helpText && (
2738+
<p className="mt-1 text-xs text-muted-foreground">{f.helpText}</p>
2739+
)}
2740+
</div>
2741+
);
2742+
})}
2743+
</div>
2744+
</>
2745+
);
2746+
2747+
/**
2748+
* A `type: 'tabbed'` form view (objectstack-ai/objectstack#26843): one tab
2749+
* per section, the tab semantics of the app's tabbed form — the form view's
2750+
* `defaultTab` opens first, `tabPosition` places the strip, a section its
2751+
* predicate hides draws no tab. The panels are force-mounted and only
2752+
* CSS-hidden when inactive, so a control on a closed tab keeps its widget
2753+
* state (an upload in flight included) as well as its value.
2754+
*/
2755+
const renderTabbedSections = () => {
2756+
const drawn = sections
2757+
.map((sec, at) => ({ sec, at, key: sectionTabKey(sec, at) }))
2758+
.filter(({ sec }) => isSectionVisible(sec, values, loaded.record, predicateScope));
2759+
if (drawn.length === 0) return null;
2760+
const active = resolveActiveTab(drawn.map((d) => d.key), pickedTab, loaded.form?.defaultTab);
2761+
const stripAt = loaded.form?.tabPosition ?? 'top';
2762+
const verticalStrip = stripAt === 'left' || stripAt === 'right';
2763+
return (
2764+
<Tabs
2765+
value={active}
2766+
onValueChange={setPickedTab}
2767+
orientation={verticalStrip ? 'vertical' : 'horizontal'}
2768+
// Always a flex container: `order-last` on the strip is what puts it
2769+
// after the panels for `bottom` / `right`.
2770+
className={cn('flex w-full', verticalStrip ? 'gap-4' : 'flex-col')}
2771+
>
2772+
<TabsList
2773+
className={cn(
2774+
'self-start',
2775+
verticalStrip
2776+
? cn('h-auto flex-col', stripAt === 'right' && 'order-last')
2777+
: stripAt === 'bottom' ? 'order-last mt-4' : 'mb-4',
2778+
)}
2779+
>
2780+
{drawn.map(({ sec, at, key }) => (
2781+
<TabsTrigger
2782+
key={key}
2783+
value={key}
2784+
data-testid={`form-tab:${key}`}
2785+
className={cn(verticalStrip && 'w-full justify-start')}
2786+
>
2787+
{/* A section with no heading is named by its position. */}
2788+
{sectionHeading(sec) || String(at + 1)}
2789+
</TabsTrigger>
2790+
))}
2791+
</TabsList>
2792+
<div className="min-w-0 flex-1">
2793+
{drawn.map(({ sec, key }) => (
2794+
<TabsContent
2795+
key={key}
2796+
value={key}
2797+
forceMount
2798+
data-testid={`form-tab-panel:${key}`}
2799+
className="mt-0 rounded-md border bg-card p-4 data-[state=inactive]:hidden sm:p-5"
2800+
>
2801+
{renderSectionBody(sec)}
2802+
</TabsContent>
2803+
))}
2804+
</div>
2805+
</Tabs>
2806+
);
2807+
};
2808+
25792809
return (
25802810
<div className="mx-auto max-w-2xl p-4 sm:p-6">
25812811
<header className="mb-6">
@@ -2626,7 +2856,7 @@ export function FormPage({ mode, recordPath, cancelPath }: FormPageProps) {
26262856
)}
26272857
</header>
26282858
<form onSubmit={handleSubmit} className="space-y-6">
2629-
{sections.map((sec, i) => {
2859+
{tabbedView ? renderTabbedSections() : sections.map((sec, i) => {
26302860
// A section conditioned away is skipped IN PLACE rather than filtered
26312861
// out of the list first (objectui#5627). The key here is the section's
26322862
// POSITION, and these predicates re-decide on every keystroke: filtering
@@ -2639,98 +2869,16 @@ export function FormPage({ mode, recordPath, cancelPath }: FormPageProps) {
26392869
{sec.label && (
26402870
<h2 className={sec.description ? 'mb-1 text-sm font-medium' : 'mb-3 text-sm font-medium'}>
26412871
{/*
2642-
* objectui#8813 — the heading resolves
2643-
* `objects.{object}._sections.{name}.label` and falls back to
2644-
* the authored string, exactly as `ObjectForm` does on the
2645-
* app path. The KEY is `sec.name`; a section authored without
2646-
* one has no key and keeps its authored heading.
2647-
*
2648-
* The `sec.label &&` guard above is unchanged on purpose: this
2649-
* card translates a heading that already renders, it does not
2650-
* start rendering headings for sections that had none. A
2651-
* translation cannot conjure a heading the author did not
2652-
* write — that would be a different, wider change.
2872+
* The `sec.label &&` guard is unchanged on purpose (objectui#8813):
2873+
* this heading translates a heading that already renders, it
2874+
* does not start rendering headings for sections that had
2875+
* none. A translation cannot conjure a heading the author did
2876+
* not write — that would be a different, wider change.
26532877
*/}
2654-
{sec.name ? sectionLabel(loaded.object, sec.name, sec.label) : sec.label}
2878+
{sectionHeading(sec)}
26552879
</h2>
26562880
)}
2657-
{/*
2658-
* objectui#12333 — the served description, under the heading.
2659-
* No client-side key is read for it: the signed-out visitor holds
2660-
* no bundle, and the server already localized it.
2661-
*/}
2662-
{sec.description && (
2663-
<p className="mb-3 text-sm text-muted-foreground">{sec.description}</p>
2664-
)}
2665-
<div
2666-
className={
2667-
sec.columns === 1
2668-
? 'grid grid-cols-1 gap-4'
2669-
: sec.columns === 2
2670-
? 'grid grid-cols-1 gap-4 sm:grid-cols-2'
2671-
: sec.columns === 3
2672-
? 'grid grid-cols-1 gap-4 sm:grid-cols-3'
2673-
: 'grid grid-cols-1 gap-4 sm:grid-cols-4'
2674-
}
2675-
>
2676-
{sec.fields.map((f) => {
2677-
// One resolution per row per render, read by all three of the
2678-
// things that depend on it below — the row's presence, the
2679-
// required marker, and the control's own attributes. Computing
2680-
// it three times would be three chances to disagree.
2681-
const state = resolveRowState(f, values, loaded.record, isCreateForm, predicateScope);
2682-
if (!state.visible) return null;
2683-
return (
2684-
<div
2685-
key={f.name}
2686-
className={
2687-
f.colSpan === 2 ? 'sm:col-span-2'
2688-
: f.colSpan === 3 ? 'sm:col-span-3'
2689-
: f.colSpan === 4 ? 'sm:col-span-4'
2690-
: ''
2691-
}
2692-
>
2693-
<label
2694-
// `for` the control, or an `id` the group / display answers
2695-
// to — the widget's declaration decides (objectui#10179).
2696-
{...rowLabelProps(f)}
2697-
className="mb-1 block text-xs font-medium text-foreground"
2698-
>
2699-
{/*
2700-
* objectui#8813 — `fields.{object}.{name}`, the same
2701-
* resolver and the same fallback-to-the-served-label as
2702-
* the app path (`ObjectForm`). On this route the served
2703-
* label is usually ALREADY localized by the server
2704-
* payload, which is why the symptom looked like "field
2705-
* labels translate, section headings do not"; the client
2706-
* lookup is the overlay an app bundle can put on top,
2707-
* not a replacement for that server value.
2708-
*/}
2709-
{fieldLabel(loaded.object, f.name, f.label)}
2710-
{/* Visual-only (objectui#3299): the control's `required` /
2711-
`aria-required` is the announced channel; hiding the `*`
2712-
keeps it out of the control's accessible name. */}
2713-
{state.required && (
2714-
<span className="ml-0.5 text-destructive" aria-hidden="true">*</span>
2715-
)}
2716-
</label>
2717-
<FieldInput
2718-
field={f}
2719-
state={state}
2720-
value={values[f.name]}
2721-
values={values}
2722-
onChange={(v) => setValues((prev) => ({ ...prev, [f.name]: v }))}
2723-
onUploadingChange={(u) =>
2724-
setUploading((prev) => (prev[f.name] === u ? prev : { ...prev, [f.name]: u }))
2725-
}
2726-
/>
2727-
{f.helpText && (
2728-
<p className="mt-1 text-xs text-muted-foreground">{f.helpText}</p>
2729-
)}
2730-
</div>
2731-
);
2732-
})}
2733-
</div>
2881+
{renderSectionBody(sec)}
27342882
</section>
27352883
);
27362884
})}

0 commit comments

Comments
 (0)