Skip to content

Commit 38c8664

Browse files
committed
fix(console): form entry points render the form view's buttons, layout, defaults and tab keys (wip)
Claude-Session: https://claude.ai/code/session_01JQwxbabN2bfLWeaoNk6Jue Co-authored-by: Claude <noreply@anthropic.com>
1 parent b8f596c commit 38c8664

7 files changed

Lines changed: 259 additions & 34 deletions

File tree

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

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,12 @@ export function readLoadedRecord(
12541254
* then write;
12551255
* 3. an explicit `?prefill_<field>=` query param, which wins over both.
12561256
*
1257+
* A CREATE also reads the form view's own `defaults` (objectstack-ai/objectstack#26843),
1258+
* the author's opening values for this form: they sit over source 1, since a
1259+
* form view is narrower than its object, and under source 3, since a `prefill_`
1260+
* param outranks every default. An edit never reads them: the stored record is
1261+
* the truth there.
1262+
*
12571263
* Point 3 is the ruling: "explicit `prefill_` params override the loaded
12581264
* record's values for the fields they name … record values fill the rest". A
12591265
* producer that forwards `?recordId=` AND `?prefill_x=` in one URL is
@@ -1331,6 +1337,7 @@ export function readPrefill(
13311337
fields: RenderableField[],
13321338
search: URLSearchParams,
13331339
record?: Record<string, unknown> | null,
1340+
formDefaults?: Record<string, unknown> | null,
13341341
): Record<string, unknown> {
13351342
const out: Record<string, unknown> = {};
13361343
for (const f of fields) {
@@ -1343,6 +1350,10 @@ export function readPrefill(
13431350
} else if (f.defaultValue !== undefined && !isRuntimeDefault(f.defaultValue)) {
13441351
out[f.name] = f.defaultValue;
13451352
}
1353+
// The form view's `defaults`, on a create only — see the docblock.
1354+
if (!record && formDefaults && Object.prototype.hasOwnProperty.call(formDefaults, f.name)) {
1355+
out[f.name] = formDefaults[f.name];
1356+
}
13461357
// `hasOwnProperty` rather than a truthiness/undefined test: a stored null
13471358
// or empty string is a real value on an edit form, and must beat the
13481359
// default.
@@ -1355,6 +1366,20 @@ export function readPrefill(
13551366
return out;
13561367
}
13571368

1369+
/**
1370+
* One `FormView.buttons` entry as this page draws it (objectstack-ai/objectstack#26843):
1371+
* shown unless the author wrote `show: false`, and the authored label — an
1372+
* `I18nLabel`, resolved against the visitor's language — or `''` when there is
1373+
* none, so the caller's own label shows. `buttons.reset` is never read: this
1374+
* page draws no reset button.
1375+
*/
1376+
export function readFormButton(
1377+
config: { show?: boolean; label?: unknown } | undefined,
1378+
language: string,
1379+
): { show: boolean; label: string } {
1380+
return { show: config?.show !== false, label: pickLocalized(config?.label, language) };
1381+
}
1382+
13581383
/**
13591384
* Drop the keys a CREATE payload must leave to the producer (objectui#5883).
13601385
*
@@ -2261,7 +2286,7 @@ export function FormPage({ mode, recordPath, cancelPath }: FormPageProps) {
22612286
// build needs no language and the load does not re-run on a switch.
22622287
const sections = buildSections(result.form, result.objectSchema);
22632288
const allFields = sections.flatMap((s) => s.fields);
2264-
setValues(readPrefill(allFields, search, result.record));
2289+
setValues(readPrefill(allFields, search, result.record, result.form?.defaults));
22652290
})
22662291
.catch((e) => {
22672292
if (!cancelled) {
@@ -2284,6 +2309,15 @@ export function FormPage({ mode, recordPath, cancelPath }: FormPageProps) {
22842309
[loaded, language],
22852310
);
22862311

2312+
/**
2313+
* The form view's `buttons` (objectstack-ai/objectstack#26843), which this
2314+
* page never read: an authored `show: false` removes the button, an authored
2315+
* `label` replaces this page's own. The public route draws no Cancel,
2316+
* authored or not — see {@link FormPageProps.cancelPath}.
2317+
*/
2318+
const submitButton = readFormButton(loaded?.form?.buttons?.submit, language);
2319+
const cancelButton = readFormButton(loaded?.form?.buttons?.cancel, language);
2320+
22872321
const behavior: EffectiveSubmitBehavior = resolveSubmitBehavior(
22882322
mode,
22892323
loaded?.form?.submitBehavior,
@@ -2460,7 +2494,7 @@ export function FormPage({ mode, recordPath, cancelPath }: FormPageProps) {
24602494
// means back to the record as loaded — NOT to blank, which would
24612495
// stage a wipe of every field on the next save.
24622496
const allFields = sections.flatMap((s) => s.fields);
2463-
setValues(readPrefill(allFields, search, loaded.record));
2497+
setValues(readPrefill(allFields, search, loaded.record, loaded.form?.defaults));
24642498
break;
24652499
}
24662500
case 'next-record':
@@ -2706,21 +2740,22 @@ export function FormPage({ mode, recordPath, cancelPath }: FormPageProps) {
27062740
</div>
27072741
)}
27082742
<div className="flex justify-end gap-2">
2709-
{mode === 'internal' && (
2743+
{mode === 'internal' && cancelButton.show && (
27102744
<button
27112745
type="button"
27122746
onClick={handleCancel}
27132747
disabled={submitting}
27142748
className="rounded-md border bg-background px-4 py-2 text-sm font-medium shadow-sm hover:bg-accent hover:text-accent-foreground disabled:opacity-50"
27152749
>
2716-
{t('common.cancel')}
2750+
{cancelButton.label || t('common.cancel')}
27172751
</button>
27182752
)}
27192753
{/* Blocked while an upload is in flight (objectui#10167): a file's
27202754
value only becomes a `sys_file` id once the adapter settles, so a
27212755
submit that beats it writes an empty field and still reports
27222756
success. The widget contract exists for this — see
27232757
`FieldInputProps.onUploadingChange`. */}
2758+
{submitButton.show && (
27242759
<button
27252760
type="submit"
27262761
disabled={submitting || isUploading}
@@ -2730,8 +2765,9 @@ export function FormPage({ mode, recordPath, cancelPath }: FormPageProps) {
27302765
? t('publicForm.submitting')
27312766
: isUploading
27322767
? t('fields.file.uploading')
2733-
: t('publicForm.submit')}
2768+
: submitButton.label || t('publicForm.submit')}
27342769
</button>
2770+
)}
27352771
</div>
27362772
</form>
27372773
</div>

‎ui/packages/app-shell/src/console/AppContent.tsx‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { buildExpressionUser } from '../providers/expressionUser.js';
3434
import { useTrackRouteAsRecent } from '../hooks/useTrackRouteAsRecent.js';
3535
import { useHomePath } from '../hooks/useHomePath.js';
3636
import { useSignedInUserLocale } from '../hooks/useUserLocale.js';
37-
import { resolveRecordFormTarget, resolveRecordFormModalLayout, resolveNavigateCreateUrl, resolveNavigateEditUrl, resolvePostCreateTarget, recordFormCreateValues, readRecordFormCreateValues, RECORD_FORM_CREATE_VALUES_STATE } from '../utils/recordFormNavigation.js';
37+
import { resolveRecordFormTarget, resolveRecordFormModalLayout, resolveFormViewChrome, selectRecordFormView, resolveNavigateCreateUrl, resolveNavigateEditUrl, resolvePostCreateTarget, recordFormCreateValues, readRecordFormCreateValues, RECORD_FORM_CREATE_VALUES_STATE } from '../utils/recordFormNavigation.js';
3838
import { deriveRecordSurface, deriveRecordFlowSurface } from '@object-ui/plugin-view';
3939
import { RECORD_FORM_PARAM, RECORD_FORM_OBJECT_PARAM, RECORD_FORM_LINK_PARAM } from '../urlParams.js';
4040
import { appAddressSegment, appRouteSegment, canonicalAppPathname, matchAppBySegment } from '../utils/appRoute.js';
@@ -322,7 +322,7 @@ export function AppContent({ extraRoutes, extraRoutesNoApp }: AppContentProps =
322322
// worse answer than the literal it replaced.
323323
const homePath = useHomePath();
324324
const previewDrafts = usePreviewDrafts();
325-
const { t } = useObjectTranslation();
325+
const { t, language } = useObjectTranslation();
326326
const { objectLabel, objectPluralLabel } = useObjectLabel();
327327

328328

@@ -1500,9 +1500,6 @@ export function AppContent({ extraRoutes, extraRoutesNoApp }: AppContentProps =
15001500
// history entry carries. Each is handed as the object it is,
15011501
// never a merge rebuilt per render: the form re-seeds whenever
15021502
// `initialValues` is a new object.
1503-
...(!editingRecord && (formLinkValues ?? overlayCreateValues)
1504-
? { initialValues: formLinkValues ?? overlayCreateValues }
1505-
: {}),
15061503
// Honor the object's DEFAULT FORM VIEW: curated sections (field
15071504
// selection + order + grouping), `contentLayout: 'tabbed'` when the
15081505
// view is tabbed, inline child collections (master-detail), and its
@@ -1542,7 +1539,6 @@ export function AppContent({ extraRoutes, extraRoutesNoApp }: AppContentProps =
15421539
: t('form.createTitle', { object: objectLabel(formObjectDef as any) }),
15431540
open: isDialogOpen,
15441541
onOpenChange: (open: boolean) => { if (!open) closeRecordForm(); },
1545-
layout: 'vertical',
15461542
// objectui#6514 — the same declared-key gate `RecordFormPage`
15471543
// runs, through the same helper. This read `f.visible`, a key
15481544
// `FieldSchema` refuses by name, so nothing an author could
@@ -1563,12 +1559,26 @@ export function AppContent({ extraRoutes, extraRoutesNoApp }: AppContentProps =
15631559
: [],
15641560
onSuccess: handleRecordFormSuccess,
15651561
onCancel: handleDialogCancel,
1566-
showSubmit: true,
1567-
showCancel: true,
1568-
submitText: editingRecord
1569-
? t('form.update', { defaultValue: 'Update' })
1570-
: t('form.create', { defaultValue: 'Create' }),
1571-
cancelText: t('common.cancel'),
1562+
// objectstack-ai/objectstack#26843 — the selected form view's
1563+
// `buttons`, `layout` and `defaults` over this dialog's own
1564+
// defaults (`resolveFormViewChrome` states the precedence). A
1565+
// create's opening values: the `formLink` pre-link
1566+
// (objectstack-ai/objectstack#2604 D3, refresh-safe), else the
1567+
// values its opener's history entry carries (objectui#12291),
1568+
// over the form view's `defaults`; each handed on as the object
1569+
// it is, as the form re-seeds on a new `initialValues`.
1570+
...resolveFormViewChrome(
1571+
selectRecordFormView(formObjectDef as any, editingRecord ? 'edit' : 'create'),
1572+
{
1573+
flow: editingRecord ? 'edit' : 'create',
1574+
language,
1575+
submitText: editingRecord
1576+
? t('form.update', { defaultValue: 'Update' })
1577+
: t('form.create', { defaultValue: 'Create' }),
1578+
cancelText: t('common.cancel'),
1579+
createValues: editingRecord ? undefined : formLinkValues ?? overlayCreateValues,
1580+
},
1581+
),
15721582
}}
15731583
dataSource={dataSource}
15741584
/>

‎ui/packages/app-shell/src/utils/recordFormNavigation.ts‎

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import { FormViewSchema, type FormView } from '@objectstack/spec/ui';
1212
import type { RecordFlowSurface } from '@objectstack/spec/data';
13+
import { pickLocalized } from '@object-ui/i18n';
1314

1415
/**
1516
* Subset of the object metadata shape consumed by the resolver. Mirrors the
@@ -157,6 +158,16 @@ export interface FormViewDefinition {
157158
subforms?: any[];
158159
/** The modal's size bucket, as the spec's `FormView.modalSize` declares it. */
159160
modalSize?: FormView['modalSize'];
161+
/** Initially active tab of a `tabbed` form view (a section name). */
162+
defaultTab?: FormView['defaultTab'];
163+
/** Tab strip position of a `tabbed` form view. */
164+
tabPosition?: FormView['tabPosition'];
165+
/** Field layout direction — see {@link resolveFormViewChrome}. */
166+
layout?: FormView['layout'];
167+
/** Per-button visibility and label — see {@link resolveFormViewChrome}. */
168+
buttons?: FormView['buttons'];
169+
/** Create-mode initial values — see {@link resolveFormViewChrome}. */
170+
defaults?: FormView['defaults'];
160171
}
161172

162173
/**
@@ -227,6 +238,13 @@ export interface FormViewModalLayout {
227238
* the form view authors none, so the modal keeps its own default size.
228239
*/
229240
modalSize?: FormView['modalSize'];
241+
/**
242+
* A `tabbed` form view's `defaultTab` and `tabPosition`
243+
* (objectstack-ai/objectstack#26843), copied only beside
244+
* `contentLayout: 'tabbed'` — the one layout that draws a tab strip.
245+
*/
246+
defaultTab?: string;
247+
tabPosition?: FormView['tabPosition'];
230248
}
231249

232250
/**
@@ -271,7 +289,21 @@ export function resolveFormViewLayout(
271289
layout.sections = formView.sections;
272290
// Only 'tabbed' maps to a modal content layout; 'wizard'/'split' have no
273291
// modal equivalent and degrade to a stacked section list.
274-
if (formView.type === 'tabbed') layout.contentLayout = 'tabbed';
292+
if (formView.type === 'tabbed') {
293+
layout.contentLayout = 'tabbed';
294+
// objectstack-ai/objectstack#26843: the tab the form opens on and where
295+
// its strip sits, which the modal dropped. A position is copied only when
296+
// the spec's enum allows it, read from `FormViewSchema` as `modalSize` is.
297+
if (typeof formView.defaultTab === 'string' && formView.defaultTab) {
298+
layout.defaultTab = formView.defaultTab;
299+
}
300+
if (
301+
formView.tabPosition !== undefined
302+
&& FormViewSchema.shape.tabPosition.safeParse(formView.tabPosition).success
303+
) {
304+
layout.tabPosition = formView.tabPosition;
305+
}
306+
}
275307
}
276308

277309
if (Array.isArray(formView.subforms) && formView.subforms.length > 0) {
@@ -314,6 +346,106 @@ export function resolveRecordFormModalLayout(
314346
return modalSize ? { ...layout, modalSize } : layout;
315347
}
316348

349+
/**
350+
* The form chrome a console form host draws from the form view it opens
351+
* (objectstack-ai/objectstack#26843), ready to spread into an `<ObjectForm>` or
352+
* `<ModalForm>` schema: the action buttons' visibility and labels, the field
353+
* layout, and a create's opening values.
354+
*/
355+
export interface FormViewChrome {
356+
showSubmit: boolean;
357+
submitText: string;
358+
showCancel: boolean;
359+
cancelText: string;
360+
layout: 'vertical' | 'horizontal';
361+
/** Omitted when the flow opens with no values (an edit, or a bare create). */
362+
initialValues?: Record<string, unknown>;
363+
}
364+
365+
/**
366+
* The opening values of a create that carries both a form view's `defaults`
367+
* and the host's own values, per `defaults` payload. The forms re-seed whenever
368+
* `initialValues` is a new object, so a merge rebuilt per render would wipe
369+
* what the user typed. The cache keys on the metadata payload and on the
370+
* host's values by CONTENT, never on a memoised identity (ui/AGENTS.md #10).
371+
*/
372+
const mergedCreateValues = new WeakMap<object, { key: string; values: Record<string, unknown> }>();
373+
374+
function createSeed(
375+
defaults: Record<string, unknown> | undefined,
376+
createValues: Record<string, unknown> | undefined,
377+
): Record<string, unknown> | undefined {
378+
if (!defaults || Object.keys(defaults).length === 0) return createValues;
379+
if (!createValues) return defaults;
380+
let key: string;
381+
try {
382+
key = JSON.stringify(createValues);
383+
} catch {
384+
return { ...defaults, ...createValues };
385+
}
386+
const hit = mergedCreateValues.get(defaults);
387+
if (hit && hit.key === key) return hit.values;
388+
const values = { ...defaults, ...createValues };
389+
mergedCreateValues.set(defaults, { key, values });
390+
return values;
391+
}
392+
393+
/**
394+
* Resolve the {@link FormViewChrome} of the form view a record flow opens.
395+
*
396+
* Every console form host used to hard-set these props — `showSubmit: true`,
397+
* its own submit label, `layout: 'vertical'` — over the keys the form view
398+
* declares, so an authored `buttons`, `layout` or `defaults` saved, published
399+
* and never rendered. No flat `showSubmit` / `submitText` key is an author's
400+
* (the spec's form view refuses them), so the host's values are console
401+
* defaults and the author's keys win:
402+
*
403+
* - `buttons.submit` / `buttons.cancel`: an authored `show` over the host's
404+
* `true`; an authored `label`, an `I18nLabel` resolved against `language`,
405+
* over the host's label. `buttons.reset` is not read: no host draws a
406+
* reset button.
407+
* - `layout`: the authored value when the spec's enum allows it, else
408+
* `'vertical'`, the renderer default.
409+
* - `defaults`, on a create only: the opening values under the ones the host
410+
* already carries (`createValues`: a URL prefill, a parent pre-link, a
411+
* seeded day), which win per field.
412+
*/
413+
export function resolveFormViewChrome(
414+
formView: FormViewDefinition | null | undefined,
415+
opts: {
416+
flow: RecordFormFlow;
417+
/** The UI language an authored `I18nLabel` resolves against. */
418+
language: string;
419+
/** The host's submit label when the form view authors none. */
420+
submitText: string;
421+
/** The host's cancel label when the form view authors none. */
422+
cancelText: string;
423+
/** The values the host opens a create with, if any. */
424+
createValues?: Record<string, unknown>;
425+
},
426+
): FormViewChrome {
427+
const { submit, cancel } = formView?.buttons ?? {};
428+
const layout = formView?.layout !== undefined
429+
&& FormViewSchema.shape.layout.safeParse(formView.layout).success
430+
? formView.layout
431+
: undefined;
432+
const chrome: FormViewChrome = {
433+
showSubmit: submit?.show ?? true,
434+
submitText: pickLocalized(submit?.label, opts.language) || opts.submitText,
435+
showCancel: cancel?.show ?? true,
436+
cancelText: pickLocalized(cancel?.label, opts.language) || opts.cancelText,
437+
layout: layout ?? 'vertical',
438+
};
439+
const defaults = formView?.defaults && typeof formView.defaults === 'object' && !Array.isArray(formView.defaults)
440+
? formView.defaults
441+
: undefined;
442+
const initialValues = opts.flow === 'create'
443+
? createSeed(defaults, opts.createValues)
444+
: opts.createValues;
445+
if (initialValues) chrome.initialValues = initialValues;
446+
return chrome;
447+
}
448+
317449
/**
318450
* Result of the post-create-save navigation decision (objectstack-ai/objectstack#2604 save invariant:
319451
* *create takes you to the record you made*). `kind: 'none'` means stay put

0 commit comments

Comments
 (0)