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

feat: add new attribute 'hiddenColumns' to configure which columns sh… #2279

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 20 additions & 0 deletions packages/ts/react-crud/src/autogrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@
* can be specified using dot notation, e.g. `address.street`.
*/
visibleColumns?: string[];
/**
* Allows to customize which columns to hide. This must be an array of property
* names that are defined in the model. Nested properties can be specified using
* dot notation, e.g. `address.street`.
*/
hiddenColumns?: string[];
/**
* Disables header filters, which are otherwise enabled by default.
*/
Expand Down Expand Up @@ -136,6 +142,7 @@

interface ColumnConfigurationOptions {
visibleColumns?: string[];
hiddenColumns?: string[];
noHeaderFilters?: boolean;
customColumns?: JSX.Element[];
columnOptions?: Record<string, ColumnOptions>;
Expand Down Expand Up @@ -258,6 +265,17 @@

columns = addCustomColumns(columns, options, setColumnFilter);

// When using `hiddenColumns` option, remove columns to hide using their `key`
if (options.hiddenColumns) {
columns = columns.filter((column) => {
const { key } = column;
if (key) {
return !options.hiddenColumns?.includes(key);
}
return true;

Check warning on line 275 in packages/ts/react-crud/src/autogrid.tsx

View check run for this annotation

Codecov / codecov/patch

packages/ts/react-crud/src/autogrid.tsx#L275

Added line #L275 was not covered by tests
});
rbrki07 marked this conversation as resolved.
Show resolved Hide resolved
}

if (options.rowNumbers) {
columns = [
<GridColumn key="rownumbers" width="4em" flexGrow={0} renderer={AutoGridRowNumberRenderer}></GridColumn>,
Expand Down Expand Up @@ -292,6 +310,7 @@
itemIdProperty,
experimentalFilter,
visibleColumns,
hiddenColumns,
noHeaderFilters,
customColumns,
columnOptions,
Expand Down Expand Up @@ -351,6 +370,7 @@
const properties = visibleColumns ? modelInfo.getProperties(visibleColumns) : getDefaultProperties(modelInfo);
const children = useColumns(properties, setHeaderFilter, {
visibleColumns,
hiddenColumns,
noHeaderFilters,
customColumns,
columnOptions,
Expand Down
82 changes: 81 additions & 1 deletion packages/ts/react-crud/test/autogrid.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ describe('@vaadin/hilla-react-crud', () => {
expect(grid.getBodyCellContent(0, 0)).to.have.rendered.text('IT');
});

it('should ignore unknown columns', async () => {
it('should ignore unknown columns when using visibleColumns', async () => {
const grid = await GridController.init(
render(
<TestAutoGrid visibleColumns={['foo', 'email', 'bar', 'firstName', 'address.foo', 'department.foo']} />,
Expand All @@ -1103,6 +1103,51 @@ describe('@vaadin/hilla-react-crud', () => {
await assertColumns(grid, 'email', 'firstName');
});

it('should hide columns and keep default order', async () => {
const grid = await GridController.init(
render(<TestAutoGrid hiddenColumns={['gender', 'birthDate', 'address.country']} />),
user,
);
await assertColumns(
grid,
'firstName',
'lastName',
'email',
'someInteger',
'someDecimal',
'vip',
'shiftStart',
'appointmentTime',
'address.street',
'address.city',
'department',
);
});

it('should ignore unknown columns when using hiddenColumns', async () => {
const grid = await GridController.init(
render(
<TestAutoGrid hiddenColumns={['foo', 'gender', 'bar', 'birthDate', 'address.foo', 'department.foo']} />,
),
user,
);
await assertColumns(
grid,
'firstName',
'lastName',
'email',
'someInteger',
'someDecimal',
'vip',
'shiftStart',
'appointmentTime',
'address.street',
'address.city',
'address.country',
'department',
);
});

it('uses custom column options on top of the type defaults', async () => {
const NameRenderer = ({ item }: { item: Person }): JSX.Element => <span>{item.firstName.toUpperCase()}</span>;
const StreetRenderer = ({ item }: { item: Person }): JSX.Element => (
Expand Down Expand Up @@ -1320,6 +1365,41 @@ describe('@vaadin/hilla-react-crud', () => {
expect(grid.getBodyCellContent(0, 0)).to.have.rendered.text('Jane Love');
});

it('should hide configured columns and render remaining columns including custom columns at the end', async () => {
const grid = await GridController.init(
render(
<TestAutoGrid
noHeaderFilters
hiddenColumns={[
'email',
'someInteger',
'someDecimal',
'vip',
'shiftStart',
'appointmentTime',
'address.street',
'address.city',
'address.country',
'department',
'secondFullName',
]}
customColumns={[
<GridColumn key="fullName" header="Full name" autoWidth renderer={FullNameRenderer}></GridColumn>,
<GridColumn
key="secondFullName"
header="Second full name"
autoWidth
renderer={FullNameHyphenRenderer}
></GridColumn>,
]}
/>,
),
user,
);
await assertColumnsOrder(grid, 'firstName', 'lastName', 'gender', 'birthDate', 'fullName');
expect(grid.getBodyCellContent(0, 4)).to.have.rendered.text('Jane Love');
});

describe('with header filters', () => {
let clock: sinon.SinonFakeTimers;

Expand Down
Loading