Skip to content

Commit

Permalink
Merge pull request #15549 from storybookjs/fix/controls-color-matcher
Browse files Browse the repository at this point in the history
Controls: Fix color matching behavior for non-string types
  • Loading branch information
shilman committed Jul 13, 2021
2 parents 5085073 + 2ac6d8a commit 49e1327
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
56 changes: 33 additions & 23 deletions lib/client-api/src/inferControls.test.ts
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
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

0 comments on commit 49e1327

Please sign in to comment.