Skip to content

Commit

Permalink
Merge pull request #105 from kodiak-packages/table-changes
Browse files Browse the repository at this point in the history
Change hover state condition
  • Loading branch information
bramvanhoutte authored Aug 18, 2020
2 parents 20b88d4 + f5ca8d4 commit 145b79d
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const Button: React.FC<Props> = ({
type={htmlType}
onClick={isLoading || suffixIcon ? undefined : onClick}
name={name}
data-testid={`button-${name}`}
data-testid={name && `button-${name}`}
>
{isLoading ? <Spinner className={styles.spinner} /> : prefixIcon}
<span className={labelClassNames}>{children}</span>
Expand Down
4 changes: 0 additions & 4 deletions src/components/Button/__snapshots__/Button.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ exports[`Button default snapshot 1`] = `
<DocumentFragment>
<button
class="ventura button typePrimary"
data-testid="button-undefined"
type="button"
>
<span
Expand Down Expand Up @@ -68,7 +67,6 @@ exports[`Button prefix icon button 1`] = `
<DocumentFragment>
<button
class="ventura button typePrimary"
data-testid="button-undefined"
type="button"
>
<svg
Expand Down Expand Up @@ -99,7 +97,6 @@ exports[`Button secondary button 1`] = `
<DocumentFragment>
<button
class="ventura button typeSecondary"
data-testid="button-undefined"
type="button"
>
<span
Expand All @@ -115,7 +112,6 @@ exports[`Button suffix icon button 1`] = `
<DocumentFragment>
<button
class="ventura button typePrimary"
data-testid="button-undefined"
type="button"
>
<span
Expand Down
2 changes: 1 addition & 1 deletion src/components/Input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const Input = React.forwardRef<HTMLInputElement, Props>(
maxLength={maxLength}
ref={ref}
className={inputClassNames}
data-testid={`input-${name}`}
data-testid={name && `input-${name}`}
onBlur={onBlur}
/>
);
Expand Down
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
37 changes: 36 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,39 @@ 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);
});
});
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={name && `row-${name}`}>
{children}
</tr>
);
};

export default TableRow;
2 changes: 1 addition & 1 deletion src/components/TextArea/TextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const TextArea = React.forwardRef<HTMLTextAreaElement, Props>(
maxLength={maxLength}
ref={ref}
className={textareaClassNames}
data-testid={`textarea-${name}`}
data-testid={name && `textarea-${name}`}
onBlur={onBlur}
/>
);
Expand Down

0 comments on commit 145b79d

Please sign in to comment.