Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .changeset/36923-standalone-form-lookupautofill-refused.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
'@object-ui/fields': patch
---

fix(fields): a `lookupAutofill` written by hand on a standalone form node's field is not run

Clause-②: no (narrowing)
Metadata-protocol: none

<!-- adr-0087: not-required (no-migration-prescription) No schema key, accepted value or metadata type is added, removed or renamed, and nothing stored is rewritten: `lookupAutofill` on a form field was never in a declared accept set. What narrows is the runtime behaviour of @object-ui/fields (`readLookupAutofillTargets`, and `LookupField` handed `onAutofill`), whose consumer is code, not metadata an upgrade could rewrite. This body does carry a code-level prescription (the FROM, TO and Fix lines below, written without an arrow), so the gate admits this marker on a detector miss, not on a checked fact: no fact-based category covers a runtime function whose behaviour narrows (`runtime-interface-only` reads exported types, `runtime-export-retired` reads an export that is gone). The vocabulary gap is #37136. -->

`lookupAutofill` is a fact about the object, and it runs where the object is read: in a record form and in a line-item grid column. A lookup on a standalone `form` node whose field carries `lookupAutofill` written by hand now fills nothing when a record is picked. The lookup itself is still set. Before, such a pick copied values into the other fields without the cross-currency rule, so an amount could be copied into a field in another currency with no note. A development build now warns once, naming the field and saying where the key is honoured.

Record forms and grid columns behave as before, the cross-currency rule included. The widget recognises a record form by the stamp the record form puts on its lookups (`withLookupAutofillTargets`). That stamp is now written on every lookup that declares pairs, including when no target is a field of the loaded object, and only a stamp the record form issued counts. A `lookupAutofillTargets` key written as data is ignored.

No metadata key is added or changed. Fix: declare `lookupAutofill` on the object's lookup field and draw the object's form.

**BREAKING** (`@object-ui/fields`, the `lookup` and `master_detail` widgets): `LookupField` handed an `onAutofill` writer runs its field's `lookupAutofill` pairs only when the record form stamped the field.

- FROM: a single-value pick, and the opening fill of a create form that opened with the lookup set, filled the declared targets whenever the host handed `onAutofill` (and no `onSelectRecord`) and the field carried `lookupAutofill` pairs.
- TO: it also needs the stamp `withLookupAutofillTargets` issues, which the record form applies to every field it draws. A host that hands `onAutofill` without that stamp, such as a standalone `form` node, sets the lookup and fills nothing, on a pick and on the opening fill alike, and a development build warns once per field.
- Fix: stamp the field through `withLookupAutofillTargets(field, objectFields)` before handing it to the widget, passing the object's field map as the record form does. With no map the stamp names no target, so the pick copies without the cross-currency rule, which is the copy this change stops. Or declare `lookupAutofill` on the object field and draw the object's record form.

**BREAKING** (`@object-ui/fields`, runtime export): `readLookupAutofillTargets(field)` now answers only for a target map that `withLookupAutofillTargets` issued.

- FROM: `readLookupAutofillTargets(field)` returned any non-array object found under the field's `lookupAutofillTargets` key.
- TO: it returns only the map `withLookupAutofillTargets` stamped, which is what the record form does. A same-name key written by hand reads `undefined`. `withLookupAutofillTargets` now also stamps a lookup whose pairs name no field of the map it is given, with an empty map. Before, it returned the field unchanged. The map it stamps is frozen, and a field with pairs always comes back as a copy.
- Fix: stamp the field through `withLookupAutofillTargets`, as the record form does. If you need the raw value, read `field.lookupAutofillTargets` directly.
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,10 @@ const LEDGER: Readonly<Record<string, LedgerRow>> = {
lookupAutofill: {
readers: [LOOKUP],
why: 'RUNTIME-ONLY (objectstack#28245) — object-field metadata (the spec\'s `FieldSchema.lookupAutofill`) read off the '
+ 'resolved `field` stash: which fields a pick fills is a fact about the object, not about one form.',
+ 'resolved `field` stash: which fields a pick fills is a fact about the object, not about one form. Honoured only '
+ 'in a record form, which stamps the lookup (`withLookupAutofillTargets`), and in a line-item grid column; the key '
+ 'written on a standalone form node\'s field is read and not executed (the pick fills nothing, and a development '
+ 'build names the field), per ruling 7138010812.',
},
recordBeingEdited: {
readers: ['entry@fields/src/widgets/lookupRecordReference.ts'],
Expand Down
52 changes: 44 additions & 8 deletions ui/packages/fields/src/widgets/LookupField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ import { ScanFieldButton, useFieldScanOffered } from './ScanFieldButton.js';
/** The record a host that edits none hands `{ $record }` resolution: nothing to read. */
const NO_EDITED_RECORD: Readonly<Record<string, unknown>> = Object.freeze({});

const warnedAutofillRefused = new Set<string>();

/**
* The development-build warning for a `lookupAutofill` this host does not
* run, said once per field: the pick then fills no other field, and the
* warning says why, so the author who wrote the key on a standalone form
* finds where it is honoured.
*/
function warnAutofillRefused(field: string): void {
if ((globalThis as { process?: { env?: { NODE_ENV?: string } } }).process?.env?.NODE_ENV === 'production') return;
if (warnedAutofillRefused.has(field)) return;
warnedAutofillRefused.add(field);
console.warn(
`[LookupField] lookupAutofill on "${field || '(unnamed)'}" is not executed here: a pick fills other fields only `
+ 'in a record form (the object\'s own lookup field) and in a line-item grid column. Declare lookupAutofill on '
+ 'the object field and draw the object\'s form; on a standalone form node the key is ignored.',
);
}

export interface LookupOption {
value: string | number;
label: string;
Expand Down Expand Up @@ -1222,9 +1241,24 @@ export function LookupField({ value, onChange, field, readonly, error: fieldErro
* no `onSelectRecord` and supplies `onAutofill`, the writer for the sibling
* fields of the record being edited. A host that owns the pick (the line-item
* grid) applies the pairs itself.
*
* The pairs are an object fact, honoured where the object is read: a record
* form says so by the stamp it puts on every lookup declaring them
* (`withLookupAutofillTargets`, read by `readLookupAutofillTargets`). A
* lookup without that stamp — a standalone `form` node whose field carries
* the key written by hand — fills nothing, and a development build names
* the field once.
*/
const autofillPairs: readonly LookupAutofillPair[] | undefined =
const declaredPairs: readonly LookupAutofillPair[] | undefined =
!multiple && !onSelectRecord && props.onAutofill ? fieldMeta?.lookupAutofill : undefined;
const autofillTargets = declaredPairs?.length ? readLookupAutofillTargets(fieldMeta) : undefined;
const autofillPairs: readonly LookupAutofillPair[] | undefined = autofillTargets ? declaredPairs : undefined;
const refusedAutofillField = declaredPairs?.length && !autofillTargets
? String(props.name || lookupField?.name || '')
: undefined;
useEffect(() => {
if (refusedAutofillField !== undefined) warnAutofillRefused(refusedAutofillField);
}, [refusedAutofillField]);
const onAutofill = props.onAutofill;

/**
Expand All @@ -1238,10 +1272,12 @@ export function LookupField({ value, onChange, field, readonly, error: fieldErro
* for the parent the record holds, as the amount box itself does.
*/
const { currency: homeCurrency } = useLocalization();
const autofillTargets = autofillPairs?.length ? readLookupAutofillTargets(fieldMeta) : undefined;
// A stamp that names no target (the schema had not loaded, or no target is
// a field of the object) judges no currency: every value copies as before.
const currencyTargets = autofillTargets && Object.keys(autofillTargets).length > 0 ? autofillTargets : undefined;
const followingTarget = useMemo(
() => (autofillTargets ? Object.values(autofillTargets).find((d) => recordCurrencyPath(d as never)) : undefined),
[autofillTargets],
() => (currencyTargets ? Object.values(currencyTargets).find((d) => recordCurrencyPath(d as never)) : undefined),
[currencyTargets],
);
const { record: editedRecordForCurrency, state: followedParentState } = useFollowedParentRead(
followingTarget as never,
Expand All @@ -1254,25 +1290,25 @@ export function LookupField({ value, onChange, field, readonly, error: fieldErro
// (`targetCurrencyUnread`).
const followedParentPending = followedParentState === 'pending';
const followedParentUnread = followedParentState === 'unread';
const autofillCurrency = !!autofillTargets;
const autofillCurrency = !!currencyTargets;
/** The currency context of a pick of `row` (`crossCurrencyRefusal`), or none without stamped targets. */
const currencyFor = useCallback(
(row: Record<string, unknown>): AutofillCurrencyContext | undefined => {
if (!autofillTargets) return undefined;
if (!currencyTargets) return undefined;
const sourceFields = (refObjectSchema as { fields?: Record<string, unknown> } | null)?.fields;
return {
home: homeCurrency,
sourceCurrency: (source, v) => pickedAmountCurrencyOf(sourceFields?.[source] as never, v, row),
targetCurrency: (target, writes) => {
const def = autofillTargets[target];
const def = currencyTargets[target];
if (followedParentUnread && recordCurrencyPath(def as never)) {
return amountCurrencyOf(def as never) ? { kind: 'unread' } : undefined;
}
return amountCurrencyOf(def as never, editedRecordForCurrency, writes);
},
};
},
[autofillTargets, refObjectSchema, homeCurrency, editedRecordForCurrency, followedParentUnread],
[currencyTargets, refObjectSchema, homeCurrency, editedRecordForCurrency, followedParentUnread],
);
/** What the last fill left empty, for the record it picked; said under the field. */
const [withheldFill, setWithheldFill] = useState<{ picked: string; items: readonly WithheldAutofill[] } | null>(null);
Expand Down
46 changes: 39 additions & 7 deletions ui/packages/fields/src/widgets/lookupAutofill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,40 +375,72 @@ export function withheldAutofillMessage(
* targets under, on the lookup's own field metadata: the widget receives one
* field, not the object, and a target's currency is its definition's. The
* same hand-off shape as `FOLLOWED_PARENT_OBJECT_KEY` (`currency.ts`).
*
* The stamp is also the record form's statement that the lookup is drawn
* for the record being edited, and so the one thing that lets a pick fill
* other fields at all ({@link readLookupAutofillTargets}): `lookupAutofill`
* is a fact about the object, honoured in a record form and a line-item grid
* column, and a pick in any other host fills nothing.
*
* ⛔ Only a stamp {@link withLookupAutofillTargets} issued counts: a value
* under this key that arrived as data (written by hand on a standalone form
* node's field, or anywhere else) is no stamp, whatever its shape.
*/
export const LOOKUP_AUTOFILL_TARGETS_KEY = 'lookupAutofillTargets';

/**
* Every target map {@link withLookupAutofillTargets} issued. Membership is
* what makes the stamp the record form's statement rather than a document's:
* nothing parsed from JSON is ever in it. A copy of the field def keeps the
* map it carries, so the stamp survives every spread between the form and
* the widget.
*/
const ISSUED_TARGET_STAMPS = new WeakSet<object>();

/**
* `def` with the definitions of its `lookupAutofill` targets stamped, read
* off `fields` (the edited object's field map), each carrying the parent
* object it follows when its currency follows a parent (ADR-0144 D2 as
* amended). `labels` names each field as the form shows it (translated);
* a target the form draws carries that name as its `label`, so a note about
* it names the field the way the form does. `def` itself when it declares no
* pairs or `fields` names none of its targets.
* it names the field the way the form does. Only a host that edits one
* record of the object calls this: the record form.
*
* A lookup that declares pairs is stamped even when `fields` names none of
* its targets (the schema is still loading, the form has no metadata source,
* a target is not a field of the map): the map is then empty, the pick
* copies with no currency to judge, and the stamp still says the lookup is
* drawn by a record form. `def` itself when it declares no pairs.
*/
export function withLookupAutofillTargets<T extends object>(
def: T,
fields: Readonly<Record<string, unknown>> | null | undefined,
labels?: Readonly<Record<string, string>>,
): T {
const pairs = (def as { lookupAutofill?: unknown }).lookupAutofill;
if (!Array.isArray(pairs) || pairs.length === 0 || !fields) return def;
if (!Array.isArray(pairs) || pairs.length === 0) return def;
const targets: Record<string, object> = {};
for (const pair of pairs as LookupAutofillPair[]) {
const target = fields[pair?.target];
const target = fields?.[pair?.target];
if (!target || typeof target !== 'object') continue;
const stamped = withFollowedParentObject(target, fields);
const shown = labels?.[pair.target];
targets[pair.target] = typeof shown === 'string' && shown !== '' ? { ...stamped, label: shown } : stamped;
}
return Object.keys(targets).length > 0 ? { ...def, [LOOKUP_AUTOFILL_TARGETS_KEY]: targets } : def;
const stamp = Object.freeze(targets);
ISSUED_TARGET_STAMPS.add(stamp);
return { ...def, [LOOKUP_AUTOFILL_TARGETS_KEY]: stamp };
}

/** The target definitions a form stamped on `field`, or `undefined`. */
/**
* The target definitions a record form stamped on `field`
* ({@link withLookupAutofillTargets}), possibly none, or `undefined` when no
* record form stamped it. A value under the key that
* {@link withLookupAutofillTargets} did not issue is no stamp.
*/
export function readLookupAutofillTargets(field: unknown): Readonly<Record<string, Record<string, unknown>>> | undefined {
const value = (field as Record<string, unknown> | null | undefined)?.[LOOKUP_AUTOFILL_TARGETS_KEY];
return value && typeof value === 'object' && !Array.isArray(value)
return value && typeof value === 'object' && ISSUED_TARGET_STAMPS.has(value)
? (value as Record<string, Record<string, unknown>>)
: undefined;
}
6 changes: 5 additions & 1 deletion ui/packages/fields/src/widgets/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,11 @@ export type FieldWidgetComponentProps<T = any> = {
* **Producer**: the form renderer, which owns the record's values.
* **Consumer**: `LookupField`, on a single-value pick whose host owns no
* `onSelectRecord` (a host that owns the pick, the line-item grid, applies
* the pairs itself). A lookup reached without it fills nothing.
* the pairs itself). A lookup reached without it fills nothing. A lookup
* reached with it fills only when the record form stamped the field
* (`withLookupAutofillTargets`, applied to every field the record form
* draws): a host that hands `onAutofill` without that stamp, such as a
* standalone `form` node, fills nothing either.
*
* `options.opening` marks the fill a create form's opening value made (see
* {@link openingValue}): the filled values join what the form opened with,
Expand Down
Loading
Loading