UNPKG

@opentiny/vue-renderless

Version:

An enterprise-class UI component library, support both Vue.js 2 and Vue.js 3, as well as PC and mobile.

135 lines (134 loc) 4.13 kB
import { __objRest, __spreadProps, __spreadValues } from "../chunk-G2ADBYYC.js"; const api = [ "state", "resolvedComponent", "mergedProps", "listeners", "slotNames", "hasScopedDefault", "focus", "blur" ]; const renderless = (props, { reactive, computed, useAttrs }, { constants, vm, components }) => { const api2 = {}; const resolvedComponent = computed(() => computedResolvedComponent({ props, constants, vm, components })); const mergedProps = computed(() => { const runtimeAttrs = typeof useAttrs === "function" ? useAttrs() : null; const attrs = runtimeAttrs || vm.$attrs || {}; const className = attrs.class; const classArray = Array.isArray(className) ? ["tiny-select", ...className] : className ? ["tiny-select", className] : ["tiny-select"]; const _a = attrs, { class: _omitClass } = _a, restAttrs = __objRest(_a, ["class"]); return __spreadProps(__spreadValues(__spreadValues({}, props), restAttrs), { class: Array.from(new Set(classArray)), dataTag: "tiny-select" }); }); const slotNames = computed(() => Object.keys(vm.$slots || {}).filter((name) => name && name !== "default")); const hasScopedDefault = computed(() => { var _a, _b; const scoped = (_a = vm.$scopedSlots) == null ? void 0 : _a.default; if (scoped && scoped.length) { return true; } const slot = (_b = vm.$slots) == null ? void 0 : _b.default; return typeof slot === "function" && slot.length > 0; }); const listeners = computed(() => { if (vm.$listeners) { return vm.$listeners; } return {}; }); const getChildComponent = () => { var _a; return (_a = vm.$refs) == null ? void 0 : _a.childComponent; }; const state = new Proxy( {}, { get(target, prop) { var _a; const child = getChildComponent(); return (_a = child == null ? void 0 : child.state) == null ? void 0 : _a[prop]; }, set(target, prop, value) { const child = getChildComponent(); if (child == null ? void 0 : child.state) { child.state[prop] = value; return true; } return false; }, has(target, prop) { const child = getChildComponent(); return prop in ((child == null ? void 0 : child.state) || {}); }, ownKeys(target) { const child = getChildComponent(); return Object.keys((child == null ? void 0 : child.state) || {}); }, getOwnPropertyDescriptor(target, prop) { const child = getChildComponent(); const childState = (child == null ? void 0 : child.state) || {}; if (prop in childState) { return { enumerable: true, configurable: true, value: childState[prop] }; } return void 0; } } ); const focus = () => { const child = getChildComponent(); child && typeof child.focus === "function" && child.focus(); }; const blur = () => { const child = getChildComponent(); child && typeof child.blur === "function" && child.blur(); }; const hasData = (value) => { if (!value) { return false; } if (Array.isArray(value)) { return value.length > 0; } if (typeof value === "object") { return Object.keys(value).length > 0; } return true; }; const computedResolvedComponent = ({ props: props2, constants: constants2, vm: vm2, components: components2 }) => { var _a; const comps = components2 || ((_a = vm2.$options) == null ? void 0 : _a.components) || {}; if (props2.renderType === constants2.TYPE.Tree || hasData(props2.treeOp)) { return comps.TinyTreeSelect || "TinyTreeSelect"; } if (props2.renderType === constants2.TYPE.Grid || hasData(props2.gridOp)) { return comps.TinyGridSelect || "TinyGridSelect"; } return comps.TinyBaseSelect || "TinyBaseSelect"; }; Object.assign(api2, { state, resolvedComponent, mergedProps, listeners, slotNames, hasScopedDefault, focus, blur }); return api2; }; export { api, renderless };