@sit-onyx/storybook-utils 1.0.0-beta.53 → 1.0.0-beta.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/sbType.ts +70 -1
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sit-onyx/storybook-utils",
|
|
3
3
|
"description": "Storybook utilities for Vue",
|
|
4
|
-
"version": "1.0.0-beta.
|
|
4
|
+
"version": "1.0.0-beta.54",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "Schwarz IT KG",
|
|
7
7
|
"license": "Apache-2.0",
|
|
@@ -27,13 +27,13 @@
|
|
|
27
27
|
"storybook": ">= 8.3.0",
|
|
28
28
|
"storybook-dark-mode": ">= 4",
|
|
29
29
|
"@sit-onyx/icons": "^1.0.0-beta.5",
|
|
30
|
-
"sit-onyx": "^1.0.0-beta.
|
|
30
|
+
"sit-onyx": "^1.0.0-beta.48"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"deepmerge-ts": "^7.1.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"vue": "
|
|
36
|
+
"vue": "3.5.10"
|
|
37
37
|
},
|
|
38
38
|
"scripts": {
|
|
39
39
|
"build": "tsc --noEmit",
|
package/src/sbType.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
ArgTypesEnhancer,
|
|
3
|
+
InputType,
|
|
4
|
+
SBType,
|
|
5
|
+
StrictInputType,
|
|
6
|
+
} from "storybook/internal/types";
|
|
2
7
|
|
|
3
8
|
/**
|
|
4
9
|
* Call a function `cb` for every type node in the storybook type tree.
|
|
@@ -33,3 +38,67 @@ export const walkTree = <TValue>(
|
|
|
33
38
|
);
|
|
34
39
|
}
|
|
35
40
|
};
|
|
41
|
+
|
|
42
|
+
const SB_TYPE_CONTROL_MAP: Partial<Record<SBType["name"], InputType["control"]>> = {
|
|
43
|
+
boolean: { type: "boolean" },
|
|
44
|
+
string: { type: "text" },
|
|
45
|
+
number: { type: "number" },
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const getFormInjectedParent = (symbol: string, inputType?: StrictInputType) => {
|
|
49
|
+
if (!inputType?.type || inputType.table?.defaultValue?.summary !== symbol) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return walkTree(inputType.type, (elem, parent) =>
|
|
54
|
+
elem.name === "symbol" || (elem.name === "other" && elem.value === "unique symbol")
|
|
55
|
+
? parent
|
|
56
|
+
: undefined,
|
|
57
|
+
);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Can be used to create an `ArgTypesEnhancer` which matches a Symbol that is used as default Prop.
|
|
62
|
+
* When it matches the passed description text will be set.
|
|
63
|
+
*
|
|
64
|
+
* @param symbol description of the symbol that should be matched.
|
|
65
|
+
* @param description the description text that should be shown in Storybook for this prop.
|
|
66
|
+
* @returns An `ArgTypesEnhancer` which can be passed to storybook.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* import { createSymbolArgTypeEnhancer } from "@sit-onyx/storybook-utils";
|
|
71
|
+
*
|
|
72
|
+
* export const enhanceFormInjectedSymbol = createSymbolArgTypeEnhancer(
|
|
73
|
+
* "FORM_INJECTED_SYMBOL",
|
|
74
|
+
* "If no value (or `undefined`) is provided, `FORM_INJECTED_SYMBOL` is the internal default value for this prop.\n" +
|
|
75
|
+
* "In that case the props value will be derived from it's parent form (if it exists).\n",
|
|
76
|
+
* );
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export const createSymbolArgTypeEnhancer = (
|
|
80
|
+
symbol: string,
|
|
81
|
+
description: string,
|
|
82
|
+
): ArgTypesEnhancer => {
|
|
83
|
+
return (context) => {
|
|
84
|
+
Object.values(context.argTypes)
|
|
85
|
+
.map((argType) => {
|
|
86
|
+
const parent = getFormInjectedParent(symbol, argType);
|
|
87
|
+
return { argType, parent };
|
|
88
|
+
})
|
|
89
|
+
.filter(({ parent }) => parent)
|
|
90
|
+
.forEach(({ argType, parent }) => {
|
|
91
|
+
const firstAvailableControl = walkTree(
|
|
92
|
+
parent || argType.type!,
|
|
93
|
+
(sb) => SB_TYPE_CONTROL_MAP[sb.name],
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
if (firstAvailableControl && argType.table?.defaultValue) {
|
|
97
|
+
argType.control = firstAvailableControl;
|
|
98
|
+
argType.table.defaultValue.detail = description;
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
return context.argTypes;
|
|
103
|
+
};
|
|
104
|
+
};
|