Skip to content

Commit

Permalink
fix: Fixes table column min width when only width is set
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-kot committed Oct 21, 2024
1 parent 01aecc9 commit b5d421b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
15 changes: 13 additions & 2 deletions pages/table/sticky-scrollbar-in-container.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import React, { useContext } from 'react';

import { useCollection } from '@cloudscape-design/collection-hooks';

import { Checkbox, Container, Header, Pagination, SpaceBetween, Table } from '~components';
import { Button, Checkbox, Container, Header, Pagination, SpaceBetween, Table } from '~components';

import AppContext, { AppContextType } from '../app/app-context';
import ScreenshotArea from '../utils/screenshot-area';
import { generateItems } from './generate-data';
import { columnsConfig, paginationLabels } from './shared-configs';
import { columnsConfig as sharedColumnsConfig, paginationLabels } from './shared-configs';

type PageContext = React.Context<
AppContextType<{
Expand All @@ -20,6 +20,15 @@ type PageContext = React.Context<

const allItems = generateItems();
const PAGE_SIZE = 50;
const columnsConfig = [
...sharedColumnsConfig,
{
id: 'actions',
header: 'Fav',
cell: () => <Button variant="icon" iconName="star" ariaLabel="make favorite" />,
width: 70,
},
];

export default function App() {
const {
Expand All @@ -44,6 +53,8 @@ export default function App() {
footer={hasFooter ? <Pagination {...paginationProps} ariaLabels={paginationLabels} /> : undefined}
columnDefinitions={columnsConfig}
items={items}
stickyColumns={{ last: 1 }}
resizableColumns={true}
/>
</Container>
</div>
Expand Down
14 changes: 14 additions & 0 deletions src/table/__tests__/resizable-columns.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,20 @@ test('should not allow to resize column below 120px if min width is not defined'
expect(wrapper.findColumnHeaders()[0].getElement()).toHaveStyle({ width: '120px' });
});

test('takes width as min width if it is less than 120px and min width is not set', () => {
const props: TableProps<Item> = {
...defaultProps,
columnDefinitions: [{ header: 'id', cell: item => item.id, width: 100 }, defaultProps.columnDefinitions[1]],
};
const { wrapper } = renderTable(<Table {...props} />);
expect(wrapper.findColumnHeaders()[0].getElement()).toHaveStyle({ width: '100px' });

fireMousedown(wrapper.findColumnResizer(1)!);
fireMouseMove(100);
fireMouseup(100);
expect(wrapper.findColumnHeaders()[0].getElement()).toHaveStyle({ width: '100px' });
});

test('should follow along each mouse move event', () => {
const { wrapper } = renderTable(<Table {...defaultProps} />);
expect(wrapper.findColumnHeaders()[0].getElement()).toHaveStyle({ width: '150px' });
Expand Down
8 changes: 7 additions & 1 deletion src/table/use-column-widths.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ function updateWidths(
columnId: PropertyKey
) {
const column = visibleColumns.find(column => column.id === columnId);
const minWidth = typeof column?.minWidth === 'number' ? column.minWidth : DEFAULT_COLUMN_WIDTH;
let minWidth = DEFAULT_COLUMN_WIDTH;
if (typeof column?.width === 'number' && column.width < DEFAULT_COLUMN_WIDTH) {
minWidth = column?.width;
}
if (typeof column?.minWidth === 'number') {
minWidth = column?.minWidth;
}
newWidth = Math.max(newWidth, minWidth);
if (oldWidths.get(columnId) === newWidth) {
return oldWidths;
Expand Down

0 comments on commit b5d421b

Please sign in to comment.