UNPKG

@vrx-arco/use

Version:

<p align="center"> <img src="https://vrx-arco.github.io/arco-design-pro/favicon.svg" width="200" height="250"> </p>

232 lines (227 loc) 6.36 kB
import { createSharedComposable, useBreakpoints } from '@vueuse/core'; import { computed, unref, ref, Fragment, provide, inject, toValue, onBeforeUnmount, Comment, Text } from 'vue'; // src/useShareBreakpoints/index.ts var useShareBreakpoints = () => { const _useShareBreakpoints = createSharedComposable(useBreakpoints); return _useShareBreakpoints({ sm: 576, md: 768, lg: 992, xl: 1200, xxl: 1600 }); }; // src/propsSlot/index.ts var propsSlot = (slots, props, key, slotKey) => { var _a, _b; return (_b = props[key]) != null ? _b : (_a = slots[slotKey || key]) == null ? void 0 : _a.call(slots); }; var useAutoProPagination = (originData, paginationProps) => { const defaultPageSize = computed( () => { var _a; return ((_a = paginationProps.value) == null ? void 0 : _a.defaultPageSize) || 10; } ); const total = computed(() => originData.value.length); const current = ref(1); const pageSize = ref(defaultPageSize.value || 10); const getPaginationList = () => { const pageRange = [0, 0]; pageRange[0] = (current.value - 1) * pageSize.value; pageRange[1] = pageRange[0] + pageSize.value; if (pageRange[1] > total.value) { pageRange[1] = total.value; } return originData.value.slice(...pageRange); }; const data = computed(() => getPaginationList()); const pageChange = (value) => { current.value = value; getPaginationList(); }; const pageSizeChange = (value) => { current.value = 1; pageSize.value = value; getPaginationList(); }; return { data, total, current, pageSize, pageChange, pageSizeChange }; }; // src/useProPagination/useProPagination.ts var useProPagination = (data, pagination, paginationProps) => { if (pagination.value === true) { return useAutoProPagination(data, paginationProps); } const total = computed(() => { var _a; return ((_a = pagination.value) == null ? void 0 : _a.total) || 0; }); const current = computed({ get: () => { var _a; return ((_a = pagination.value) == null ? void 0 : _a.pageNum) || 1; }, set: (value) => { if (typeof pagination.value !== "object") { return; } pagination.value.pageNum = value; } }); const pageSize = computed({ get: () => { var _a; return ((_a = pagination.value) == null ? void 0 : _a.pageSize) || 10; }, set: (value) => { if (typeof pagination.value !== "object") { return; } pagination.value.pageSize = value; } }); const pageChange = (value) => { current.value = value; }; const pageSizeChange = (value) => { current.value = 1; pageSize.value = value; }; return { total, current, pageSize, pageChange, pageSizeChange, data }; }; var useGrid = (useColumn, column, gutter) => { const getColumn = (column2) => useColumn ? Math.ceil(24 / column2) : column2; const { xxl, xl, lg, md, sm, smaller } = useShareBreakpoints(); const xs = smaller("sm"); const grid = computed(() => { const grid2 = { xxl: xxl.value, xl: xl.value, lg: lg.value, md: md.value, sm: sm.value, xs: xs.value }; return Object.keys(grid2).find((key) => grid2[key]) || "span"; }); const gridProps = computed(() => { if (typeof column.value === "number") { return { span: getColumn(column.value), gutter: unref(gutter) }; } return { gutter: unref(gutter), span: getColumn(column.value[grid.value]) || 6 }; }); return { gridProps, grid }; }; var controlVModel = (props, name, emit, init) => { const value = ref(init()); return computed({ get() { var _a; return (_a = props[name]) != null ? _a : value.value; }, set(v) { emit(`update:${name}`, v); value.value = v; } }); }; var isEmptyElement = (c) => c && (c.type === Comment || c.type === Fragment && c.children.length === 0 || c.type === Text && c.children.trim() === ""); var filterEmptyChildren = (children = []) => { const res = []; children.forEach((child) => { if (Array.isArray(child)) { res.push(...child); return; } if ((child == null ? void 0 : child.type) === Fragment) { res.push(...filterEmptyChildren(child.children)); return; } res.push(child); }); return res.filter((c) => !isEmptyElement(c)); }; var EmptyComponentContext = Symbol("EmptyComponent"); var useEmptyComponentProvide = () => { const empty = ref(false); provide(EmptyComponentContext, { empty }); return { empty }; }; var useEmptyComponentInject = () => inject(EmptyComponentContext, { empty: ref(false) }); var createTreeSelectFilterNode = (fieldNames) => { const filterTreeNode = (searchKey, nodeData) => { if (!searchKey) { return true; } const _fieldNames = toValue(fieldNames); const value = nodeData[_fieldNames.title || "title"]; return String(value).includes(searchKey); }; return { fieldNames, filterTreeNode }; }; var useRangePickerObjectValue = (model, startKey, endKey) => { return computed({ get: () => { const data = toValue(model); const start = data[startKey]; const end = data[endKey]; if (start && end) { return [start, end]; } return []; }, set: (list = []) => { const data = toValue(model); data[startKey] = list[0]; data[endKey] = list[1]; } }); }; function useAbortController(options) { const supportAbort = typeof AbortController === "function"; let contoller = null; const abort = () => { if (!supportAbort) { return; } contoller == null ? void 0 : contoller.abort(); contoller = new AbortController(); if (options == null ? void 0 : options.onabort) { contoller.signal.onabort = options.onabort; } }; abort(); onBeforeUnmount(() => { contoller == null ? void 0 : contoller.abort(); contoller = null; }); return { abort, get signal() { return contoller == null ? void 0 : contoller.signal; } }; } export { controlVModel, createTreeSelectFilterNode, filterEmptyChildren, propsSlot, useAbortController, useEmptyComponentInject, useEmptyComponentProvide, useGrid, useProPagination, useRangePickerObjectValue, useShareBreakpoints };