Skip to content

Commit 51e60c5

Browse files
authored
fix: sorting frozen columns (deephaven#1749)
- Fixes deephaven#1645 - Moves are now generated for frozen columns (like front/end for initial moved columns) and only used when sorting - The range being sorted is truncated to remove frozen/front/end columns - Added tests to make sure the sort is correct
1 parent 0781900 commit 51e60c5

2 files changed

Lines changed: 73 additions & 2 deletions

File tree

packages/iris-grid/src/sidebar/visibility-ordering-builder/VisibilityOrderingBuilder.test.tsx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,57 @@ test('Sorts items', async () => {
763763
);
764764
});
765765

766+
test('Sort ascending items with frozen/front/back columns', async () => {
767+
const user = userEvent.setup({ delay: null });
768+
const mockHandler = jest.fn();
769+
const sortButton = () => screen.getByLabelText('Sort ascending');
770+
const model = irisGridTestUtils.makeModel(
771+
irisGridTestUtils.makeTable({
772+
columns: COLUMNS,
773+
layoutHints: {
774+
frontColumns: [`${COLUMN_PREFIX}2`, `${COLUMN_PREFIX}3`],
775+
frozenColumns: [`${COLUMN_PREFIX}0`, `${COLUMN_PREFIX}1`],
776+
backColumns: [`${COLUMN_PREFIX}9`],
777+
},
778+
})
779+
);
780+
render(<Builder model={model} onMovedColumnsChanged={mockHandler} />);
781+
782+
await selectItems(user, [1]);
783+
await user.click(sortButton());
784+
const newMoves = [];
785+
expect(mockHandler).toBeCalledWith(newMoves);
786+
});
787+
788+
test('Sort descending items with frozen/front/back columns', async () => {
789+
const user = userEvent.setup({ delay: null });
790+
const mockHandler = jest.fn();
791+
const sortButton = () => screen.getByLabelText('Sort descending');
792+
const model = irisGridTestUtils.makeModel(
793+
irisGridTestUtils.makeTable({
794+
columns: COLUMNS,
795+
layoutHints: {
796+
frontColumns: [`${COLUMN_PREFIX}1`],
797+
frozenColumns: [`${COLUMN_PREFIX}0`],
798+
backColumns: [`${COLUMN_PREFIX}9`],
799+
},
800+
})
801+
);
802+
render(<Builder model={model} onMovedColumnsChanged={mockHandler} />);
803+
804+
await selectItems(user, [1]);
805+
await user.click(sortButton());
806+
const newMoves = [
807+
{ from: 8, to: 2 },
808+
{ from: 8, to: 3 },
809+
{ from: 8, to: 4 },
810+
{ from: 8, to: 5 },
811+
{ from: 8, to: 6 },
812+
{ from: 8, to: 7 },
813+
];
814+
expect(mockHandler).toBeCalledWith(newMoves);
815+
});
816+
766817
test('Creates groups', async () => {
767818
const user = userEvent.setup({ delay: null });
768819
const model = makeModel();

packages/iris-grid/src/sidebar/visibility-ordering-builder/VisibilityOrderingBuilder.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,11 +635,31 @@ class VisibilityOrderingBuilder extends PureComponent<
635635
option: keyof typeof VisibilityOrderingBuilder.SORTING_OPTIONS
636636
): void {
637637
const { model, onMovedColumnsChanged } = this.props;
638+
const tree = this.getTreeItems();
639+
const firstIndex = this.getFirstMovableIndex() ?? 0;
640+
const lastIndex = this.getLastMovableIndex() ?? tree.length - 1;
641+
const moveableTree = tree.slice(firstIndex, lastIndex + 1);
642+
643+
// add frozen moves
644+
const initialAndFrozenMovedColumns = [...model.initialMovedColumns];
645+
for (let i = 0; i < model.frozenColumns.length; i += 1) {
646+
const frozenColumn = model.frozenColumns[i];
647+
const newFrozenIndex = GridUtils.getVisibleIndex(
648+
model.getColumnIndexByName(frozenColumn) ?? 0,
649+
initialAndFrozenMovedColumns
650+
);
651+
if (newFrozenIndex !== i) {
652+
initialAndFrozenMovedColumns.push({
653+
from: newFrozenIndex,
654+
to: i,
655+
});
656+
}
657+
}
638658

639659
const newMoves = this.getSortMoves(
640-
this.getTreeItems(),
660+
moveableTree,
641661
option,
642-
model.initialMovedColumns
662+
initialAndFrozenMovedColumns
643663
);
644664

645665
onMovedColumnsChanged(newMoves);

0 commit comments

Comments
 (0)