{"version":3,"file":"current.mjs","names":[],"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"],"mappings":";;;AAOA,SAAS,WAAiC,aAAkC;CAC1E,MAAM,WAAW,mBAAmB;CACpC,MAAM,iBAAqC,IAAI,IAAI;CACnD,MAAM,aAA4B,IAAI,IAAI;CAE1C,MAAM,oBAAoB,QAAgB;EACxC,SAAS,MAAM,aAAa;EAC5B,eAAe,QAAQ;EACvB,mBAAmB,GAAG;CACxB;CAEA,MAAM,6BAA6B;EACjC,eAAe,QAAQ;CACzB;CAEA,MAAM,sBAAsB,QAAgB;EAC1C,MAAM,EAAE,MAAM,WAAW;EACzB,MAAM,gBAAgB,WAAW;EACjC,IAAI,cAAwB;EAC5B,IAAI,OAAO,OACT,eACG,MAAM,IAAI,KAAK,CAAC,EAAA,CAAG,MACjB,SAAS,eAAe,MAAM,OAAO,KAAK,MAAM,GACnD,KAAK;EAET,WAAW,QAAQ,eAAe;EAClC,SAAS,KAAK,kBAAkB,WAAW,OAAO,aAAa;CACjE;CAEA,MAAM,oBAAoB,gBAAmB;EAC3C,MAAM,gBAAgB,WAAW;EACjC,IAAI,eAAe,gBAAgB,eAAe;GAChD,WAAW,QAAQ;GACnB,SAAS,KAAK,kBAAkB,WAAW,OAAO,aAAa;GAC/D;EACF;EACA,IAAI,CAAC,eAAe,eAAe;GACjC,WAAW,QAAQ;GACnB,SAAS,KAAK,kBAAkB,MAAM,aAAa;EACrD;CACF;CAEA,MAAM,6BAA6B;EACjC,MAAM,SAAS,YAAY,OAAO;EAElC,MAAM,OAAO,YAAY,KAAK,SAAS,CAAC;EACxC,MAAM,gBAAgB,WA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