ivue-material-plus
Version:
A high quality UI components Library with Vue.js
1 lines • 5.53 kB
Source Map (JSON)
{"version":3,"file":"current.mjs","sources":["../../../../../../src/components/ivue-table/store/current.ts"],"sourcesContent":["import { getCurrentInstance, ref, unref } from 'vue';\r\nimport { getRowIdentity } from '../utils';\r\n\r\n// ts\r\nimport type { WatcherPropsData } from './index';\r\nimport type { Table } from '../table/defaults';\r\n\r\nfunction useCurrent(watcherData: WatcherPropsData) {\r\n const vm = getCurrentInstance() as Table;\r\n\r\n // 当前行数据\r\n const currentRow = ref(null);\r\n // 当前行key\r\n const _currentRowKey = ref<string | null>(null);\r\n\r\n // 更新当前行数据\r\n const updateCurrentRowData = () => {\r\n // 行数据的 Ke\r\n const rowKey = watcherData.rowKey.value;\r\n // data 为 null 时,解构时的默认值会被忽略\r\n const data = watcherData.data.value || [];\r\n\r\n // 上一个行数据\r\n const oldCurrentRow = currentRow.value;\r\n\r\n // 当 currentRow 不在 data 中时尝试更新数据\r\n if (!data.includes(oldCurrentRow) && oldCurrentRow) {\r\n // 行数据的key\r\n if (rowKey) {\r\n // 获取rowKey对应的数据\r\n const currentRowKey = getRowIdentity(oldCurrentRow, rowKey);\r\n\r\n // 设置当前行数据的key\r\n setCurrentRowKey(currentRowKey);\r\n }\r\n // 没有key\r\n else {\r\n currentRow.value = null;\r\n }\r\n\r\n // 当表格的当前行发生变化的时候会触发该事件\r\n if (currentRow.value === null) {\r\n vm.emit('on-current-change', null, oldCurrentRow);\r\n }\r\n }\r\n // 当前行key\r\n else if (_currentRowKey.value) {\r\n // 把初始时下设置的 rowKey 转化成 rowData\r\n setCurrentRowKey(_currentRowKey.value);\r\n\r\n // 恢复当前行键\r\n restoreCurrentRowKey();\r\n }\r\n };\r\n\r\n // 设置当前行的key\r\n const setCurrentRowKey = (key: string) => {\r\n const { data, rowKey } = watcherData;\r\n\r\n // 当前行数据\r\n let _currentRow = null;\r\n\r\n // 行数据的key\r\n if (rowKey.value) {\r\n // 获取rowKey对应的数据\r\n _currentRow = (unref(data) || []).find(\r\n (item) => getRowIdentity(item, rowKey.value) === key\r\n );\r\n }\r\n\r\n // 获取rowKey对应的数据\r\n currentRow.value = _currentRow;\r\n\r\n vm.emit('on-current-change', currentRow.value, null);\r\n };\r\n\r\n // 恢复当前行key\r\n const restoreCurrentRowKey = () => {\r\n _currentRowKey.value = null;\r\n };\r\n\r\n // 当前选择的行\r\n const updateCurrentRow = (_currentRow) => {\r\n // 上一个数据\r\n const oldCurrentRow = currentRow.value;\r\n\r\n // 是否有上一个数据\r\n if (_currentRow && _currentRow !== oldCurrentRow) {\r\n // 当前行数据\r\n currentRow.value = _currentRow;\r\n\r\n // 当表格的当前行发生变化的时候会触发该事件\r\n vm.emit('on-current-change', currentRow.value, oldCurrentRow);\r\n }\r\n\r\n // 没有当前数据\r\n if (!_currentRow && oldCurrentRow) {\r\n currentRow.value = null;\r\n\r\n // 当表格的当前行发生变化的时候会触发该事件\r\n vm.emit('on-current-change', null, oldCurrentRow);\r\n }\r\n };\r\n\r\n return {\r\n updateCurrentRowData,\r\n updateCurrentRow,\r\n setCurrentRowKey,\r\n states: {\r\n _currentRowKey,\r\n currentRow,\r\n },\r\n };\r\n}\r\n\r\nexport default useCurrent;\r\n"],"names":[],"mappings":";;;AAOA,SAAS,WAAW,WAA+B,EAAA;AACjD,EAAA,MAAM,KAAK,kBAAmB,EAAA,CAAA;AAG9B,EAAM,MAAA,UAAA,GAAa,IAAI,IAAI,CAAA,CAAA;AAE3B,EAAM,MAAA,cAAA,GAAiB,IAAmB,IAAI,CAAA,CAAA;AAG9C,EAAA,MAAM,uBAAuB,MAAM;AAEjC,IAAM,MAAA,MAAA,GAAS,YAAY,MAAO,CAAA,KAAA,CAAA;AAElC,IAAA,MAAM,IAAO,GAAA,WAAA,CAAY,IAAK,CAAA,KAAA,IAAS,EAAC,CAAA;AAGxC,IAAA,MAAM,gBAAgB,UAAW,CAAA,KAAA,CAAA;AAGjC,IAAA,IAAI,CAAC,IAAA,CAAK,QAA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