Skip to content

Commit

Permalink
fix: sorting frozen columns (deephaven#1749)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
wusteven815 authored Feb 7, 2024
1 parent 0781900 commit 51e60c5
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,57 @@ test('Sorts items', async () => {
);
});

test('Sort ascending items with frozen/front/back columns', async () => {
const user = userEvent.setup({ delay: null });
const mockHandler = jest.fn();
const sortButton = () => screen.getByLabelText('Sort ascending');
const model = irisGridTestUtils.makeModel(
irisGridTestUtils.makeTable({
columns: COLUMNS,
layoutHints: {
frontColumns: [`${COLUMN_PREFIX}2`, `${COLUMN_PREFIX}3`],
frozenColumns: [`${COLUMN_PREFIX}0`, `${COLUMN_PREFIX}1`],
backColumns: [`${COLUMN_PREFIX}9`],
},
})
);
render(<Builder model={model} onMovedColumnsChanged={mockHandler} />);

await selectItems(user, [1]);
await user.click(sortButton());
const newMoves = [];
expect(mockHandler).toBeCalledWith(newMoves);
});

test('Sort descending items with frozen/front/back columns', async () => {
const user = userEvent.setup({ delay: null });
const mockHandler = jest.fn();
const sortButton = () => screen.getByLabelText('Sort descending');
const model = irisGridTestUtils.makeModel(
irisGridTestUtils.makeTable({
columns: COLUMNS,
layoutHints: {
frontColumns: [`${COLUMN_PREFIX}1`],
frozenColumns: [`${COLUMN_PREFIX}0`],
backColumns: [`${COLUMN_PREFIX}9`],
},
})
);
render(<Builder model={model} onMovedColumnsChanged={mockHandler} />);

await selectItems(user, [1]);
await user.click(sortButton());
const newMoves = [
{ from: 8, to: 2 },
{ from: 8, to: 3 },
{ from: 8, to: 4 },
{ from: 8, to: 5 },
{ from: 8, to: 6 },
{ from: 8, to: 7 },
];
expect(mockHandler).toBeCalledWith(newMoves);
});

test('Creates groups', async () => {
const user = userEvent.setup({ delay: null });
const model = makeModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,11 +635,31 @@ class VisibilityOrderingBuilder extends PureComponent<
option: keyof typeof VisibilityOrderingBuilder.SORTING_OPTIONS
): void {
const { model, onMovedColumnsChanged } = this.props;
const tree = this.getTreeItems();
const firstIndex = this.getFirstMovableIndex() ?? 0;
const lastIndex = this.getLastMovableIndex() ?? tree.length - 1;
const moveableTree = tree.slice(firstIndex, lastIndex + 1);

// add frozen moves
const initialAndFrozenMovedColumns = [...model.initialMovedColumns];
for (let i = 0; i < model.frozenColumns.length; i += 1) {
const frozenColumn = model.frozenColumns[i];
const newFrozenIndex = GridUtils.getVisibleIndex(
model.getColumnIndexByName(frozenColumn) ?? 0,
initialAndFrozenMovedColumns
);
if (newFrozenIndex !== i) {
initialAndFrozenMovedColumns.push({
from: newFrozenIndex,
to: i,
});
}
}

const newMoves = this.getSortMoves(
this.getTreeItems(),
moveableTree,
option,
model.initialMovedColumns
initialAndFrozenMovedColumns
);

onMovedColumnsChanged(newMoves);
Expand Down

0 comments on commit 51e60c5

Please sign in to comment.