(
+ ...args: IComponentOptions
): DefineComponent {
+ const [options, styles = []] = args;
insertCss(styles);
return defineComponent(options as any);
}
+
+/**
+ * - Props 类型
+ * - Attar props 外的类型,如原生 div 的一些属性
+ * @param args
+ * @returns
+ */
+export function componentV2(
+ ...args: IComponentOptions
+) {
+ return component(
+ // @ts-ignore
+ ...args,
+ );
+}
diff --git a/src/shared/types/common.ts b/src/shared/types/common.ts
index 06e82c4..8b70c10 100644
--- a/src/shared/types/common.ts
+++ b/src/shared/types/common.ts
@@ -1,3 +1,5 @@
+import { PropType } from 'vue';
+
export type ISize = 'xs' | 'sm' | 'md' | 'lg';
export type IStateColor = 'info' | 'success' | 'warning' | 'error';
@@ -12,3 +14,18 @@ export type IColorType = IBrandColor | IStateColor;
export type BoolConstructorToBase = {
[k in keyof T]: T[k] extends BooleanConstructor ? boolean : T[k];
};
+
+type ExtractFromPropType = T extends PropType ? U : T;
+
+/**
+ * 从 props 对象类型中反推出类型
+ */
+export type ExtractFromProps = {
+ [k in keyof T]?: ExtractFromPropType<
+ T[k] extends {
+ type: any;
+ }
+ ? T[k]['type']
+ : T[k]
+ >;
+};
diff --git a/src/shared/utils.ts b/src/shared/utils.ts
index 823d38d..e28baea 100644
--- a/src/shared/utils.ts
+++ b/src/shared/utils.ts
@@ -1,6 +1,8 @@
+import { VNode } from 'vue';
+
export function cssUnit(unit: string | number) {
- if (typeof unit === 'number' && Number.isFinite(unit)) {
- return unit + 'px';
+ if (typeof unit === 'number') {
+ return (unit || 0) + 'px';
}
return unit || '';
}
@@ -12,3 +14,18 @@ export function isBool(v: any): boolean {
export function isUndefined(v: any): boolean {
return typeof v === 'undefined';
}
+
+export function addClass(str: string, newClass: string | string[]) {
+ const arr = Array.isArray(newClass) ? newClass : [newClass];
+ const classes = new Set(str.split(' '));
+ arr.forEach((c) => {
+ !classes.has(c) && classes.add(c);
+ });
+
+ return Array.from(classes).join(' ');
+}
+
+export function removeClass(str: string, removeClass: string) {
+ const classes = str.split(' ');
+ return classes.filter((c) => c !== removeClass).join(' ');
+}