Skip to content

Commit

Permalink
fix(Table): Change hover cell to hover row (#840)
Browse files Browse the repository at this point in the history
* fix(SalesTable): Change hover cell to hover row

* feat(Table): Add isHoverable props to project

* doc(Table): Update doc

* test(Table): Add isHoverable to test

* fix(Table): Fix sticky footer cliping
  • Loading branch information
Artikodin authored and sun-tea committed Oct 28, 2019
1 parent 2ae03ae commit 752ecf4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/Table/__stories__/Table.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ storiesOf('Table', module)
})
.add('default', () => {
const striped = boolean('Striped', false, 'State');
const isHoverable = boolean('Hoverable', false, 'State');
const selectableRow = boolean('Selectable row', false, 'State');
const dishRowSortable = boolean('Dish row is sortable', true, 'State');
const priceRowSortable = boolean('Price row is sortable', true, 'State');
Expand Down Expand Up @@ -94,12 +95,14 @@ storiesOf('Table', module)
colsDef={getColsDef(taxCountryCodeValue)}
rowsDef={rowsDef}
striped={striped}
isHoverable={isHoverable}
/>
</Wrapper>
);
})
.add('Scrollable table', () => {
const striped = boolean('Striped', false, 'State');
const isHoverable = boolean('Hoverable', false, 'State');
const selectableRow = boolean('Selectable row', false, 'State');
const dishRowSortable = boolean('Dish row is sortable', true, 'State');
const priceRowSortable = boolean('Price row is sortable', true, 'State');
Expand Down Expand Up @@ -287,6 +290,7 @@ storiesOf('Table', module)
<Wrapper>
<Table
isScrollable
isHoverable={isHoverable}
height="40rem"
data={data}
dataTotal={dataTotal}
Expand Down
17 changes: 17 additions & 0 deletions src/Table/__tests__/Table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ StubComponent.displayName = 'StubComponent';
const getColsDef = (taxCountryCode = 'fr') => [
{
title: 'DISH',
isRowHeader: true,
value: d => d.code,
isSortable: true,
align: 'left',
Expand Down Expand Up @@ -249,4 +250,20 @@ describe('<Table />', () => {

expect(tableContainer).toHaveStyleRule('overflow', 'scroll');
});

test('should table be hoverable', () => {
const { getByText, getAllByTestId } = render(
<Table isHoverable height="10rem" colsDef={getColsDef()} data={data} />,
);

const sortedBodyRows = getAllByTestId('body-row');

expect(sortedBodyRows[0]).toHaveTextContent(/tartare de boeuf/i);

fireEvent.mouseOver(sortedBodyRows[0]);

const tartareRow = getByText(/tartare de boeuf/i);

expect(tartareRow).toHaveStyleRule(`background-color: ${Theme.palette.veryLightGrey}`);
});
});
30 changes: 23 additions & 7 deletions src/Table/elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ export const Body = styled.tbody`
&:last-child {
padding-right: 3rem;
}
&:hover {
background-color: ${({ theme: { palette } }) => palette.veryLightGrey};
}
}
`;

Expand Down Expand Up @@ -163,6 +160,17 @@ export const BodyRow = styled(Row)`
box-shadow: inset 3px 0px 0 0px ${({ theme: { palette } }) => palette.primary.default};
}
`};
${({ isHoverable }) =>
isHoverable &&
css`
&:hover {
& > th,
& > td {
background-color: ${({ theme: { palette } }) => palette.veryLightGrey};
}
}
`}
`;

export const RowHeader = styled.th`
Expand Down Expand Up @@ -198,10 +206,6 @@ export const Footer = styled.tfoot`
td {
font-feature-settings: 'tnum';
&:hover {
background-color: ${({ theme: { palette } }) => palette.veryLightGrey};
}
}
th,
Expand All @@ -220,6 +224,7 @@ export const Footer = styled.tfoot`
top: 0;
width: 100%;
border-top: 1px solid ${({ theme: { palette } }) => palette.veryLightBlue};
box-sizing: border-box;
}
height: 5.2rem;
Expand All @@ -246,6 +251,17 @@ export const Footer = styled.tfoot`
td:last-of-type {
padding: 0 3rem 0 0.5rem;
}
${({ isHoverable }) =>
isHoverable &&
css`
tr:hover {
& > th,
& > td {
background-color: ${({ theme: { palette } }) => palette.veryLightGrey};
}
}
`}
`;

// Table Container
Expand Down
13 changes: 11 additions & 2 deletions src/Table/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class Table extends PureComponent {
colsDef,
data,
isScrollable,
isHoverable,
rowsDef: { selectable },
striped,
} = this.props;
Expand Down Expand Up @@ -184,6 +185,7 @@ class Table extends PureComponent {
selected={selected === key}
striped={striped}
onClick={() => this.handleRowSelect(item, key)}
isHoverable={isHoverable}
>
{colsDef.map(({ isRowHeader, value, format, align }, columnIndex) =>
isRowHeader ? (
Expand Down Expand Up @@ -212,10 +214,10 @@ class Table extends PureComponent {
* @return {jsx}
*/
renderFooter() {
const { colsDef, dataTotal, isScrollable } = this.props;
const { colsDef, dataTotal, isScrollable, isHoverable } = this.props;

return (
<Footer data-testid="footer-row" isScrollable={isScrollable}>
<Footer data-testid="footer-row" isScrollable={isScrollable} isHoverable={isHoverable}>
<tr>
{colsDef.map(({ isRowHeader, total, format, align }, columnIndex) =>
isRowHeader ? (
Expand Down Expand Up @@ -303,6 +305,12 @@ Table.propTypes = {
**/
isScrollable: bool,

/**
* Define if the table is isHoverable or not.
* When the table is isHoverable if a user hover a row it background change increasing contrast with others improving readability.
**/
isHoverable: bool,

/** Rows definition */
rowsDef: shape({
onSelect: func,
Expand All @@ -321,6 +329,7 @@ Table.defaultProps = {
dataTotal: null,
height: 'initial',
isScrollable: false,
isHoverable: false,
rowsDef: {
onSelect: () => {},
selectable: false,
Expand Down

0 comments on commit 752ecf4

Please sign in to comment.