UNPKG

ivue-material-plus

Version:

A high quality UI components Library with Vue.js

65 lines (62 loc) 1.94 kB
import { getCurrentInstance, ref, unref } from 'vue'; import { getRowIdentity } from '../utils.mjs'; function useCurrent(watcherData) { const vm = getCurrentInstance(); const currentRow = ref(null); const _currentRowKey = ref(null); 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 = getRowIdentity(oldCurrentRow, rowKey); setCurrentRowKey(currentRowKey); } else { currentRow.value = null; } if (currentRow.value === null) { vm.emit("on-current-change", null, oldCurrentRow); } } else if (_currentRowKey.value) { setCurrentRowKey(_currentRowKey.value); restoreCurrentRowKey(); } }; const setCurrentRowKey = (key) => { const { data, rowKey } = watcherData; let _currentRow = null; if (rowKey.value) { _currentRow = (unref(data) || []).find( (item) => getRowIdentity(item, rowKey.value) === key ); } currentRow.value = _currentRow; vm.emit("on-current-change", currentRow.value, null); }; const restoreCurrentRowKey = () => { _currentRowKey.value = null; }; const updateCurrentRow = (_currentRow) => { const oldCurrentRow = currentRow.value; if (_currentRow && _currentRow !== oldCurrentRow) { currentRow.value = _currentRow; vm.emit("on-current-change", currentRow.value, oldCurrentRow); } if (!_currentRow && oldCurrentRow) { currentRow.value = null; vm.emit("on-current-change", null, oldCurrentRow); } }; return { updateCurrentRowData, updateCurrentRow, setCurrentRowKey, states: { _currentRowKey, currentRow } }; } export { useCurrent as default }; //# sourceMappingURL=current.mjs.map