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

Change hover state condition #105

Merged
merged 3 commits into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
64 changes: 64 additions & 0 deletions src/components/Table/Table.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,70 @@ Use the different building block to correctly structure your table:
</Table>
</Playground>

### Table with click events

<Playground>
<Table>
<Table.Header>
<Table.Cell>
Course
</Table.Cell>
<Table.Cell>
Length
</Table.Cell>
<Table.Cell>
Status
</Table.Cell>
</Table.Header>
<Table.Body>
<Table.Row onClick={() => {alert('Row clicked');}}>
<Table.Cell>
Introduction To React
</Table.Cell>
<Table.Cell>
2h 27m
</Table.Cell>
<Table.Cell>
<CheckCircle /> Finished
</Table.Cell>
</Table.Row>
<Table.Row onClick={() => {alert('Row clicked');}}>
<Table.Cell>
Introduction To Docker
</Table.Cell>
<Table.Cell>
33m
</Table.Cell>
<Table.Cell>
<Circle /> Not started
</Table.Cell>
</Table.Row>
<Table.Row onClick={() => {alert('Row clicked');}}>
<Table.Cell>
Introduction To Python
</Table.Cell>
<Table.Cell>
1h 45m
</Table.Cell>
<Table.Cell>
<Circle /> Not started
</Table.Cell>
</Table.Row>
<Table.Row onClick={() => {alert('Row clicked');}}>
<Table.Cell>
Introduction To Ventura
</Table.Cell>
<Table.Cell>
15m
</Table.Cell>
<Table.Cell>
<PauseCircle /> In progress
</Table.Cell>
</Table.Row>
</Table.Body>
</Table>
</Playground>

## API

### Table
Expand Down
40 changes: 39 additions & 1 deletion src/components/Table/Table.test.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -44,4 +44,42 @@ describe('Table', () => {
const { asFragment } = render(component);
expect(asFragment()).toMatchSnapshot();
});

test('onClick property', () => {
const onClickFn = jest.fn();

const component = (
<Table>
<Table.Header>
<Table.Cell>Course</Table.Cell>
<Table.Cell>Length</Table.Cell>
<Table.Cell>Status</Table.Cell>
</Table.Header>
<Table.Body>
{courses.map((course, index) => {
return (
<Table.Row
key={course.name}
name={index === 0 ? 'click' : undefined}
onClick={onClickFn}
>
<Table.Cell>{course.name}</Table.Cell>
<Table.Cell>{course.length}</Table.Cell>
<Table.Cell>{course.status}</Table.Cell>
</Table.Row>
);
})}
</Table.Body>
</Table>
);

const { getByTestId } = render(component);
const button = getByTestId('row-click');

fireEvent.click(button);
expect(onClickFn).toHaveBeenCalledTimes(1);

// const { asFragment } = render(component);
// expect(asFragment()).toMatchSnapshot();
});
});
2 changes: 1 addition & 1 deletion src/components/Table/TableCell/TableCell.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
}

.headerCell {
background-color: var(--color-primary-hover);
background-color: var(--color-primary);
height: 36px;
line-height: 36px;
padding-left: 14px;
Expand Down
3 changes: 2 additions & 1 deletion src/components/Table/TableRow/TableRow.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
background-color: var(--color-primary);
}

.row:hover td {
.hoverable:hover td {
background-color: var(--color-primary-hover);
cursor: pointer;
}
18 changes: 14 additions & 4 deletions src/components/Table/TableRow/TableRow.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import React from 'react';
import React, { MouseEventHandler } from 'react';
import classNames from 'classnames';

import styles from './TableRow.module.css';

type Props = {
className?: string;
children?: React.ReactNode;
onClick?: MouseEventHandler<HTMLTableRowElement>;
name?: string;
};

const TableRow: React.FC<Props> = ({ className, children }: Props) => {
const tableClassNames = classNames(styles.row, className);
const TableRow: React.FC<Props> = ({ className, children, onClick, name }: Props) => {
const tableClassNames = classNames(
styles.row,
{ [styles.hoverable]: Boolean(onClick) },
className,
);

return <tr className={tableClassNames}>{children}</tr>;
return (
<tr onClick={onClick} className={tableClassNames} data-testid={`row-${name}`}>
{children}
</tr>
);
};

export default TableRow;
7 changes: 7 additions & 0 deletions src/components/Table/__snapshots__/Table.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ exports[`Table default snapshot 1`] = `
<tbody>
<tr
class="row"
data-testid="row-undefined"
>
<td
class="cellAlignLeft cell"
Expand All @@ -46,6 +47,7 @@ exports[`Table default snapshot 1`] = `
</tr>
<tr
class="row"
data-testid="row-undefined"
>
<td
class="cellAlignLeft cell"
Expand All @@ -65,6 +67,7 @@ exports[`Table default snapshot 1`] = `
</tr>
<tr
class="row"
data-testid="row-undefined"
>
<td
class="cellAlignLeft cell"
Expand All @@ -84,6 +87,7 @@ exports[`Table default snapshot 1`] = `
</tr>
<tr
class="row"
data-testid="row-undefined"
>
<td
class="cellAlignLeft cell"
Expand All @@ -103,6 +107,7 @@ exports[`Table default snapshot 1`] = `
</tr>
<tr
class="row"
data-testid="row-undefined"
>
<td
class="cellAlignLeft cell"
Expand All @@ -122,6 +127,7 @@ exports[`Table default snapshot 1`] = `
</tr>
<tr
class="row"
data-testid="row-undefined"
>
<td
class="cellAlignLeft cell"
Expand All @@ -141,6 +147,7 @@ exports[`Table default snapshot 1`] = `
</tr>
<tr
class="row"
data-testid="row-undefined"
>
<td
class="cellAlignLeft cell"
Expand Down