Skip to content

Commit

Permalink
Sorta revert column widths cache/state storage added in draggable col…
Browse files Browse the repository at this point in the history
…umns PR
  • Loading branch information
cee-chen committed Oct 16, 2024
1 parent 4259443 commit cc2bf82
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
25 changes: 22 additions & 3 deletions packages/eui/src/components/datagrid/utils/col_widths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ describe('useColumnWidths', () => {
columns: [{ id: 'b', initialWidth: 75 }, { id: 'c' }],
trailingControlColumns: [{ id: 'd', width: 25 }] as any,
defaultColumnWidth: 150,
onColumnResize: jest.fn(),
};

describe('columnWidths', () => {
Expand Down Expand Up @@ -136,16 +135,36 @@ describe('useColumnWidths', () => {
});
expect(result.current.columnWidths).toEqual({ b: 150 });
});

it('does override user resized column widths if `onColumnResize` is passed', () => {
const controlledWidthsArgs = {
...args,
onColumnResize: jest.fn(),
};
const { rerender, result } = renderHook(useColumnWidths, {
initialProps: controlledWidthsArgs,
});

renderHookAct(() => result.current.setColumnWidth('b', 150));
rerender({
...controlledWidthsArgs,
columns: [{ id: 'b', initialWidth: 100 }],
});
expect(result.current.columnWidths).toEqual({ b: 100 });
});
});
});

describe('setColumnWidth', () => {
it("sets a single column's width in the columnWidths map", () => {
const { result } = renderHook(() => useColumnWidths(args));
const onColumnResize = jest.fn();
const { result } = renderHook(() =>
useColumnWidths({ ...args, onColumnResize })
);

renderHookAct(() => result.current.setColumnWidth('c', 125));
expect(result.current.columnWidths).toEqual({ b: 75, c: 125 });
expect(args.onColumnResize).toHaveBeenCalledWith({
expect(onColumnResize).toHaveBeenCalledWith({
columnId: 'c',
width: 125,
});
Expand Down
10 changes: 8 additions & 2 deletions packages/eui/src/components/datagrid/utils/col_widths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,25 @@ export const useColumnWidths = ({
setColumnWidth: (columnId: string, width: number) => void;
getColumnWidth: (index: number) => number;
} => {
const hasOnColumnResize = !!onColumnResize;
const getInitialWidths = useCallback(
(prevColumnWidths?: EuiDataGridColumnWidths) => {
const columnWidths = { ...prevColumnWidths };
columns
.filter(doesColumnHaveAnInitialWidth)
.forEach(({ id, initialWidth }) => {
if (columnWidths[id] == null) {
// Several Kibana datagrids are using `onColumnResize` and `column.initialWidth`
// to fully control column widths. Sadly, we didn't do a good job documenting
// the intent of this prop and how controlled it is, so for now we'll assume
// that any datagrid passing `onColumnResize` is controlling its column widths
// and should override any internal column width state set by user resizing
if (hasOnColumnResize || columnWidths[id] == null) {
columnWidths[id] = initialWidth!;
}
});
return columnWidths;
},
[columns]
[columns, hasOnColumnResize]
);

// Passes initializer function for performance, so computing only runs once on init
Expand Down

0 comments on commit cc2bf82

Please sign in to comment.