element-plus
Version:
A Component Library for Vue 3
76 lines (71 loc) • 2.27 kB
JavaScript
Object.defineProperty(exports, '__esModule', { value: true });
var vue = require('vue');
var lodashUnified = require('lodash-unified');
var util = require('../util.js');
function useCurrent(watcherData) {
const instance = vue.getCurrentInstance();
const _currentRowKey = vue.ref(null);
const currentRow = vue.ref(null);
const setCurrentRowKey = (key) => {
instance.store.assertRowKey();
_currentRowKey.value = key;
setCurrentRowByKey(key);
};
const restoreCurrentRowKey = () => {
_currentRowKey.value = null;
};
const setCurrentRowByKey = (key) => {
const { data, rowKey } = watcherData;
let _currentRow = null;
if (rowKey.value) {
_currentRow = (vue.unref(data) || []).find((item) => util.getRowIdentity(item, rowKey.value) === key);
}
currentRow.value = _currentRow;
instance.emit("current-change", currentRow.value, null);
};
const updateCurrentRow = (_currentRow) => {
const oldCurrentRow = currentRow.value;
if (_currentRow && _currentRow !== oldCurrentRow) {
currentRow.value = _currentRow;
instance.emit("current-change", currentRow.value, oldCurrentRow);
return;
}
if (!_currentRow && oldCurrentRow) {
currentRow.value = null;
instance.emit("current-change", null, oldCurrentRow);
}
};
const updateCurrentRowData = () => {
const rowKey = watcherData.rowKey.value;
const data = watcherData.data.value || [];
const oldCurrentRow = currentRow.value;
if (!data.includes(oldCurrentRow) && oldCurrentRow) {
if (rowKey) {
const currentRowKey = util.getRowIdentity(oldCurrentRow, rowKey);
setCurrentRowByKey(currentRowKey);
} else {
currentRow.value = null;
}
if (lodashUnified.isNull(currentRow.value)) {
instance.emit("current-change", null, oldCurrentRow);
}
} else if (_currentRowKey.value) {
setCurrentRowByKey(_currentRowKey.value);
restoreCurrentRowKey();
}
};
return {
setCurrentRowKey,
restoreCurrentRowKey,
setCurrentRowByKey,
updateCurrentRow,
updateCurrentRowData,
states: {
_currentRowKey,
currentRow
}
};
}
exports["default"] = useCurrent;
//# sourceMappingURL=current.js.map
;