|
1 | 1 | import { arrayMove } from "@dnd-kit/sortable"
|
2 |
| -import { get, isEqual } from "lodash-es" |
3 |
| -import { FC, useEffect, useMemo } from "react" |
| 2 | +import deepDiff from "deep-diff" |
| 3 | +import { get } from "lodash-es" |
| 4 | +import { FC, useMemo } from "react" |
4 | 5 | import { useSelector } from "react-redux"
|
5 | 6 | import { v4 } from "uuid"
|
6 | 7 | import { dealRawData2ArrayData } from "@/page/App/components/InspectPanel/PanelSetters/DataGridSetter/utils"
|
@@ -90,39 +91,60 @@ const ColumnSetter: FC<ColumnSetterProps> = (props) => {
|
90 | 91 | }
|
91 | 92 | }, [arrayData])
|
92 | 93 |
|
| 94 | + const customColumns = useMemo(() => { |
| 95 | + return value.filter((item) => !item.isCalc) |
| 96 | + }, [value]) |
| 97 | + |
93 | 98 | const mixedColumns: ColumnConfig[] = useMemo(() => {
|
94 | 99 | if (calculateColumns.length === 0) {
|
95 | 100 | return value
|
96 | 101 | }
|
97 |
| - const mixedColumns: ColumnConfig[] = [] |
98 |
| - |
99 |
| - value.forEach((config) => { |
100 |
| - const index = calculateColumns.findIndex( |
101 |
| - (item) => item.field === config.field, |
102 |
| - ) |
103 |
| - if (index !== -1) { |
104 |
| - calculateColumns.splice(index, 1) |
105 |
| - mixedColumns.push(config) |
| 102 | + const mixedColumnsResult: ColumnConfig[] = [] |
| 103 | + |
| 104 | + calculateColumns.forEach((config) => { |
| 105 | + const oldConfig = value.find((item) => item.field === config.field) |
| 106 | + if (oldConfig) { |
| 107 | + mixedColumnsResult.push(oldConfig) |
106 | 108 | } else {
|
107 |
| - if (!config.isCalc) { |
108 |
| - mixedColumns.push(config) |
109 |
| - } |
| 109 | + mixedColumnsResult.push(config) |
110 | 110 | }
|
111 | 111 | })
|
112 |
| - calculateColumns.forEach((config) => { |
113 |
| - mixedColumns.push(config) |
| 112 | + mixedColumnsResult.push(...customColumns) |
| 113 | + |
| 114 | + mixedColumnsResult.sort((a, b) => { |
| 115 | + const aIndex = value.findIndex((item) => item.field === a.field) |
| 116 | + const bIndex = value.findIndex((item) => item.field === b.field) |
| 117 | + |
| 118 | + if (aIndex === -1 && bIndex === -1) { |
| 119 | + return 0 |
| 120 | + } |
| 121 | + if (aIndex === -1) { |
| 122 | + return 1 |
| 123 | + } |
| 124 | + |
| 125 | + if (bIndex === -1) { |
| 126 | + return -1 |
| 127 | + } |
| 128 | + |
| 129 | + return aIndex - bIndex |
114 | 130 | })
|
115 | 131 |
|
116 |
| - return mixedColumns |
117 |
| - }, [calculateColumns, value]) |
| 132 | + const diff = deepDiff(value, mixedColumnsResult) |
118 | 133 |
|
119 |
| - useEffect(() => { |
120 |
| - if (!isEqual(mixedColumns, value)) { |
| 134 | + if ((diff?.length ?? 0) > 0) { |
121 | 135 | handleUpdateMultiAttrDSL?.({
|
122 |
| - [attrName]: mixedColumns, |
| 136 | + [attrName]: mixedColumnsResult, |
123 | 137 | })
|
124 | 138 | }
|
125 |
| - }, [attrName, handleUpdateMultiAttrDSL, mixedColumns, value]) |
| 139 | + |
| 140 | + return mixedColumnsResult |
| 141 | + }, [ |
| 142 | + attrName, |
| 143 | + calculateColumns, |
| 144 | + customColumns, |
| 145 | + handleUpdateMultiAttrDSL, |
| 146 | + value, |
| 147 | + ]) |
126 | 148 |
|
127 | 149 | return (
|
128 | 150 | <ColumnContainer
|
|
0 commit comments