3f3579126ec51f4d11166ec2f495127c5608fd0133d4c02018f07fdbb15a98a64079e66df3664b820984b54c2466773eb5ff86b92718c7233993d25c370a04 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { getCurrentInstance, ref } from 'vue';
  2. import { getKeysMap, getRowIdentity, toggleRowStatus } from '../util.mjs';
  3. function useExpand(watcherData) {
  4. const instance = getCurrentInstance();
  5. const defaultExpandAll = ref(false);
  6. const expandRows = ref([]);
  7. const updateExpandRows = () => {
  8. const data = watcherData.data.value || [];
  9. const rowKey = watcherData.rowKey.value;
  10. if (defaultExpandAll.value) {
  11. expandRows.value = data.slice();
  12. } else if (rowKey) {
  13. const expandRowsMap = getKeysMap(expandRows.value, rowKey);
  14. expandRows.value = data.reduce((prev, row) => {
  15. const rowId = getRowIdentity(row, rowKey);
  16. const rowInfo = expandRowsMap[rowId];
  17. if (rowInfo) {
  18. prev.push(row);
  19. }
  20. return prev;
  21. }, []);
  22. } else {
  23. expandRows.value = [];
  24. }
  25. };
  26. const toggleRowExpansion = (row, expanded) => {
  27. const changed = toggleRowStatus(expandRows.value, row, expanded, void 0, void 0, void 0, watcherData.rowKey.value);
  28. if (changed) {
  29. instance.emit("expand-change", row, expandRows.value.slice());
  30. }
  31. };
  32. const setExpandRowKeys = (rowKeys) => {
  33. instance.store.assertRowKey();
  34. const data = watcherData.data.value || [];
  35. const rowKey = watcherData.rowKey.value;
  36. const keysMap = getKeysMap(data, rowKey);
  37. expandRows.value = rowKeys.reduce((prev, cur) => {
  38. const info = keysMap[cur];
  39. if (info) {
  40. prev.push(info.row);
  41. }
  42. return prev;
  43. }, []);
  44. };
  45. const isRowExpanded = (row) => {
  46. const rowKey = watcherData.rowKey.value;
  47. if (rowKey) {
  48. const expandMap = getKeysMap(expandRows.value, rowKey);
  49. return !!expandMap[getRowIdentity(row, rowKey)];
  50. }
  51. return expandRows.value.includes(row);
  52. };
  53. return {
  54. updateExpandRows,
  55. toggleRowExpansion,
  56. setExpandRowKeys,
  57. isRowExpanded,
  58. states: {
  59. expandRows,
  60. defaultExpandAll
  61. }
  62. };
  63. }
  64. export { useExpand as default };
  65. //# sourceMappingURL=expand.mjs.map