UNPKG

3.11 kBJavaScriptView Raw
1"use strict";
2
3require("core-js/modules/es.function.name.js");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.inferControls = void 0;
9
10var _mapValues = _interopRequireDefault(require("lodash/mapValues"));
11
12var _parameters = require("./parameters");
13
14var _filterArgTypes = require("./filterArgTypes");
15
16function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
18var inferControl = function inferControl(argType, name, matchers) {
19 var type = argType.type,
20 options = argType.options;
21
22 if (!type && !options) {
23 return undefined;
24 } // args that end with background or color e.g. iconColor
25
26
27 if (matchers.color && matchers.color.test(name)) {
28 return {
29 control: {
30 type: 'color'
31 }
32 };
33 } // args that end with date e.g. purchaseDate
34
35
36 if (matchers.date && matchers.date.test(name)) {
37 return {
38 control: {
39 type: 'date'
40 }
41 };
42 }
43
44 switch (type.name) {
45 case 'array':
46 return {
47 control: {
48 type: 'object'
49 }
50 };
51
52 case 'boolean':
53 return {
54 control: {
55 type: 'boolean'
56 }
57 };
58
59 case 'string':
60 return {
61 control: {
62 type: 'text'
63 }
64 };
65
66 case 'number':
67 return {
68 control: {
69 type: 'number'
70 }
71 };
72
73 case 'enum':
74 {
75 var _ref = type,
76 value = _ref.value;
77 return {
78 control: {
79 type: (value === null || value === void 0 ? void 0 : value.length) <= 5 ? 'radio' : 'select'
80 },
81 options: value
82 };
83 }
84
85 case 'function':
86 case 'symbol':
87 case 'void':
88 return null;
89
90 default:
91 return {
92 control: {
93 type: options ? 'select' : 'object'
94 }
95 };
96 }
97};
98
99var inferControls = function inferControls(context) {
100 var _context$parameters = context.parameters,
101 __isArgsStory = _context$parameters.__isArgsStory,
102 argTypes = _context$parameters.argTypes,
103 _context$parameters$c = _context$parameters.controls;
104 _context$parameters$c = _context$parameters$c === void 0 ? {} : _context$parameters$c;
105 var _context$parameters$c2 = _context$parameters$c.include,
106 include = _context$parameters$c2 === void 0 ? null : _context$parameters$c2,
107 _context$parameters$c3 = _context$parameters$c.exclude,
108 exclude = _context$parameters$c3 === void 0 ? null : _context$parameters$c3,
109 _context$parameters$c4 = _context$parameters$c.matchers,
110 matchers = _context$parameters$c4 === void 0 ? {} : _context$parameters$c4;
111 if (!__isArgsStory) return argTypes;
112 var filteredArgTypes = (0, _filterArgTypes.filterArgTypes)(argTypes, include, exclude);
113 var withControls = (0, _mapValues.default)(filteredArgTypes, function (argType, name) {
114 return (argType === null || argType === void 0 ? void 0 : argType.type) && inferControl(argType, name, matchers);
115 });
116 return (0, _parameters.combineParameters)(withControls, filteredArgTypes);
117};
118
119exports.inferControls = inferControls;
\No newline at end of file