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(Table): columns change throws #1219

Merged
merged 4 commits into from
Dec 25, 2023
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
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 [];
korvin89 marked this conversation as resolved.
Show resolved Hide resolved
}

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
Loading