element-plus
Version:
A Component Library for Vue 3
1 lines • 3.72 kB
Source Map (JSON)
{"version":3,"file":"current.mjs","sources":["../../../../../../../packages/components/table/src/store/current.ts"],"sourcesContent":["import { ref, getCurrentInstance, unref } from 'vue'\nimport { getRowIdentity } from '../util'\n\nimport type { Ref } from 'vue'\nimport type { Table } from '../table/defaults'\nimport type { WatcherPropsData } from './index'\n\nfunction useCurrent<T>(watcherData: WatcherPropsData<T>) {\n const instance = getCurrentInstance() as Table<T>\n const _currentRowKey = ref<string>(null)\n const currentRow: Ref<T> = 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 = null\n if (rowKey.value) {\n _currentRow = (unref(data) || []).find(\n (item) => getRowIdentity(item, rowKey.value) === key\n )\n }\n currentRow.value = _currentRow\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 (data.indexOf(oldCurrentRow) === -1 && oldCurrentRow) {\n if (rowKey) {\n const currentRowKey = getRowIdentity(oldCurrentRow, rowKey)\n setCurrentRowByKey(currentRowKey)\n } else {\n currentRow.value = null\n }\n if (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,oBAAuB,aAAkC;AACvD,QAAM,WAAW;AACjB,QAAM,iBAAiB,IAAY;AACnC,QAAM,aAAqB,IAAI;AAE/B,QAAM,mBAAmB,CAAC,QAAgB;AACxC,aAAS,MAAM;AACf,mBAAe,QAAQ;AACvB,uBAAmB;AAAA;AAGrB,QAAM,uBAAuB,MAAM;AACjC,mBAAe,QAAQ;AAAA;AAGzB,QAAM,qBAAqB,CAAC,QAAgB;AAC1C,UAAM,EAAE,MAAM,WAAW;AACzB,QAAI,cAAc;AAClB,QAAI,OAAO,OAAO;AAChB,oBAAe,OAAM,SAAS,IAAI,KAChC,CAAC,SAAS,eAAe,MAAM,OAAO,WAAW;AAAA;AAGrD,eAAW,QAAQ;AAAA;AAGrB,QAAM,mBAAmB,CAAC,gBAAmB;AAC3C,UAAM,gBAAgB,WAAW;AACjC,QAAI,eAAe,gBAAgB,eAAe;AAChD,iBAAW,QAAQ;AACnB,eAAS,KAAK,kBAAkB,WAAW,OAAO;AAClD;AAAA;AAEF,QAAI,CAAC,eAAe,eAAe;AACjC,iBAAW,QAAQ;AACnB,eAAS,KAAK,kBAAkB,MAAM;AAAA;AAAA;AAI1C,QAAM,uBAAuB,MAAM;AACjC,UAAM,SAAS,YAAY,OAAO;AAElC,UAAM,OAAO,YAAY,KAAK,SAAS;AACvC,UAAM,gBAAgB,WAAW;AAEjC,QAAI,KAAK,QAAQ,mBAAmB,MAAM,eAAe;AACvD,UAAI,QAAQ;AACV,cAAM,gBAAgB,eAAe,eAAe;AACpD,2BAAmB;AAAA,aACd;AACL,mBAAW,QAAQ;AAAA;AAErB,UAAI,WAAW,UAAU,MAAM;AAC7B,iBAAS,KAAK,kBAAkB,MAAM;AAAA;AAAA,eAE/B,eAAe,OAAO;AAE/B,yBAAmB,eAAe;AAClC;AAAA;AAAA;AAIJ,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,QAAQ;AAAA,MACN;AAAA,MACA;AAAA;AAAA;AAAA;;;;"}