@fe6/water-pro
Version:
An enterprise-class UI design language and Vue-based implementation
152 lines (140 loc) • 4.9 kB
JavaScript
import _regeneratorRuntime from "@babel/runtime/regenerator";
/** @format */
import { __awaiter } from "tslib";
import { ref, onUnmounted, unref, watch, toRaw } from 'vue';
import { getDynamicProps } from '../../../_util/use';
import { isProdMode } from '../../../_util/env';
import warning from '../../../_util/warning';
export function useTable(tableProps) {
var _this = this;
var tableRef = ref(null);
var loadedRef = ref(false);
var formRef = ref(null);
function register(instance, formInstance) {
isProdMode() && onUnmounted(function () {
tableRef.value = null;
loadedRef.value = null;
});
if (unref(loadedRef) && isProdMode() && instance === unref(tableRef)) {
return;
}
tableRef.value = instance;
formRef.value = formInstance;
tableProps && instance.setProps(getDynamicProps(tableProps));
loadedRef.value = true;
watch(function () {
return tableProps;
}, function () {
tableProps && instance.setProps(getDynamicProps(tableProps));
}, {
immediate: true,
deep: true
});
}
function getTableInstance() {
var table = unref(tableRef);
if (!table) {
warning('The table instance has not been obtained yet, please make sure the table is presented when performing the table operation!');
}
return table;
}
var methods = {
reload: function reload(opt) {
return __awaiter(_this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
getTableInstance().reload(opt);
case 1:
case "end":
return _context.stop();
}
}
}, _callee);
}));
},
setProps: function setProps(props) {
getTableInstance().setProps(props);
},
redoHeight: function redoHeight() {
getTableInstance().redoHeight();
},
setLoading: function setLoading(loading) {
getTableInstance().setLoading(loading);
},
getDataSource: function getDataSource() {
return toRaw(getTableInstance().getDataSource());
},
getColumns: function getColumns() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$ignoreIndex = _ref.ignoreIndex,
ignoreIndex = _ref$ignoreIndex === void 0 ? false : _ref$ignoreIndex;
var columns = getTableInstance().getColumns({
ignoreIndex: ignoreIndex
}) || [];
return toRaw(columns);
},
setColumns: function setColumns(columns) {
getTableInstance().setColumns(columns);
},
setTableData: function setTableData(values) {
return getTableInstance().setTableData(values);
},
setPagination: function setPagination(info) {
return getTableInstance().setPagination(info);
},
deleteSelectRowByKey: function deleteSelectRowByKey(key) {
getTableInstance().deleteSelectRowByKey(key);
},
getSelectRowKeys: function getSelectRowKeys() {
return toRaw(getTableInstance().getSelectRowKeys());
},
getSelectRows: function getSelectRows() {
return toRaw(getTableInstance().getSelectRows());
},
clearSelectedRowKeys: function clearSelectedRowKeys() {
getTableInstance().clearSelectedRowKeys();
},
setSelectedRowKeys: function setSelectedRowKeys(keys) {
getTableInstance().setSelectedRowKeys(keys);
},
getPaginationRef: function getPaginationRef() {
return getTableInstance().getPaginationRef();
},
getSize: function getSize() {
return toRaw(getTableInstance().getSize());
},
updateTableData: function updateTableData(index, key, value) {
return getTableInstance().updateTableData(index, key, value);
},
getRowSelection: function getRowSelection() {
return toRaw(getTableInstance().getRowSelection());
},
getCacheColumns: function getCacheColumns() {
return toRaw(getTableInstance().getCacheColumns());
},
getForm: function getForm() {
return unref(formRef);
},
setShowPagination: function setShowPagination(show) {
return __awaiter(_this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
getTableInstance().setShowPagination(show);
case 1:
case "end":
return _context2.stop();
}
}
}, _callee2);
}));
},
getShowPagination: function getShowPagination() {
return toRaw(getTableInstance().getShowPagination());
}
};
return [register, methods];
}