UNPKG

yanzhen-design-vue

Version:

102 lines (101 loc) 3.48 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const vue = require("vue"); const shared = require("@vue/shared"); const utils = require("@yanzhen-libs/utils"); const lodashEs = require("lodash-es"); const utilsVue = require("@yanzhen-libs/utils-vue"); function isEmitListener(options, key) { if (!options || !shared.isOn(key)) { return; } key = key.slice(2).replace(/Once$/, ""); for (const emitKey of [key[0].toLowerCase() + key.slice(1), shared.hyphenate(key), key]) { if (shared.hasOwn(options, emitKey)) { return emitKey; } } } const useProxy = (proxyConfig) => { const instance = vue.getCurrentInstance(); const { props: proxyProps, on: proxyEvents, excludeProps } = proxyConfig || {}; return vue.computed(() => { const bindProps = Object.assign({}, instance.attrs); if (proxyProps) { Object.keys(proxyProps).forEach((propKey) => { if (proxyProps[propKey]) { bindProps[propKey] = vue.unref(proxyProps[propKey]); } }); } const rawProps = instance.vnode.props; if (rawProps) { Object.keys(rawProps).forEach((propKey) => { const matchKey = isEmitListener(instance.emitsOptions, propKey); if (matchKey) { bindProps[propKey] = (proxyEvents == null ? void 0 : proxyEvents[vue.camelize(matchKey)]) || rawProps[propKey]; } }); } Object.keys(instance.props).forEach((propKey) => { if (!(propKey in (proxyProps ?? {})) && !(excludeProps == null ? void 0 : excludeProps.includes(propKey))) { bindProps[propKey] = instance.props[propKey]; } }); return bindProps; }); }; function useProxyConfig(props, emits, context) { const _emits = vue.computed(() => { const _emitsObj = vue.unref(emits); let keys = []; if (utils.isObject(_emitsObj)) { keys = Object.keys(_emitsObj); } else if (utils.isArray(_emitsObj)) { keys = _emitsObj.filter((value) => typeof value === "string"); } const _props = vue.unref(props); const funs = lodashEs.fromPairs( Object.entries(_props).map(([k, value]) => { if (k.startsWith("on")) { const eventName = utilsVue.toHandlerKey(k); return [eventName, value]; } return [k, value]; }) ); return lodashEs.fromPairs( keys.map((key) => { const emit = context == null ? void 0 : context.emit; const k = key.startsWith("on") ? key : `on${lodashEs.upperFirst(lodashEs.camelCase(key))}`; const eventName = utilsVue.toHandlerKey(key); return [ eventName, (...args) => { if (typeof funs[eventName] === "function") { funs[eventName](...args); } else if (typeof funs[k] === "function") { funs[k](...args); } else if (typeof emit === "function") { emit(key, ...args); } } ]; }) ); }); return vue.computed(() => { let _props = vue.unref(props); const exclude = context == null ? void 0 : context.exclude; const include = context == null ? void 0 : context.include; if (exclude) { _props = lodashEs.omit(_props, ...exclude); } if (include) { _props = lodashEs.pick(_props, ...include); } return vue.mergeProps(vue.useAttrs(), _props, vue.unref(_emits)); }); } exports.useProxy = useProxy; exports.useProxyConfig = useProxyConfig;