UNPKG

@visactor/vue-vtable

Version:

The vue version of VTable

57 lines (54 loc) 2.02 kB
import { getCurrentInstance, computed, watchEffect, onBeforeUnmount } from 'vue'; import { isArray, isObject, isValid } from '@visactor/vutils'; import 'tslib'; import '@visactor/vtable'; import { checkRenderEditor, getRenderEditor, getRenderEditorColumnKeyField } from '../edit/util.js'; function useEditorRender(props, tableRef) { const instance = getCurrentInstance(); const validColumns = computed(() => { var _a; const columns = (_a = props.options) === null || _a === void 0 ? void 0 : _a.columns; if (!isArray(columns)) { return []; } return columns.filter(col => !!isObject(col) && !!checkRenderEditor(col)); }); watchEffect(() => { resolveRenderEditor(); }); onBeforeUnmount(() => { releaseRenderEditor(); }); function resolveRenderEditor() { const id = getTableId(); if (!isValid(id)) { return; } let renderEditor = getRenderEditor(); if (renderEditor) { renderEditor.removeNode(id); } else if (validColumns.value.length > 0) { renderEditor = getRenderEditor(true, instance === null || instance === void 0 ? void 0 : instance.appContext); } validColumns.value.forEach(column => { const { getEditCustomNode } = column; const key = getRenderEditorColumnKeyField(column); renderEditor.registerNode(id, key, getEditCustomNode); delete column.editCustomNode; }); } function releaseRenderEditor() { const id = getTableId(); if (!isValid(id)) { return; } const renderEditor = getRenderEditor(); renderEditor === null || renderEditor === void 0 ? void 0 : renderEditor.release(id); } function getTableId() { var _a; return (_a = tableRef.value) === null || _a === void 0 ? void 0 : _a.id; } } export { useEditorRender };