From a8581a7cd0666f6c9044bc83cb56ded8ff5a8a25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Wilby?= Date: Sun, 31 Mar 2024 08:30:48 +0200 Subject: [PATCH] feat: add new attribute 'hiddenColumns' to configure which columns should hide in autogrid --- packages/ts/react-crud/src/autogrid.tsx | 23 +++++++- packages/ts/react-crud/test/autogrid.spec.tsx | 56 +++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/packages/ts/react-crud/src/autogrid.tsx b/packages/ts/react-crud/src/autogrid.tsx index 0108def65c..f8668ee85e 100644 --- a/packages/ts/react-crud/src/autogrid.tsx +++ b/packages/ts/react-crud/src/autogrid.tsx @@ -57,7 +57,8 @@ interface AutoGridOwnProps { * * By default, the grid shows columns for all properties of the model which * have a type that is supported. Use the `visibleColumns` option to customize - * which columns to show and in which order. + * which columns to show and in which order. Use the `hiddenColumns` option to + * hide certain columns and to keep the default order. */ model: DetachedModelConstructor>; /** @@ -85,6 +86,12 @@ interface AutoGridOwnProps { * 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. */ @@ -136,6 +143,7 @@ export type AutoGridProps = GridProps & Readonly; @@ -258,6 +266,17 @@ function useColumns( 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; + }); + } + if (options.rowNumbers) { columns = [ , @@ -292,6 +311,7 @@ function AutoGridInner( itemIdProperty, experimentalFilter, visibleColumns, + hiddenColumns, noHeaderFilters, customColumns, columnOptions, @@ -351,6 +371,7 @@ function AutoGridInner( const properties = visibleColumns ? modelInfo.getProperties(visibleColumns) : getDefaultProperties(modelInfo); const children = useColumns(properties, setHeaderFilter, { visibleColumns, + hiddenColumns, noHeaderFilters, customColumns, columnOptions, diff --git a/packages/ts/react-crud/test/autogrid.spec.tsx b/packages/ts/react-crud/test/autogrid.spec.tsx index a4adaa0f71..dff28ff364 100644 --- a/packages/ts/react-crud/test/autogrid.spec.tsx +++ b/packages/ts/react-crud/test/autogrid.spec.tsx @@ -1103,6 +1103,27 @@ describe('@vaadin/hilla-react-crud', () => { await assertColumns(grid, 'email', 'firstName'); }); + it('should hide configured columns and keep default order', async () => { + const grid = await GridController.init( + render(), + user, + ); + await assertColumns( + grid, + 'firstName', + 'lastName', + 'email', + 'someInteger', + 'someDecimal', + 'vip', + 'shiftStart', + 'appointmentTime', + 'address.street', + 'address.city', + 'department', + ); + }); + it('uses custom column options on top of the type defaults', async () => { const NameRenderer = ({ item }: { item: Person }): JSX.Element => {item.firstName.toUpperCase()}; const StreetRenderer = ({ item }: { item: Person }): JSX.Element => ( @@ -1320,6 +1341,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( + , + , + ]} + />, + ), + 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;