Skip to content

Commit 3df2e38

Browse files
author
Hector Arce De Las Heras
committed
Cell background color modification feature added to table
This commit introduces a feature that allows the modification of the background color of table cells. This enhancement provides more flexibility in customizing the appearance of the table, enabling the highlighting of specific cells, rows, or columns for better data visualization and readability.
1 parent 4481ef7 commit 3df2e38

File tree

4 files changed

+129
-8
lines changed

4 files changed

+129
-8
lines changed

src/components/table/__tests__/table.test.tsx

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,84 @@ const mockBase = {
129129
],
130130
};
131131

132+
const mockBaseCustomizable = {
133+
variant: 'DEFAULT',
134+
headerVariant: 'PRIMARY',
135+
footer: { content: <div>Footer</div> },
136+
headers: [
137+
{
138+
id: 'date',
139+
label: 'Date',
140+
config: { hasDivider: true },
141+
},
142+
{
143+
id: 'name',
144+
label: 'Recipient name',
145+
config: { alignHeader: 'left', alignValue: 'left' },
146+
value: value =>
147+
value.surname ? (
148+
<div>
149+
<div>{value.name}</div>
150+
<div>{value.surname}</div>
151+
</div>
152+
) : (
153+
value.name
154+
),
155+
},
156+
{
157+
id: 'routingNumber',
158+
label: 'Routing number',
159+
config: { alignHeader: 'left', alignValue: 'left' },
160+
},
161+
{
162+
id: 'accountNumber',
163+
label: 'Account number',
164+
config: { alignHeader: 'left', alignValue: 'left' },
165+
},
166+
{
167+
id: 'transferMoney',
168+
label: 'Transfer money',
169+
config: { alignHeader: 'center', alignValue: 'center' },
170+
value: () => (
171+
<Button size="LARGE" variant="PRIMARY">
172+
Transfer money
173+
</Button>
174+
),
175+
},
176+
{
177+
id: 'edit',
178+
label: 'Edit',
179+
config: { alignHeader: 'center', alignValue: 'center' },
180+
value: () => 'plug',
181+
},
182+
{
183+
id: 'delete',
184+
label: 'Delete',
185+
config: { alignHeader: 'right' },
186+
value: () => 'edit active',
187+
},
188+
],
189+
values: [
190+
{
191+
date: '18 DIC',
192+
name: 'Michael',
193+
surname: 'Scott',
194+
routingNumber: { value: '113456789', backgroundColor: 'red' },
195+
accountNumber: '****999999',
196+
transactionNumber: '0000',
197+
},
198+
{
199+
date: '18 DIC',
200+
name: 'Dwight',
201+
surname: 'Schrute',
202+
routingNumber: '777456789',
203+
accountNumber: '****999777',
204+
transactionNumber: '7777',
205+
backgroundColor: 'blue',
206+
},
207+
],
208+
};
209+
132210
const mockExpanded = {
133211
variant: 'DEFAULT',
134212
headerVariant: 'PRIMARY',
@@ -311,6 +389,22 @@ describe('Table component', () => {
311389
expect(results).toHaveNoViolations();
312390
});
313391

392+
it('Renders with a valid HTML structure as customizableTable', async () => {
393+
const { container } = renderProvider(
394+
<Table
395+
aria-label={'aria table label'}
396+
captionDescription={'caption description'}
397+
{...mockBaseCustomizable}
398+
/>
399+
);
400+
const results = await axe(container);
401+
const table = screen.getByRole('table');
402+
403+
expect(table).toBeDefined();
404+
expect(container).toHTMLValidate();
405+
expect(results).toHaveNoViolations();
406+
});
407+
314408
it('can have a divider configured from the values', async () => {
315409
const { container } = renderProvider(
316410
<Table

src/components/table/component/tableRow.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const TableRow = (props: ITableRow): JSX.Element => {
3434
dividerValue,
3535
getExpandedAria,
3636
getValue,
37+
getBackgroundColorCellValue,
3738
handleShowExpandedContent,
3839
hasExpandedContentRow,
3940
hasFooter,
@@ -69,7 +70,9 @@ export const TableRow = (props: ITableRow): JSX.Element => {
6970
headerValue.config?.alignHeader?.[props.device] ||
7071
headerValue?.config?.alignHeader
7172
}
72-
customBackgroundColor={props.value.backgroundColor}
73+
customBackgroundColor={
74+
getBackgroundColorCellValue(headerValue) ?? props.value.backgroundColor
75+
}
7376
customWidth={headerValue.config?.width}
7477
data-testid={`${props.dataTestId}Row${props.indexRow}Content${indexHeader}`}
7578
flexWidth={headerValue.config?.flexWidth}

src/components/table/hooks/useContent.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
/* eslint-disable complexity */
22
import * as React from 'react';
33

4-
import { DividerContent, ITableHeader, ITableStandAlone, IValue, TableDividerType } from '../types';
4+
import {
5+
DividerContent,
6+
ITableHeader,
7+
ITableStandAlone,
8+
IValue,
9+
TableDividerType,
10+
ValueConfigType,
11+
} from '../types';
512

613
interface IUseContent extends Omit<ITableStandAlone, 'values' | 'headerVariant'> {
714
hasSomeExpandedContent?: boolean;
@@ -15,6 +22,7 @@ interface IUseContentResponse {
1522
dividerValue: () => null | TableDividerType | unknown;
1623
getExpandedAria: () => string | undefined;
1724
getValue: (headerValue: ITableHeader) => string | JSX.Element | DividerContent;
25+
getBackgroundColorCellValue: (headerValue: ITableHeader) => string | undefined;
1826
handleShowExpandedContent: (value: boolean) => void;
1927
hasExpandedContentRow: boolean;
2028
hasFooter: boolean;
@@ -73,9 +81,23 @@ export const useContent = (props: IUseContent): IUseContentResponse => {
7381
return headerValue.value(props.value);
7482
}
7583

76-
return props.value[headerValue.id]
77-
? (props.value[headerValue.id] as string | JSX.Element)
84+
const cellValue = props.value[headerValue.id]
85+
? (props.value[headerValue.id] as string | JSX.Element | ValueConfigType)
7886
: '-';
87+
if (React.isValidElement(cellValue) || typeof cellValue === 'string') {
88+
return cellValue;
89+
}
90+
return (cellValue as ValueConfigType).value as string | JSX.Element;
91+
};
92+
93+
const getBackgroundColorCellValue = (headerValue: ITableHeader): string | undefined => {
94+
const cellValue = props.value[headerValue.id]
95+
? (props.value[headerValue.id] as string | JSX.Element | ValueConfigType)
96+
: '-';
97+
if (React.isValidElement(cellValue) || typeof cellValue === 'string') {
98+
return;
99+
}
100+
return (cellValue as ValueConfigType).backgroundColor;
79101
};
80102

81103
const hasExpandedContentRow = !!Object.getOwnPropertyDescriptor(props.value, 'expandedContent');
@@ -85,6 +107,7 @@ export const useContent = (props: IUseContent): IUseContentResponse => {
85107
dividerValue,
86108
getExpandedAria,
87109
getValue,
110+
getBackgroundColorCellValue,
88111
handleShowExpandedContent,
89112
hasExpandedContentRow,
90113
hasFooter,

src/components/table/stories/mockTable.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -479,11 +479,11 @@ export const mockCustomizableTable = {
479479
value: (value): string | JSX.Element =>
480480
value.surname ? (
481481
<div>
482-
<div>{value.name}</div>
482+
<div>{value.name.value ?? value.name}</div>
483483
<div>{value.surname}</div>
484484
</div>
485485
) : (
486-
value.name
486+
value.name.value ?? value.name
487487
),
488488
},
489489
{
@@ -538,7 +538,7 @@ export const mockCustomizableTable = {
538538
values: [
539539
{
540540
date: '18 DIC',
541-
name: 'Kevin',
541+
name: { value: 'Kevin' },
542542
surname: 'Malone',
543543
routingNumber: '123456789',
544544
accountNumber: '****999999',
@@ -547,7 +547,7 @@ export const mockCustomizableTable = {
547547
},
548548
{
549549
date: '18 DIC',
550-
name: 'Kevin',
550+
name: { value: 'Dwight', backgroundColor: 'green' },
551551
surname: 'Malone',
552552
routingNumber: '123456789',
553553
accountNumber: '****999999',
@@ -575,6 +575,7 @@ export const mockCustomizableTable = {
575575
routingNumber: '9876132224',
576576
accountNumber: '****5986756',
577577
rowVariant: 'CUSTOMIZABLE_ROW',
578+
delete: { backgroundColor: 'pink' },
578579
},
579580
],
580581
};

0 commit comments

Comments
 (0)