1 | import mapValues from 'lodash/mapValues';
|
2 | import { logger } from '@storybook/client-logger';
|
3 | import { combineParameters } from './parameters';
|
4 | import { filterArgTypes } from './filterArgTypes';
|
5 |
|
6 | const inferControl = (argType, name, matchers) => {
|
7 | const {
|
8 | type,
|
9 | options
|
10 | } = argType;
|
11 |
|
12 | if (!type && !options) {
|
13 | return undefined;
|
14 | }
|
15 |
|
16 |
|
17 | if (matchers.color && matchers.color.test(name)) {
|
18 | const controlType = argType.type.name;
|
19 |
|
20 | if (controlType === 'string') {
|
21 | return {
|
22 | control: {
|
23 | type: 'color'
|
24 | }
|
25 | };
|
26 | }
|
27 |
|
28 | logger.warn(`Addon controls: Control of type color only supports string, received "${controlType}" instead`);
|
29 | }
|
30 |
|
31 |
|
32 | if (matchers.date && matchers.date.test(name)) {
|
33 | return {
|
34 | control: {
|
35 | type: 'date'
|
36 | }
|
37 | };
|
38 | }
|
39 |
|
40 | switch (type.name) {
|
41 | case 'array':
|
42 | return {
|
43 | control: {
|
44 | type: 'object'
|
45 | }
|
46 | };
|
47 |
|
48 | case 'boolean':
|
49 | return {
|
50 | control: {
|
51 | type: 'boolean'
|
52 | }
|
53 | };
|
54 |
|
55 | case 'string':
|
56 | return {
|
57 | control: {
|
58 | type: 'text'
|
59 | }
|
60 | };
|
61 |
|
62 | case 'number':
|
63 | return {
|
64 | control: {
|
65 | type: 'number'
|
66 | }
|
67 | };
|
68 |
|
69 | case 'enum':
|
70 | {
|
71 | const {
|
72 | value
|
73 | } = type;
|
74 | return {
|
75 | control: {
|
76 | type: (value === null || value === void 0 ? void 0 : value.length) <= 5 ? 'radio' : 'select'
|
77 | },
|
78 | options: value
|
79 | };
|
80 | }
|
81 |
|
82 | case 'function':
|
83 | case 'symbol':
|
84 | case 'void':
|
85 | return null;
|
86 |
|
87 | default:
|
88 | return {
|
89 | control: {
|
90 | type: options ? 'select' : 'object'
|
91 | }
|
92 | };
|
93 | }
|
94 | };
|
95 |
|
96 | export const inferControls = context => {
|
97 | const {
|
98 | __isArgsStory,
|
99 | argTypes,
|
100 | controls: {
|
101 | include = null,
|
102 | exclude = null,
|
103 | matchers = {}
|
104 | } = {}
|
105 | } = context.parameters;
|
106 | if (!__isArgsStory) return argTypes;
|
107 | const filteredArgTypes = filterArgTypes(argTypes, include, exclude);
|
108 | const withControls = mapValues(filteredArgTypes, (argType, name) => {
|
109 | return (argType === null || argType === void 0 ? void 0 : argType.type) && inferControl(argType, name, matchers);
|
110 | });
|
111 | return combineParameters(withControls, filteredArgTypes);
|
112 | }; |
\ | No newline at end of file |