An open source design system that's fully customizable and agent ready
TypeScript
fix(docsite): bridge Selector's onChange preview even without a seeded value (#6035)
Cindy Zhangd043454d5fe3
diff --git a/apps/docsite/src/__tests__/component-preview-state.test.ts b/apps/docsite/src/__tests__/component-preview-state.test.tsindex b6fd1bb6f7..104c6b5789 100644--- a/apps/docsite/src/__tests__/component-preview-state.test.ts+++ b/apps/docsite/src/__tests__/component-preview-state.test.ts@@ -444,6 +444,33 @@ describe('component detail preview state', () => {expect(onPropChange).toHaveBeenCalledWith('value', 42);});+ it('bridges Selector onChange even though its optional value prop is not seeded (#5909 follow-up)', () => {+ const knobs = pickPrimaryProps('Selector', [+ prop({name: 'label', type: 'string', required: true}),+ prop({name: 'options', type: 'SelectorOption[]', required: true}),+ prop({name: 'value', type: 'string'}),+ prop({name: 'onChange', type: '(value: string) => void'}),+ ]);++ const state = buildInitialState(knobs, {+ defaults: {+ label: 'Fruit',+ options: [+ {value: 'apple', label: 'Apple'},+ {value: 'orange', label: 'Orange'},+ ],+ },+ });+ expect(state.value).toBeUndefined();+ expect(getMissingRequiredProps(knobs, state)).toEqual([]);++ const onPropChange = vi.fn();+ const runtimeState = buildRuntimePreviewState(state, onPropChange, {knobs});++ (runtimeState.onChange as (value: string) => void)('orange');+ expect(onPropChange).toHaveBeenCalledWith('value', 'orange');+ });+it('bridges a Tokenizer removal back to its controlled value', () => {const knobs = pickPrimaryProps('Tokenizer', [prop({name: 'label', type: 'string', required: true}),diff --git a/apps/docsite/src/components/component-detail/interactiveState.ts b/apps/docsite/src/components/component-detail/interactiveState.tsindex 26f59b1e9e..a4b58f0393 100644--- a/apps/docsite/src/components/component-detail/interactiveState.ts+++ b/apps/docsite/src/components/component-detail/interactiveState.ts@@ -293,26 +293,35 @@ function getCallbackTargetProp(type: string): string | null {/*** The state prop a change handler writes back to, or `null` when the preview* cannot tell. Prefers the first parameter's name; for `onChange` falls back- * to the controlled `value` prop, whose payload it carries by convention.+ * to the controlled `value` prop, whose payload it carries by convention. The+ * primary `onChange` pair may target an optional, unseeded value prop; secondary+ * change handlers still require their target to be present in preview state.*/function resolveChangeTarget(name: string,type: string,state: Record<string, unknown>,+ knownProps: ReadonlySet<string>,): string | null {const named = getCallbackTargetProp(type);- if (named != null && named in state) {+ if (+ named != null &&+ (named in state || (name === 'onChange' && knownProps.has(named)))+ ) {return named;}- return name === 'onChange' && 'value' in state ? 'value' : null;+ return name === 'onChange' && ('value' in state || knownProps.has('value'))+ ? 'value'+ : null;}/*** Wires controlled-component change handlers back into playground state so the* preview reflects interaction (clicking a Pagination page, etc.). A callback- * whose first parameter names a value prop in `state` (page/onChange,- * value/onChange, pageSize/onPageSizeChange) replaces its noop with one that- * updates that prop. isOpen/onOpenChange stays gated behind canControlOpenState.+ * whose first parameter names a value prop in preview state (page/onChange,+ * pageSize/onPageSizeChange), or the primary `value` prop for literal+ * `onChange`, replaces its noop with one that updates that prop.+ * isOpen/onOpenChange stays gated behind canControlOpenState.** `onChange` is the exception to the name match: it conventionally documents* its first parameter after the payload it carries (`checked`, `items`,@@ -330,10 +339,12 @@ export function buildRuntimePreviewState(return state;}+ const knobs = options?.knobs ?? [];+ const knownProps = new Set(knobs.map(knob => knob.row.name));const next: Record<string, unknown> = {...state};let changed = false;- for (const {row, control} of options?.knobs ?? []) {+ for (const {row, control} of knobs) {if (control.kind !== 'callback') {continue;}@@ -342,7 +353,7 @@ export function buildRuntimePreviewState(if (!/^on[A-Z].*Change$/.test(row.name) && row.name !== 'onChange') {continue;}- const target = resolveChangeTarget(row.name, row.type, state);+ const target = resolveChangeTarget(row.name, row.type, state, knownProps);if (target == null) {continue;}