UNPKG

element-plus

Version:

A Component Library for Vue 3

1 lines 4.94 kB
{"version":3,"file":"current.mjs","sources":["../../../../../../../packages/components/table/src/store/current.ts"],"sourcesContent":["import { getCurrentInstance, ref, unref } from 'vue'\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 const oldCurrentRow = currentRow.value\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, oldCurrentRow)\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 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":";;;AAOA,SAAS,WAAiC,WAAA,EAAkC;AAC1E,EAAA,MAAM,WAAW,kBAAA,EAAmB;AACpC,EAAA,MAAM,cAAA,GAAqC,IAAI,IAAI,CAAA;AACnD,EAAA,MAAM,UAAA,GAA4B,IAAI,IAAI,CAAA;AAE1C,EAAA,MAAM,gBAAA,GAAmB,CAAC,GAAA,KAAgB;AACxC,IAAA,QAAA,CAAS,MAAM,YAAA,EAAa;AAC5B,IAAA,cAAA,CAAe,KAAA,GAAQ,GAAA;AACvB,IAAA,kBAAA,CAAmB,GAAG,CAAA;AAAA,EACxB,CAAA;AAEA,EAAA,MAAM,uBAAuB,MAAM;AACjC,IAAA,cAAA,CAAe,KAAA,GAAQ,IAAA;AAAA,EACzB,CAAA;AAEA,EAAA,MAAM,kBAAA,GAAqB,CAAC,GAAA,KAAgB;AAtB9C,IAAA,IAAA,EAAA;AAuBI,IAAA,MAAM,EAAE,IAAA,EAAM,MAAA,EAAO,GAAI,WAAA;AACzB,IAAA,MAAM,gBAAgB,UAAA,CAAW,KAAA;AACjC,IAAA,IAAI,WAAA,GAAwB,IAAA;AAC5B,IAAA,IAAI,OAAO,KAAA,EAAO;AAChB,MAAA,WAAA,GAAA,CACG,EAAA,GAAA,CAAA,KAAA,CAAM,IAAI,CAAA,IAAK,EAAC,EAAG,IAAA;AAAA,QAClB,CAAC,IAAA,KAAS,cAAA,CAAe,IAAA,EAAM,MAAA,CAAO,KAAK,CAAA,KAAM;AAAA,YADlD,IAAA,GAAA,EAAA,GAEI,IAAA;AAAA,IACT;AACA,IAAA,UAAA,CAAW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