diff --git a/packages/opendesign/src/_components/provide.ts b/packages/opendesign/src/_components/provide.ts new file mode 100644 index 0000000000000000000000000000000000000000..b793f43238627e4b62c690873e7f7a3051051e0a --- /dev/null +++ b/packages/opendesign/src/_components/provide.ts @@ -0,0 +1,5 @@ +import { InjectionKey } from 'vue'; + +export const innerComponentInjectKey: InjectionKey<{ + isInnerInput: Boolean; +}> = Symbol('provide-inner-component'); diff --git a/packages/opendesign/src/_utils/helper.ts b/packages/opendesign/src/_utils/helper.ts index 119942b065599ba77a0eb0c4c41d72ee6c6e4750..4f8e7fdd649ea92a668d5aaeb7f8db9190be26bf 100644 --- a/packages/opendesign/src/_utils/helper.ts +++ b/packages/opendesign/src/_utils/helper.ts @@ -1,3 +1,4 @@ +import { isObject, isUndefined } from './is'; // 防抖 时间为一个一帧 export function debounceRAF) => any>(fn: T) { let handle = 0; @@ -98,3 +99,76 @@ export function chunk(arr: any[] = [], size = 1) { (v, i) => arr.slice(i * size, i * size + size) ); } + +/** + * 异步some + * @param array 遍历数组 + * @param judgeFn 判断函数 + */ +export async function asyncSome(array: Array, judgeFn: (arrayItem: T) => Promise) { + for (const iterator of array) { + try { + if (await judgeFn(iterator)) { + return true; + } + } catch (error) { + return false; + } + } + return false; +} + +/** + * 根据path从对象中获取值 + */ +export function getValueByPath(obj: object, path: string) { + if (!obj || !path) { + return; + } + + const keys = path.split('.'); + if (keys.length === 0) { + return; + } + + let temp = obj; + for (let i = 0; i < keys.length; i++) { + if (!isObject(temp)) { + return; + } + temp = temp[keys[i] as keyof typeof temp]; + if (i === keys.length - 1) { + return temp; + } + } +} + +/** + * 根据path设置对象中的值 + */ +export function setValueByPath(obj: { [k: string]: any }, path: string, value: any) { + if (!obj || !path) { + return; + } + + const keys = path.split('.'); + if (keys.length === 0) { + return; + } + + let temp = obj; + for (let i = 0; i < keys.length; i++) { + if (!isObject(temp)) { + throw new TypeError(`Cannot set properties of non-object (setting '${keys[i]}')!`); + } + const k = keys[i] as keyof typeof temp; + if (i === keys.length - 1) { + temp[k] = value; + } else { + if (isUndefined(temp[k])) { + temp[k] = Number(keys[i + 1]) ? [] : {}; + } + temp = temp[k]; + } + } +} diff --git a/packages/opendesign/src/_utils/is.ts b/packages/opendesign/src/_utils/is.ts index a956c90fac1353b619bee3f9892d00015e836c33..d16231b11c372cad196899fdaf4f7a235c07346f 100644 --- a/packages/opendesign/src/_utils/is.ts +++ b/packages/opendesign/src/_utils/is.ts @@ -27,6 +27,14 @@ export function isFunction(val: unknown): val is Function { export function isArray(val: unknown): val is Array { return Array.isArray(val); } + +export function isEmptyArray(val: unknown): val is Array { + return isArray(val) && val.length === 0; +} + +export function isEmptyObject(val: unknown): val is {} { + return opt.call(val) === '[object object]' && Object.keys(val as Object).length === 0; +} /** * 判断日期是否合法 * @param d diff --git a/packages/opendesign/src/_utils/typeHelp.ts b/packages/opendesign/src/_utils/typeHelp.ts new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/packages/opendesign/src/_utils/types.ts b/packages/opendesign/src/_utils/types.ts index d22b38dc3724ea5bfe2f5e6afbf3fa84a10362c7..83eec4249d3ec6438c32156142df4a592faf8508 100644 --- a/packages/opendesign/src/_utils/types.ts +++ b/packages/opendesign/src/_utils/types.ts @@ -37,5 +37,6 @@ export type VariantT = (typeof VariantTypes)[number]; export const ColorTypes = ['normal', 'primary', 'success', 'warning', 'danger'] as const; export type ColorT = (typeof ColorTypes)[number]; +// 颜色 表单元素 export const Color2Types = ['normal', 'success', 'warning', 'danger'] as const; export type Color2T = (typeof ColorTypes)[number]; diff --git a/packages/opendesign/src/button/OButton.vue b/packages/opendesign/src/button/OButton.vue index cb7fc5e67c97056cb8d9c2fe0ed63566dae756ac..676cbf040fd7d68abb78a6e404852524d449dfaa 100644 --- a/packages/opendesign/src/button/OButton.vue +++ b/packages/opendesign/src/button/OButton.vue @@ -5,6 +5,7 @@ import { getRoundClass } from '../_utils/style-class'; import { buttonProps } from './types'; import HtmlTag from '../_components/html-tag'; import { isEmptySlot } from '../_utils/vue-utils'; +import { computed } from 'vue'; const props = defineProps(buttonProps); @@ -12,6 +13,8 @@ const emit = defineEmits<{ (e: 'click', evt: MouseEvent): void; }>(); +const tag = computed(() => (!!props.href ? 'a' : props.tag)); + const round = getRoundClass(props, 'btn'); const onClick = (e: MouseEvent) => { @@ -23,9 +26,9 @@ const onClick = (e: MouseEvent) => {