Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(component): prevent column resizing when editing text field #991

Merged
merged 1 commit into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/big-design/src/components/Worksheet/Worksheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const InternalWorksheet = typedMemo(
defaultExpandedRows,
disabledRows,
items,
minWidth,
onChange,
onErrors,
}: WorksheetProps<T>): React.ReactElement<WorksheetProps<T>> => {
Expand Down Expand Up @@ -133,6 +134,7 @@ const InternalWorksheet = typedMemo(
<StyledBox>
<Table
hasStaticWidth={tableHasStaticWidth}
minWidth={minWidth}
onKeyDown={handleKeyDown}
ref={tableRef}
tabIndex={0}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`renders worksheet 1`] = `
class="styled__StyledBox-sc-sj5f1m-0 gHBxjL styled__StyledBox-sc-16rzzpq-2 jjhxof"
>
<table
class="styled__Table-sc-16rzzpq-0 hiKdjk"
class="styled__Table-sc-16rzzpq-0 iumFEW"
tabindex="0"
>
<thead>
Expand All @@ -14,27 +14,27 @@ exports[`renders worksheet 1`] = `
class="styled__Status-sc-1aacki0-0 eDVZgo"
/>
<th
class="styled__Header-sc-16rzzpq-1 cRvCWu"
class="styled__Header-sc-16rzzpq-1 bAyhTK"
>
Product name
</th>
<th
class="styled__Header-sc-16rzzpq-1 biTmCe"
class="styled__Header-sc-16rzzpq-1 iEhMHd"
>
Visible on storefront
</th>
<th
class="styled__Header-sc-16rzzpq-1 cRvCWu"
class="styled__Header-sc-16rzzpq-1 bAyhTK"
>
Other field
</th>
<th
class="styled__Header-sc-16rzzpq-1 cRvCWu"
class="styled__Header-sc-16rzzpq-1 bAyhTK"
>
Other field
</th>
<th
class="styled__Header-sc-16rzzpq-1 cyFJXl"
class="styled__Header-sc-16rzzpq-1 ZLAqT"
>
Number field
</th>
Expand Down
13 changes: 11 additions & 2 deletions packages/big-design/src/components/Worksheet/spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { theme } from '@bigcommerce/big-design-theme';
import { fireEvent, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';

import { StatefulTree } from '../StatefulTree';
Expand Down Expand Up @@ -726,11 +727,11 @@ describe('SelectEditor', () => {

const cells = await findAllByDisplayValue('Value 1');

fireEvent.click(cells[0]);
await userEvent.click(cells[0]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙇🏼‍♀️


const options = await findAllByRole('option');

fireEvent.click(options[2]);
await userEvent.click(options[2]);

expect(handleChange).toHaveBeenCalledTimes(1);
expect(handleChange).toHaveBeenLastCalledWith([
Expand Down Expand Up @@ -1147,6 +1148,14 @@ describe('column widths', () => {
expect(table).toHaveStyle('width: auto');
});

test('table accepts a minWidth prop', () => {
render(<Worksheet columns={columns} items={items} minWidth={900} onChange={handleChange} />);

const table = screen.getByRole('table');

expect(table).toHaveStyle('min-width: 900px');
});

test('columns have defined widths (or auto if none)', async () => {
render(<Worksheet columns={columns} items={items} onChange={handleChange} />);

Expand Down
7 changes: 4 additions & 3 deletions packages/big-design/src/components/Worksheet/styled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import { Box } from '../Box';

import { InternalWorksheetColumn } from './types';

export const Table = styled.table<{ hasStaticWidth: boolean }>`
export const Table = styled.table<{ minWidth?: number; hasStaticWidth: boolean }>`
border-collapse: collapse;
border-spacing: 0;
min-width: ${({ minWidth, hasStaticWidth }) =>
minWidth && !hasStaticWidth ? `${minWidth}px` : 'auto'};
table-layout: fixed;
width: ${({ hasStaticWidth }) => (hasStaticWidth ? 'auto' : '100%')};

&:focus {
Expand All @@ -24,8 +27,6 @@ export const Header = styled.th<{
color: ${({ theme }) => theme.colors.secondary60};
font-weight: ${({ theme }) => theme.typography.fontWeight.semiBold};
height: ${({ theme }) => theme.helpers.remCalc(52)};
min-width: ${({ columnWidth }) =>
typeof columnWidth === 'string' ? columnWidth : `${columnWidth}px`};
overflow: hidden;
padding: ${({ theme }) => `0 ${theme.helpers.remCalc(17)}`};
text-align: ${({ columnType }) => (columnType === 'number' ? 'right' : 'left')};
Expand Down
1 change: 1 addition & 0 deletions packages/big-design/src/components/Worksheet/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface WorksheetProps<Item extends WorksheetItem> {
expandableRows?: ExpandableRows;
defaultExpandedRows?: Array<string | number>;
disabledRows?: DisabledRows;
minWidth?: number;
onChange(items: Item[]): void;
onErrors?(items: Array<WorksheetError<Item>>): void;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/docs/PropTables/WorksheetPropTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ const worksheetProps: Prop[] = [
</>
),
},
{
name: 'minWidth',
types: 'number',
description: 'Sets a min-width.',
},
];

const worksheetTextColumnProps: Prop[] = [
Expand Down
1 change: 1 addition & 0 deletions packages/docs/pages/worksheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ const WorksheetPage = () => {
<Worksheet
columns={columns}
items={items}
minWidth={900}
onChange={(items) => items}
onErrors={(items) => items}
/>
Expand Down