UNPKG

@opensig/opendesign

Version:

95 lines (94 loc) 2.83 kB
import { defineComponent, inject, ref, watch, onUnmounted, createElementBlock, openBlock, Fragment, renderList, unref, normalizeStyle, markRaw } from "vue"; import { useResizeObserver } from "../hooks/use-resize-observer.mjs"; import { isNil } from "../_utils/is.mjs"; import { debounceRAF } from "../_utils/helper.mjs"; import "../hooks/use-theme.mjs"; import "../_utils/global.mjs"; import "@vueuse/core"; import { dataTableInjectKey } from "./provide.mjs"; const _sfc_main = /* @__PURE__ */ defineComponent({ __name: "TableColGroup", setup(__props) { const dataTableInjection = inject(dataTableInjectKey); const getPropWidth = (column) => { return { minWidth: !isNil(column._minWidth) ? `${column._minWidth}px` : column._minWidth, maxWidth: !isNil(column._maxWidth) ? `${column._maxWidth}px` : column._maxWidth }; }; const colgroupRef = ref(); const setColRef = async (col, column) => { if (!col) { return; } column.colRef = markRaw(col); }; const resizeObserver = useResizeObserver(); const resizeHandler = debounceRAF(() => { if (!colgroupRef.value || !dataTableInjection) { return; } dataTableInjection.dataColumns.value.forEach((column) => { if (!column.colRef) { return; } column.resizeWidth = column.colRef.getBoundingClientRect().width; }); }); watch( colgroupRef, (newVal, oldVal) => { if (oldVal) { resizeObserver.unobserve(oldVal, resizeHandler); } if (newVal) { resizeHandler(); resizeObserver.observe(newVal, resizeHandler); } }, { immediate: true } ); onUnmounted(() => { if (colgroupRef.value) { resizeObserver.unobserve(colgroupRef.value, resizeHandler); } }); return (_ctx, _cache) => { var _a; return openBlock(), createElementBlock( "colgroup", { ref_key: "colgroupRef", ref: colgroupRef }, [ (openBlock(true), createElementBlock( Fragment, null, renderList((_a = unref(dataTableInjection)) == null ? void 0 : _a.dataColumns.value, (column) => { return openBlock(), createElementBlock( "col", { key: column.key, ref_for: true, ref: (el) => setColRef(el, column), style: normalizeStyle(getPropWidth(column)) }, null, 4 /* STYLE */ ); }), 128 /* KEYED_FRAGMENT */ )) ], 512 /* NEED_PATCH */ ); }; } }); export { _sfc_main as default };