From eb875e765d2e30cfc37956005787a9c92be60960 Mon Sep 17 00:00:00 2001 From: Bram Vanhoutte Date: Tue, 18 Aug 2020 18:41:50 +0200 Subject: [PATCH 1/3] fix(Table): hover state --- src/components/Table/Table.mdx | 64 +++++++++++++++++++ src/components/Table/Table.test.tsx | 40 +++++++++++- .../Table/TableCell/TableCell.module.css | 2 +- .../Table/TableRow/TableRow.module.css | 3 +- src/components/Table/TableRow/TableRow.tsx | 18 ++++-- .../Table/__snapshots__/Table.test.tsx.snap | 7 ++ 6 files changed, 127 insertions(+), 7 deletions(-) diff --git a/src/components/Table/Table.mdx b/src/components/Table/Table.mdx index a3ae1438..e87c9c0f 100644 --- a/src/components/Table/Table.mdx +++ b/src/components/Table/Table.mdx @@ -107,6 +107,70 @@ Use the different building block to correctly structure your table: +### Table with click events + + + + + + Course + + + Length + + + Status + + + + {alert('Row clicked');}}> + + Introduction To React + + + 2h 27m + + + Finished + + + {alert('Row clicked');}}> + + Introduction To Docker + + + 33m + + + Not started + + + {alert('Row clicked');}}> + + Introduction To Python + + + 1h 45m + + + Not started + + + {alert('Row clicked');}}> + + Introduction To Ventura + + + 15m + + + In progress + + + +
+
+ ## API ### Table diff --git a/src/components/Table/Table.test.tsx b/src/components/Table/Table.test.tsx index c6f9aece..5d5cedb0 100644 --- a/src/components/Table/Table.test.tsx +++ b/src/components/Table/Table.test.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import { render } from '@testing-library/react'; +import { fireEvent, render } from '@testing-library/react'; import Table from './Table'; @@ -44,4 +44,42 @@ describe('Table', () => { const { asFragment } = render(component); expect(asFragment()).toMatchSnapshot(); }); + + test('onClick property', () => { + const onClickFn = jest.fn(); + + const component = ( + + + Course + Length + Status + + + {courses.map((course, index) => { + return ( + + {course.name} + {course.length} + {course.status} + + ); + })} + +
+ ); + + const { getByTestId } = render(component); + const button = getByTestId('row-click'); + + fireEvent.click(button); + expect(onClickFn).toHaveBeenCalledTimes(1); + + // const { asFragment } = render(component); + // expect(asFragment()).toMatchSnapshot(); + }); }); diff --git a/src/components/Table/TableCell/TableCell.module.css b/src/components/Table/TableCell/TableCell.module.css index 2d54814d..ff7be9dd 100644 --- a/src/components/Table/TableCell/TableCell.module.css +++ b/src/components/Table/TableCell/TableCell.module.css @@ -28,7 +28,7 @@ } .headerCell { - background-color: var(--color-primary-hover); + background-color: var(--color-primary); height: 36px; line-height: 36px; padding-left: 14px; diff --git a/src/components/Table/TableRow/TableRow.module.css b/src/components/Table/TableRow/TableRow.module.css index 8ffa2f29..6cbb4750 100644 --- a/src/components/Table/TableRow/TableRow.module.css +++ b/src/components/Table/TableRow/TableRow.module.css @@ -2,6 +2,7 @@ background-color: var(--color-primary); } -.row:hover td { +.hoverable:hover td { background-color: var(--color-primary-hover); + cursor: pointer; } diff --git a/src/components/Table/TableRow/TableRow.tsx b/src/components/Table/TableRow/TableRow.tsx index 89ca6f92..566c3c4f 100644 --- a/src/components/Table/TableRow/TableRow.tsx +++ b/src/components/Table/TableRow/TableRow.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { MouseEventHandler } from 'react'; import classNames from 'classnames'; import styles from './TableRow.module.css'; @@ -6,12 +6,22 @@ import styles from './TableRow.module.css'; type Props = { className?: string; children?: React.ReactNode; + onClick?: MouseEventHandler; + name?: string; }; -const TableRow: React.FC = ({ className, children }: Props) => { - const tableClassNames = classNames(styles.row, className); +const TableRow: React.FC = ({ className, children, onClick, name }: Props) => { + const tableClassNames = classNames( + styles.row, + { [styles.hoverable]: Boolean(onClick) }, + className, + ); - return {children}; + return ( + + {children} + + ); }; export default TableRow; diff --git a/src/components/Table/__snapshots__/Table.test.tsx.snap b/src/components/Table/__snapshots__/Table.test.tsx.snap index a8c776cf..77d08a65 100644 --- a/src/components/Table/__snapshots__/Table.test.tsx.snap +++ b/src/components/Table/__snapshots__/Table.test.tsx.snap @@ -27,6 +27,7 @@ exports[`Table default snapshot 1`] = ` Date: Tue, 18 Aug 2020 18:55:24 +0200 Subject: [PATCH 2/3] remove forgotten comment --- src/components/Table/Table.test.tsx | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/components/Table/Table.test.tsx b/src/components/Table/Table.test.tsx index 5d5cedb0..6343a69c 100644 --- a/src/components/Table/Table.test.tsx +++ b/src/components/Table/Table.test.tsx @@ -78,8 +78,5 @@ describe('Table', () => { fireEvent.click(button); expect(onClickFn).toHaveBeenCalledTimes(1); - - // const { asFragment } = render(component); - // expect(asFragment()).toMatchSnapshot(); }); }); From f5ca8d4c4f73b57f9be82fde803104423c64be22 Mon Sep 17 00:00:00 2001 From: Bram Vanhoutte Date: Tue, 18 Aug 2020 18:58:05 +0200 Subject: [PATCH 3/3] remove test-id when no name is given --- src/components/Button/Button.tsx | 2 +- src/components/Button/__snapshots__/Button.test.tsx.snap | 4 ---- src/components/Input/Input.tsx | 2 +- src/components/Table/TableRow/TableRow.tsx | 2 +- src/components/Table/__snapshots__/Table.test.tsx.snap | 7 ------- src/components/TextArea/TextArea.tsx | 2 +- 6 files changed, 4 insertions(+), 15 deletions(-) diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index 008ef908..2c070025 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -58,7 +58,7 @@ const Button: React.FC = ({ type={htmlType} onClick={isLoading || suffixIcon ? undefined : onClick} name={name} - data-testid={`button-${name}`} + data-testid={name && `button-${name}`} > {isLoading ? : prefixIcon} {children} diff --git a/src/components/Button/__snapshots__/Button.test.tsx.snap b/src/components/Button/__snapshots__/Button.test.tsx.snap index 04b22c49..4d9c3271 100644 --- a/src/components/Button/__snapshots__/Button.test.tsx.snap +++ b/src/components/Button/__snapshots__/Button.test.tsx.snap @@ -21,7 +21,6 @@ exports[`Button default snapshot 1`] = `