1 | import "core-js/modules/es.array.find.js";
|
2 | import "core-js/modules/es.object.to-string.js";
|
3 | import "core-js/modules/es.function.name.js";
|
4 | import "core-js/modules/web.dom-collections.for-each.js";
|
5 | import "core-js/modules/es.symbol.js";
|
6 | import "core-js/modules/es.symbol.description.js";
|
7 | import "core-js/modules/es.array.join.js";
|
8 | import "core-js/modules/es.array.filter.js";
|
9 | import "core-js/modules/es.array.map.js";
|
10 | import { logger } from '@storybook/client-logger';
|
11 |
|
12 | function hasKeyword(keyword, keywords) {
|
13 | return keywords ? keywords.find(function (k) {
|
14 | return k.name === keyword;
|
15 | }) != null : false;
|
16 | }
|
17 |
|
18 | export var extractArgTypes = function extractArgTypes(component) {
|
19 | try {
|
20 |
|
21 | var docgen = component.__docgen;
|
22 |
|
23 | if (docgen) {
|
24 | return createArgTypes(docgen);
|
25 | }
|
26 | } catch (err) {
|
27 | logger.log("Error extracting argTypes: ".concat(err));
|
28 | }
|
29 |
|
30 | return {};
|
31 | };
|
32 | export var createArgTypes = function createArgTypes(docgen) {
|
33 | var results = {};
|
34 | docgen.data.forEach(function (item) {
|
35 | var _item$type, _item$type2;
|
36 |
|
37 | results[item.name] = {
|
38 | control: parseTypeToControl(item.type),
|
39 | name: item.name,
|
40 | description: item.description,
|
41 | type: {
|
42 | required: hasKeyword('required', item.keywords),
|
43 | name: (_item$type = item.type) === null || _item$type === void 0 ? void 0 : _item$type.text
|
44 | },
|
45 | table: {
|
46 | type: {
|
47 | summary: (_item$type2 = item.type) === null || _item$type2 === void 0 ? void 0 : _item$type2.text
|
48 | },
|
49 | defaultValue: {
|
50 | summary: item.defaultValue
|
51 | },
|
52 | category: 'properties'
|
53 | }
|
54 | };
|
55 | });
|
56 | docgen.events.forEach(function (item) {
|
57 | results["event_".concat(item.name)] = {
|
58 | name: item.name,
|
59 | description: item.description,
|
60 | type: {
|
61 | name: 'other',
|
62 | value: 'void'
|
63 | },
|
64 | table: {
|
65 | category: 'events'
|
66 | }
|
67 | };
|
68 | });
|
69 | docgen.slots.forEach(function (item) {
|
70 | var _item$params;
|
71 |
|
72 | results["slot_".concat(item.name)] = {
|
73 | name: item.name,
|
74 | description: [item.description, (_item$params = item.params) === null || _item$params === void 0 ? void 0 : _item$params.map(function (p) {
|
75 | return "`".concat(p.name, "`");
|
76 | }).join(' ')].filter(function (p) {
|
77 | return p;
|
78 | }).join('\n\n'),
|
79 | type: {
|
80 | name: 'other',
|
81 | value: 'void'
|
82 | },
|
83 | table: {
|
84 | category: 'slots'
|
85 | }
|
86 | };
|
87 | });
|
88 | return results;
|
89 | };
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | var parseTypeToControl = function parseTypeToControl(type) {
|
97 | if (!type) {
|
98 | return null;
|
99 | }
|
100 |
|
101 | if (type.kind === 'type') {
|
102 | switch (type.type) {
|
103 | case 'string':
|
104 | return {
|
105 | type: 'text'
|
106 | };
|
107 |
|
108 | case 'enum':
|
109 | return {
|
110 | type: 'radio'
|
111 | };
|
112 |
|
113 | case 'any':
|
114 | return {
|
115 | type: 'object'
|
116 | };
|
117 |
|
118 | default:
|
119 | return {
|
120 | type: type.type
|
121 | };
|
122 | }
|
123 | } else if (type.kind === 'union') {
|
124 |
|
125 | if (Array.isArray(type.type) && !type.type.find(function (t) {
|
126 | return t.type !== 'string';
|
127 | })) {
|
128 | return {
|
129 | type: 'radio',
|
130 | options: type.type.filter(function (t) {
|
131 | return t.kind === 'const';
|
132 | }).map(function (t) {
|
133 | return t.value;
|
134 | })
|
135 | };
|
136 | }
|
137 | }
|
138 |
|
139 | return null;
|
140 | }; |
\ | No newline at end of file |