提交 | 用户 | age
|
2f6253
|
1 |
import { Slots } from 'vue'; |
陈 |
2 |
import { isFunction } from '/@/utils/is'; |
a065de
|
3 |
import { RenderOpts } from '/@/components/Form'; |
2f6253
|
4 |
|
陈 |
5 |
/** |
|
6 |
* @description: Get slot to prevent empty error |
|
7 |
*/ |
a065de
|
8 |
export function getSlot(slots: Slots, slot = 'default', data?: any, opts?: RenderOpts) { |
2f6253
|
9 |
if (!slots || !Reflect.has(slots, slot)) { |
陈 |
10 |
return null; |
|
11 |
} |
|
12 |
if (!isFunction(slots[slot])) { |
|
13 |
console.error(`${slot} is not a function!`); |
|
14 |
return null; |
|
15 |
} |
|
16 |
const slotFn = slots[slot]; |
|
17 |
if (!slotFn) return null; |
a065de
|
18 |
const params = { ...data, ...opts }; |
L |
19 |
return slotFn(params); |
2f6253
|
20 |
} |
陈 |
21 |
|
|
22 |
/** |
|
23 |
* extends slots |
|
24 |
* @param slots |
ecfb70
|
25 |
* @param excludeKeys |
2f6253
|
26 |
*/ |
陈 |
27 |
export function extendSlots(slots: Slots, excludeKeys: string[] = []) { |
|
28 |
const slotKeys = Object.keys(slots); |
|
29 |
const ret: any = {}; |
|
30 |
slotKeys.map((key) => { |
|
31 |
if (excludeKeys.includes(key)) { |
|
32 |
return null; |
|
33 |
} |
266c33
|
34 |
ret[key] = (data?: any) => getSlot(slots, key, data); |
2f6253
|
35 |
}); |
陈 |
36 |
return ret; |
|
37 |
} |