Skip to content

Commit f20397c

Browse files
author
Amelia Wattenberger
committed
fix: don't override sort & sticky on data change
1 parent 17fdd65 commit f20397c

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

src/components/grid.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,13 @@ export function Grid(props: GridProps) {
163163
React.useEffect(() => {
164164
if (props.defaultSort)
165165
handleSortChange(props.defaultSort[0], props.defaultSort[1]);
166-
}, [props.defaultSort?.join(','), props.data]);
166+
}, [props.defaultSort?.join(',')]);
167167

168168
React.useEffect(updateColumnNames, [props.data, stickyColumnName]);
169169
React.useEffect(() => {
170170
if (props.defaultStickyColumnName)
171171
handleStickyColumnNameChange(props.defaultStickyColumnName);
172-
}, [props.defaultStickyColumnName, props.data]);
172+
}, [props.defaultStickyColumnName]);
173173

174174
React.useEffect(() => {
175175
handleIsEditableChange(!!props.isEditable);

src/store.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,26 @@ export const createGridStore = () =>
120120
const columnNames = data.length
121121
? Object.keys(data[0]).filter((d) => !utilKeys.includes(d))
122122
: [];
123-
draft.stickyColumnName = columnNames[0];
124-
draft.sort = columnNames[0]
125-
? [
126-
columnNames[0],
127-
// @ts-ignore
128-
cellTypeMap[draft.cellTypes[columnNames[0]]]?.sortValueType ===
129-
'string'
130-
? 'asc'
131-
: 'desc',
132-
]
133-
: [];
123+
124+
if (
125+
!draft.stickyColumnName ||
126+
!columnNames.includes(draft.stickyColumnName)
127+
) {
128+
draft.stickyColumnName = columnNames[0];
129+
}
130+
131+
if (!draft.sort.length) {
132+
draft.sort = draft.stickyColumnName
133+
? [
134+
draft.stickyColumnName,
135+
// @ts-ignore
136+
cellTypeMap[draft.cellTypes[draft.stickyColumnName]]
137+
?.sortValueType === 'string'
138+
? 'asc'
139+
: 'desc',
140+
]
141+
: [];
142+
}
134143
}),
135144
handleMetadataChange: (metadata) =>
136145
set((draft) => {

0 commit comments

Comments
 (0)