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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, expect, it } from 'vitest';
import { normalizeInputType, normalizeInputTypes } from './normalizeInputTypes';

describe('normalizeInputType', () => {
it('does nothing to strict types', () => {
it('normalizes strict types and sets disable: false when type is present', () => {
expect(
normalizeInputType(
{
Expand All @@ -18,12 +18,29 @@ describe('normalizeInputType', () => {
).toEqual({
name: 'name',
type: { name: 'string' },
control: { type: 'text' },
control: { type: 'text', disable: false },
description: 'description',
defaultValue: 'defaultValue',
});
});

it('preserves strict types with explicit disable', () => {
expect(
normalizeInputType(
{
name: 'name',
type: { name: 'string' },
control: { type: 'text', disable: true },
},
'arg'
)
).toEqual({
name: 'name',
type: { name: 'string' },
control: { type: 'text', disable: true },
});
});

it('fills in unstrict types', () => {
expect(
normalizeInputType(
Expand All @@ -38,12 +55,40 @@ describe('normalizeInputType', () => {
).toEqual({
name: 'arg',
type: { name: 'string' },
control: { type: 'text' },
control: { type: 'text', disable: false },
description: 'description',
defaultValue: 'defaultValue',
});
});

it('sets disable: false when control type is specified to override inherited disable', () => {
expect(
normalizeInputType(
{
control: { type: 'select' },
},
'arg'
)
).toEqual({
name: 'arg',
control: { type: 'select', disable: false },
});
});

it('preserves explicit disable: true in control object', () => {
expect(
normalizeInputType(
{
control: { type: 'select', disable: true },
},
'arg'
)
).toEqual({
name: 'arg',
control: { type: 'select', disable: true },
});
});

it('preserves disabled control via shortcut', () => {
expect(
normalizeInputType(
Expand Down
15 changes: 13 additions & 2 deletions code/core/src/preview-api/modules/store/csf/normalizeInputTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@ const normalizeType = (type: InputType['type']): StrictInputType['type'] => {
return typeof type === 'string' ? { name: type } : type;
};

const normalizeControl = (control: InputType['control']): StrictInputType['control'] =>
typeof control === 'string' ? { type: control } : control;
const normalizeControl = (control: InputType['control']): StrictInputType['control'] => {
if (typeof control === 'string') {
// When explicitly setting a control type, ensure disable is false to override
// any inherited disable: true from parent argTypes (fixes #27091)
return { type: control, disable: false };
}
// If control is an object with a type but no explicit disable, set disable: false
// to ensure it overrides any inherited disable: true
if (control && typeof control === 'object' && 'type' in control && !('disable' in control)) {
return { ...control, disable: false };
}
return control;
};

export const normalizeInputType = (inputType: InputType, key: string): StrictInputType => {
const { type, control, ...rest } = inputType;
Expand Down
Loading