element-plus
Version:
A Component Library for Vue 3
1 lines • 5.15 kB
Source Map (JSON)
{"version":3,"file":"current.mjs","sources":["../../../../../../../packages/components/table/src/store/current.ts"],"sourcesContent":["import { getCurrentInstance, ref, unref } from 'vue'\nimport { isNull } from 'lodash-unified'\nimport { getRowIdentity } from '../util'\n\nimport type { Ref } from 'vue'\nimport type { DefaultRow, Table } from '../table/defaults'\nimport type { WatcherPropsData } from '.'\n\nfunction useCurrent<T extends DefaultRow>(watcherData: WatcherPropsData<T>) {\n const instance = getCurrentInstance() as Table<T>\n const _currentRowKey: Ref<string | null> = ref(null)\n const currentRow: Ref<T | null> = ref(null)\n\n const setCurrentRowKey = (key: string) => {\n instance.store.assertRowKey()\n _currentRowKey.value = key\n setCurrentRowByKey(key)\n }\n\n const restoreCurrentRowKey = () => {\n _currentRowKey.value = null\n }\n\n const setCurrentRowByKey = (key: string) => {\n const { data, rowKey } = watcherData\n let _currentRow: T | null = null\n if (rowKey.value) {\n _currentRow =\n (unref(data) || []).find(\n (item) => getRowIdentity(item, rowKey.value) === key\n ) ?? null\n }\n currentRow.value = _currentRow ?? null\n instance.emit('current-change', currentRow.value, null)\n }\n\n const updateCurrentRow = (_currentRow: T) => {\n const oldCurrentRow = currentRow.value\n if (_currentRow && _currentRow !== oldCurrentRow) {\n currentRow.value = _currentRow\n instance.emit('current-change', currentRow.value, oldCurrentRow)\n return\n }\n if (!_currentRow && oldCurrentRow) {\n currentRow.value = null\n instance.emit('current-change', null, oldCurrentRow)\n }\n }\n\n const updateCurrentRowData = () => {\n const rowKey = watcherData.rowKey.value\n // data 为 null 时,解构时的默认值会被忽略\n const data = watcherData.data.value || []\n const oldCurrentRow = currentRow.value\n // 当 currentRow 不在 data 中时尝试更新数据\n if (oldCurrentRow && !data.includes(oldCurrentRow)) {\n if (rowKey) {\n const currentRowKey = getRowIdentity(oldCurrentRow, rowKey)\n setCurrentRowByKey(currentRowKey)\n } else {\n currentRow.value = null\n }\n if (isNull(currentRow.value)) {\n instance.emit('current-change', null, oldCurrentRow)\n }\n } else if (_currentRowKey.value) {\n // 把初始时下设置的 rowKey 转化成 rowData\n setCurrentRowByKey(_currentRowKey.value)\n restoreCurrentRowKey()\n }\n }\n\n return {\n setCurrentRowKey,\n restoreCurrentRowKey,\n setCurrentRowByKey,\n updateCurrentRow,\n updateCurrentRowData,\n states: {\n _currentRowKey,\n currentRow,\n },\n }\n}\n\nexport default useCurrent\n"],"names":[],"mappings":";;;;AAQA,SAAS,WAAiC,WAAkC,EAAA;AAC1E,EAAA,MAAM,WAAW,kBAAmB,EAAA,CAAA;AACpC,EAAM,MAAA,cAAA,GAAqC,IAAI,IAAI,CAAA,CAAA;AACnD,EAAM,MAAA,UAAA,GAA4B,IAAI,IAAI,CAAA,CAAA;AAE1C,EAAM,MAAA,gBAAA,GAAmB,CAAC,GAAgB,KAAA;AACxC,IAAA,QAAA,CAAS,MAAM,YAAa,EAAA,CAAA;AAC5B,IAAA,cAAA,CAAe,KAAQ,GAAA,GAAA,CAAA;AACvB,IAAA,kBAAA,CAAmB,GAAG,CAAA,CAAA;AAAA,GACxB,CAAA;AAEA,EAAA,MAAM,uBAAuB,MAAM;AACjC,IAAA,cAAA,CAAe,KAAQ,GAAA,IAAA,CAAA;AAAA,GACzB,CAAA;AAEA,EAAM,MAAA,kBAAA,GAAqB,CAAC,GAAgB,KAAA;AAvB9C,IAAA,IAAA,EAAA,CAAA;AAwBI,IAAM,MAAA,EAAE,IAAM,EAAA,MAAA,EAAW,GAAA,WAAA,CAAA;AACzB,IAAA,IAAI,WAAwB,GAAA,IAAA,CAAA;AAC5B,IAAA,IAAI,OAAO,KAAO,EAAA;AAChB,MAAA,WAAA,GAAA,CACG,EAAM,GAAA,CAAA,KAAA,CAAA,IAAI,CAAK,IAAA,EAAI,EAAA,IAAA;AAAA,QAClB,CAAC,IAAS,KAAA,cAAA,CAAe,IAAM,EAAA,MAAA,CAAO,KAAK,CAAM,KAAA,GAAA;AAAA,YADlD,IAEI,GAAA,EAAA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