|
10 | 10 |
|
11 | 11 | import { FormViewSchema, type FormView } from '@objectstack/spec/ui'; |
12 | 12 | import type { RecordFlowSurface } from '@objectstack/spec/data'; |
| 13 | +import { pickLocalized } from '@object-ui/i18n'; |
13 | 14 |
|
14 | 15 | /** |
15 | 16 | * Subset of the object metadata shape consumed by the resolver. Mirrors the |
@@ -157,6 +158,16 @@ export interface FormViewDefinition { |
157 | 158 | subforms?: any[]; |
158 | 159 | /** The modal's size bucket, as the spec's `FormView.modalSize` declares it. */ |
159 | 160 | 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']; |
160 | 171 | } |
161 | 172 |
|
162 | 173 | /** |
@@ -227,6 +238,13 @@ export interface FormViewModalLayout { |
227 | 238 | * the form view authors none, so the modal keeps its own default size. |
228 | 239 | */ |
229 | 240 | 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']; |
230 | 248 | } |
231 | 249 |
|
232 | 250 | /** |
@@ -271,7 +289,21 @@ export function resolveFormViewLayout( |
271 | 289 | layout.sections = formView.sections; |
272 | 290 | // Only 'tabbed' maps to a modal content layout; 'wizard'/'split' have no |
273 | 291 | // 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 | + } |
275 | 307 | } |
276 | 308 |
|
277 | 309 | if (Array.isArray(formView.subforms) && formView.subforms.length > 0) { |
@@ -314,6 +346,106 @@ export function resolveRecordFormModalLayout( |
314 | 346 | return modalSize ? { ...layout, modalSize } : layout; |
315 | 347 | } |
316 | 348 |
|
| 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 | + |
317 | 449 | /** |
318 | 450 | * Result of the post-create-save navigation decision (objectstack-ai/objectstack#2604 save invariant: |
319 | 451 | * *create takes you to the record you made*). `kind: 'none'` means stay put |
|
0 commit comments