Skip to content

Commit

Permalink
fix(Table): columns change throws (#1219)
Browse files Browse the repository at this point in the history
  • Loading branch information
ogonkov authored Dec 25, 2023
1 parent e9c092d commit 20756bc
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,15 @@ export class Table<I extends TableDataItem = Record<string, string>> extends Rea

return (
<colgroup>
{columnsStyles.map(({width}, index) => (
<col style={{width}} key={columns[index].id} />
))}
{columnsStyles.flatMap(({width}, index) => {
const key = columns[index]?.id;

if (!key) {
return [];
}

return <col style={{width}} key={key} />;
})}
</colgroup>
);
}
Expand Down Expand Up @@ -553,10 +559,11 @@ export class Table<I extends TableDataItem = Record<string, string>> extends Rea
return style;
}

private getCellStyles({
width: _width,
...styles
}: React.CSSProperties): React.CSSProperties | undefined {
private getCellStyles(
columnStyles: React.CSSProperties | undefined,
): React.CSSProperties | undefined {
const {width: _width, ...styles} = columnStyles || {};

return Object.keys(styles).length ? styles : undefined;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React from 'react';

import {render, screen} from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import {Table, TableColumnConfig} from '../../../Table';
import {TableSettingsData, withTableSettings} from '../withTableSettings';

const item = {name: 'John Doe', occupation: 'Worker'};

const TableWithSettings = withTableSettings<typeof item>(Table);

const columns: TableColumnConfig<typeof item>[] = [
{
id: 'name',
},
{
id: 'occupation',
},
];

const data = [item];

const settings: TableSettingsData = columns.map((column) => ({id: column.id, isSelected: true}));

test('should change table columns', async () => {
const updateSettings = jest.fn();

render(
<TableWithSettings
columns={columns}
data={data}
settings={settings}
updateSettings={updateSettings}
/>,
);

await userEvent.click(screen.getByRole('button', {name: 'Table settings'}));
await userEvent.click(await screen.findByRole('button', {name: 'occupation'}));
await userEvent.click(screen.getByRole('button', {name: 'Apply'}));

expect(updateSettings).toHaveBeenCalledWith([
{
id: 'name',
isSelected: true,
},
{
id: 'occupation',
isSelected: false,
},
]);
});
3 changes: 3 additions & 0 deletions src/components/Table/hoc/withTableSettings/i18n/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"label_settings": "Table settings"
}
8 changes: 8 additions & 0 deletions src/components/Table/hoc/withTableSettings/i18n/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {addComponentKeysets} from '../../../../utils/addComponentKeysets';

import en from './en.json';
import ru from './ru.json';

const COMPONENT = 'withTableSettings';

export default addComponentKeysets({en, ru}, COMPONENT);
3 changes: 3 additions & 0 deletions src/components/Table/hoc/withTableSettings/i18n/ru.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"label_settings": "Настройки таблицы"
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {actionsColumnId, enhanceSystemColumn} from '../withTableActions/withTabl
import {selectionColumnId} from '../withTableSelection/withTableSelection';

import {TableColumnSetup} from './TableColumnSetup/TableColumnSetup';
import i18n from './i18n';

import './withTableSettings.scss';

Expand Down Expand Up @@ -191,6 +192,7 @@ export function withTableSettings<I extends TableDataItem, E extends {} = {}>(
<Button
view="flat"
className={b('settings-button')}
extraProps={{'aria-label': i18n('label_settings')}}
onClick={onClick}
>
<Icon data={Gear} />
Expand Down

0 comments on commit 20756bc

Please sign in to comment.