|
| 1 | +export const api = [ |
| 2 | + 'state', |
| 3 | + 'resolvedComponent', |
| 4 | + 'mergedProps', |
| 5 | + 'listeners', |
| 6 | + 'slotNames', |
| 7 | + 'hasScopedDefault', |
| 8 | + 'focus', |
| 9 | + 'blur' |
| 10 | +] |
| 11 | + |
| 12 | +export const renderless = (props, { reactive, computed, useAttrs }, { constants, vm, components }) => { |
| 13 | + const api = {} |
| 14 | + |
| 15 | + const resolvedComponent = computed(() => computedResolvedComponent({ props, constants, vm, components })) |
| 16 | + |
| 17 | + const mergedProps = computed(() => { |
| 18 | + const runtimeAttrs = typeof useAttrs === 'function' ? useAttrs() : null |
| 19 | + const attrs = runtimeAttrs || vm.$attrs || {} |
| 20 | + const className = attrs.class |
| 21 | + const classArray = Array.isArray(className) |
| 22 | + ? ['tiny-select', ...className] |
| 23 | + : className |
| 24 | + ? ['tiny-select', className] |
| 25 | + : ['tiny-select'] |
| 26 | + |
| 27 | + const { class: _omitClass, ...restAttrs } = attrs |
| 28 | + |
| 29 | + return { |
| 30 | + ...props, |
| 31 | + ...restAttrs, |
| 32 | + class: classArray |
| 33 | + } |
| 34 | + }) |
| 35 | + |
| 36 | + const slotNames = computed(() => Object.keys(vm.$slots || {}).filter((name) => name && name !== 'default')) |
| 37 | + |
| 38 | + const hasScopedDefault = computed(() => { |
| 39 | + const scoped = vm.$scopedSlots?.default |
| 40 | + if (scoped && scoped.length) { |
| 41 | + return true |
| 42 | + } |
| 43 | + const slot = vm.$slots?.default |
| 44 | + return typeof slot === 'function' && slot.length > 0 |
| 45 | + }) |
| 46 | + |
| 47 | + const listeners = computed(() => { |
| 48 | + if (vm.$listeners) { |
| 49 | + return vm.$listeners |
| 50 | + } |
| 51 | + return {} |
| 52 | + }) |
| 53 | + |
| 54 | + const getChildComponent = () => vm.$refs?.childComponent |
| 55 | + |
| 56 | + // 暴露子组件的 state,让用户可以通过 ref.state 访问子组件的状态(如 cachedOptions) |
| 57 | + // 使用 Proxy 代理子组件的 state,实现动态访问 |
| 58 | + const state = new Proxy( |
| 59 | + {}, |
| 60 | + { |
| 61 | + get(target, prop) { |
| 62 | + const child = getChildComponent() |
| 63 | + return child?.state?.[prop] |
| 64 | + }, |
| 65 | + set(target, prop, value) { |
| 66 | + const child = getChildComponent() |
| 67 | + if (child?.state) { |
| 68 | + child.state[prop] = value |
| 69 | + return true |
| 70 | + } |
| 71 | + return false |
| 72 | + }, |
| 73 | + has(target, prop) { |
| 74 | + const child = getChildComponent() |
| 75 | + return prop in (child?.state || {}) |
| 76 | + }, |
| 77 | + ownKeys(target) { |
| 78 | + const child = getChildComponent() |
| 79 | + return Object.keys(child?.state || {}) |
| 80 | + }, |
| 81 | + getOwnPropertyDescriptor(target, prop) { |
| 82 | + const child = getChildComponent() |
| 83 | + const childState = child?.state || {} |
| 84 | + if (prop in childState) { |
| 85 | + return { |
| 86 | + enumerable: true, |
| 87 | + configurable: true, |
| 88 | + value: childState[prop] |
| 89 | + } |
| 90 | + } |
| 91 | + return undefined |
| 92 | + } |
| 93 | + } |
| 94 | + ) |
| 95 | + |
| 96 | + const focus = () => { |
| 97 | + const child = getChildComponent() |
| 98 | + child && typeof child.focus === 'function' && child.focus() |
| 99 | + } |
| 100 | + |
| 101 | + const blur = () => { |
| 102 | + const child = getChildComponent() |
| 103 | + child && typeof child.blur === 'function' && child.blur() |
| 104 | + } |
| 105 | + |
| 106 | + const hasData = (value: any) => { |
| 107 | + if (!value) { |
| 108 | + return false |
| 109 | + } |
| 110 | + if (Array.isArray(value)) { |
| 111 | + return value.length > 0 |
| 112 | + } |
| 113 | + if (typeof value === 'object') { |
| 114 | + return Object.keys(value).length > 0 |
| 115 | + } |
| 116 | + return true |
| 117 | + } |
| 118 | + |
| 119 | + const computedResolvedComponent = ({ props, constants, vm, components }) => { |
| 120 | + // 优先使用传入的 components,否则从 vm.$options.components 获取 |
| 121 | + const comps = components || vm.$options?.components || {} |
| 122 | + |
| 123 | + if (props.renderType === constants.TYPE.Tree || hasData(props.treeOp)) { |
| 124 | + return comps.TinyTreeSelect || 'TinyTreeSelect' |
| 125 | + } |
| 126 | + if (props.renderType === constants.TYPE.Grid || hasData(props.gridOp)) { |
| 127 | + return comps.TinyGridSelect || 'TinyGridSelect' |
| 128 | + } |
| 129 | + return comps.TinyBaseSelect || 'TinyBaseSelect' |
| 130 | + } |
| 131 | + Object.assign(api, { |
| 132 | + state, |
| 133 | + resolvedComponent, |
| 134 | + mergedProps, |
| 135 | + listeners, |
| 136 | + slotNames, |
| 137 | + hasScopedDefault, |
| 138 | + focus, |
| 139 | + blur |
| 140 | + }) |
| 141 | + |
| 142 | + return api |
| 143 | +} |
0 commit comments