Skip to content

Commit

Permalink
feat: QUV-2455 New table rowGroupingRenderer preset
Browse files Browse the repository at this point in the history
  • Loading branch information
santiago.trigo committed Aug 6, 2024
1 parent babed5b commit 0af7f76
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/table/src/columnPresets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export * from './link';
export * from './longText';
export * from './number';
export * from './options';
export * from './rowGrouping';
export * from './text';
9 changes: 9 additions & 0 deletions packages/table/src/columnPresets/rowGrouping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { TColDef } from '../declarations';
import { RowGroupingRenderer } from '../renderers';

export const rowGrouping: TColDef = {
id: 'rowGrouping',
cellRenderer: RowGroupingRenderer,
minWidth: 50,
headerOnFilterPosition: true,
};
8 changes: 8 additions & 0 deletions packages/table/src/facade/RowGrouping/RowGroupingContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as React from 'react';

export type TRowGroupingContext = {
onRowGroupingChange?: (
rowIndex: number,
event: React.FormEvent<Element>,
) => void;
};
1 change: 1 addition & 0 deletions packages/table/src/facade/RowGrouping/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './RowGroupingContext';
1 change: 1 addition & 0 deletions packages/table/src/facade/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './Action';
export * from './Bulk';
export * from './Options';
export * from './RowGrouping';
1 change: 1 addition & 0 deletions packages/table/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './useTableVirtualizationColumn';
export * from './useTableVirtualizationRow';
export * from './useOrderStruct';
export * from './useFilterStruct';
export * from './useRowGrouping';
20 changes: 20 additions & 0 deletions packages/table/src/hooks/useRowGrouping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as React from 'react';

export const useRowGrouping = ({ data, initialSelection = [] }) => {
const [rowSelection, setRowSelection] = React.useState(initialSelection);

const toggle = React.useCallback(
(index: number) => {
setRowSelection(() =>
data.map((d, i) => {
if (index === i) {
d.rowGrouping = !d.rowGrouping;
}
return d;
}),
);
},
[data],
);
return { rowSelection, toggle };
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Meta, StoryObj } from '@storybook/react';

import { RowGroupingRenderer } from './RowGroupingRenderer';

const meta: Meta<typeof RowGroupingRenderer> = {
title: 'Components/Layout/Table/Renderers/rowGroupingRenderer',
component: RowGroupingRenderer,
};

export default meta;
type Story = StoryObj<typeof RowGroupingRenderer>;

export const Base: Story = {
args: {
value: false,
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as React from 'react';

import { IconButtonCollapse } from '@devoinc/genesys-ui';

import type { TCellRenderer } from '../../declarations';
import { TRowGroupingContext } from '../../facade';

export const RowGroupingRenderer: React.FC<TCellRenderer> = ({
value,
rowIndex,
colDef,
}) => {
const context = colDef?.context as TRowGroupingContext;

return (
<IconButtonCollapse
onClick={(event) => {
if (context?.onRowGroupingChange) {
context.onRowGroupingChange(rowIndex, event);
}
}}
state={value ? 'expanded' : 'enabled'}
/>
);
};
1 change: 1 addition & 0 deletions packages/table/src/renderers/RowGroupingRenderer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './RowGroupingRenderer';
1 change: 1 addition & 0 deletions packages/table/src/renderers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from './BooleanRenderer';
export * from './BulkRenderer';
export * from './LinkRenderer';
export * from './OptionsRenderer';
export * from './RowGroupingRenderer';
export * from './TextRenderer';
70 changes: 70 additions & 0 deletions packages/table/stories/RowGrouping.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import * as React from 'react';
import { Meta, StoryObj } from '@storybook/react';

import { Flex } from '@devoinc/genesys-ui';

import {
orderDataByOrderStruct,
RowGroupingRenderer,
Table,
TColDef,
TextRenderer,
useOrderStruct,
useRowGrouping,
} from '../src';

const meta: Meta<typeof Table> = {
title: 'Components/Layout/Table/RowGrouping',
component: Table,
parameters: {
layout: 'fullscreen',
},
};

export default meta;
type Story = StoryObj<typeof Table>;

const initialData = Array.from({ length: 10 }).map((_, index) => ({
rowGrouping: false,
text: `text ${index}`,
}));

const BulkExample = () => {
const { orderStruct, onSort } = useOrderStruct([
{ id: 'text', sort: 'desc' },
]);
const dataOrdered = [...initialData].sort(orderDataByOrderStruct(orderStruct));

const { rowSelection, toggle } = useRowGrouping({ data: dataOrdered, initialSelection: [] });

return (
<Flex flexDirection="column" gap="cmp-md">
<Flex.Item>
<Table
data={dataOrdered}
onSort={(colDef: TColDef) => {
onSort(colDef.id);
}}
colDefs={[
{
id: 'rowGrouping',
cellRenderer: RowGroupingRenderer,
width: 46,
context: {
rowSelection,
onRowGroupingChange: (rowIndex) => {
toggle(rowIndex);
},
}
},
{ id: 'text', cellRenderer: TextRenderer, headerName: 'Text', sortable: true },
]}
/>
</Flex.Item>
</Flex>
);
};

export const Base: Story = {
render: () => <BulkExample />,
};

0 comments on commit 0af7f76

Please sign in to comment.