Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controls: Fix color matching behavior for non-string types #15549

Merged
merged 1 commit into from
Jul 13, 2021
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
56 changes: 33 additions & 23 deletions lib/client-api/src/inferControls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ describe('inferControls', () => {
warnSpy.mockRestore();
});

it('should return color type when matching color', () => {
it('should return color type when using color matcher', () => {
// passing a string, should return control type color
const inferredControls = inferControls(
getStoryContext({
argTypes: {
background: {
type: {
name: 'string',
value: 'red',
},
name: 'background',
},
Expand All @@ -55,31 +54,42 @@ describe('inferControls', () => {
expect(inferredControls.background.control.type).toEqual('color');
});

it('should return inferred type when matches color but arg is not a string', () => {
// passing an object which is unsupported, should infer the type to object
const inferredControls = inferControls(
getStoryContext({
argTypes: {
background: {
type: {
name: 'object',
value: {
rgb: [255, 255, 0],
},
},
name: 'background',
it('should return inferred type when using color matcher but arg passed is not a string', () => {
const sampleTypes = [
{
name: 'object',
value: {
rgb: {
name: 'number',
},
},
controls: {
matchers: {
color: /background/,
},
{ name: 'number' },
{ name: 'boolean' },
];

sampleTypes.forEach((type) => {
const inferredControls = inferControls(
getStoryContext({
argTypes: {
background: {
// passing an object which is unsupported
// should ignore color control and infer the type instead
type,
name: 'background',
},
},
},
})
);
controls: {
matchers: {
color: /background/,
},
},
})
);

expect(warnSpy).toHaveBeenCalled();
expect(inferredControls.background.control.type).toEqual('object');
expect(warnSpy).toHaveBeenCalled();
expect(inferredControls.background.control.type).toEqual(type.name);
});
});
});

Expand Down
3 changes: 2 additions & 1 deletion lib/client-api/src/inferControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const inferControl = (argType: ArgType, name: string, matchers: ControlsMatchers

// args that end with background or color e.g. iconColor
if (matchers.color && matchers.color.test(name)) {
const controlType = typeof argType.type.value;
const controlType = argType.type.name;

if (controlType === 'string') {
return { control: { type: 'color' } };
}
Expand Down