UNPKG

@oruga-ui/oruga-next

Version:

UI components for Vue.js and CSS framework agnostic

84 lines (83 loc) 2.84 kB
"use strict"; /*! Oruga v0.11.4 | MIT License | github.com/oruga-ui/oruga */ const vue = require("vue"); const unrefElement = require("./unrefElement-Cr-ChCX2.cjs"); const useDebounce = require("./useDebounce-CeqYk8AA.cjs"); const useSequentialId = require("./useSequentialId-Du428Zv5.cjs"); function useProviderParent(options) { const vm = vue.getCurrentInstance(); if (!vm) throw new Error( "useProviderChild must be called within a component setup function." ); const configField = vm.proxy?.$options.configField; const key = options?.key || configField; const childItems = vue.ref([]); if (options?.rootRef) { const sortHandler = useDebounce.useDebounce((items) => { const parent = unrefElement.unrefElement(options.rootRef); if (!parent) return; const ids = items.map((item) => `[data-id="${key}-${item.identifier}"]`).join(","); if (!ids) return; const children = parent.querySelectorAll(ids); const sortedIds = Array.from(children).map( (el) => el.getAttribute("data-id")?.replace(`${key}-`, "") ); items.forEach( (item) => item.index = sortedIds.indexOf(`${item.identifier}`) ); items.sort((a, b) => a.index - b.index); }, 500); vue.watch(childItems, sortHandler); } const { nextSequence } = useSequentialId.useSequentialId(1); function registerItem(data) { const index = childItems.value.length; const identifier = nextSequence(); const item = { index, data, identifier }; childItems.value = [ ...childItems.value, item ]; return item; } function unregisterItem(item) { childItems.value = childItems.value.filter((i) => i !== item); } vue.provide("$o-" + key, { registerItem, unregisterItem, data: options?.data }); return { childItems }; } function useProviderChild(options) { options = Object.assign({ needParent: true, register: true }, options); const vm = vue.getCurrentInstance(); if (!vm) throw new Error( "useProviderChild must be called within a component setup function." ); const configField = vm.proxy?.$options.configField; const key = options?.key || configField; const parent = vue.inject("$o-" + key, void 0); if (options.needParent && !parent) throw new Error( `You should wrap ${vm.proxy?.$options.name} in a ${key} component` ); const item = vue.ref(); if (parent && options.register) item.value = parent.registerItem( options?.data ); vue.onUnmounted(() => { if (parent && item.value) parent.unregisterItem(item.value); }); const data = parent?.data || vue.ref(); return { parent: data, item }; } exports.useProviderChild = useProviderChild; exports.useProviderParent = useProviderParent; //# sourceMappingURL=useParentProvider-D71VAfNa.cjs.map