Skip to content
This repository has been archived by the owner on Oct 19, 2021. It is now read-only.

Commit

Permalink
fix(DataTable): update data-table styles and add description prop (#2127
Browse files Browse the repository at this point in the history
)

* feat: update data-table styles and add description prop

* fix: update snapshots

* chore: remove old icons

* chore: update snapshots

* feat: Add expansion and sorting functionality

* test: update snapshots and tests

* docs: update default table to be sortable

* feat: add class to parent/sibling on hover

* test: update snapshots

* fix: update sort prop to match header

* fix: use previousElementSibling and combine event handlers

* fix: don't use toggle
  • Loading branch information
vpicone authored Apr 5, 2019
1 parent ac870a6 commit 4196a39
Show file tree
Hide file tree
Showing 36 changed files with 1,072 additions and 512 deletions.
15 changes: 11 additions & 4 deletions src/components/DataTable/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ export const Table = ({
children,
short,
shouldShowBorder,
isSortable,
...other
}) => {
const componentClass = cx(`${prefix}--data-table-v2`, className, {
[`${prefix}--data-table-v2--zebra`]: zebra,
[`${prefix}--data-table-v2--short`]: short,
[`${prefix}--data-table-v2--no-border`]: !shouldShowBorder,
const componentClass = cx(`${prefix}--data-table`, className, {
[`${prefix}--data-table--zebra`]: zebra,
[`${prefix}--data-table--short`]: short,
[`${prefix}--data-table--sort`]: isSortable,
[`${prefix}--data-table--no-border`]: !shouldShowBorder,
});
return (
<table {...other} className={componentClass}>
Expand All @@ -48,6 +50,11 @@ Table.propTypes = {
*/
short: PropTypes.bool,

/**
* `false` Applies styles for data tables with sorting functionality.
*/
isSortable: PropTypes.bool,

/**
* `true` for data table without borders.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/components/DataTable/TableBatchAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

import PropTypes from 'prop-types';
import React from 'react';
import { iconAddSolid } from 'carbon-icons';
import iconAddSolid from '@carbon/icons-react/lib/add--filled/16';
import Button from '../Button';

const TableBatchAction = props => (
<Button small kind="ghost" icon={iconAddSolid} {...props} />
<Button small kind="ghost" renderIcon={iconAddSolid} {...props} />
);

TableBatchAction.propTypes = {
Expand Down
24 changes: 21 additions & 3 deletions src/components/DataTable/TableContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,27 @@ import { settings } from 'carbon-components';

const { prefix } = settings;

const TableContainer = ({ className, children, title, ...rest }) => {
const TableContainer = ({
className,
children,
title,
description,
...rest
}) => {
const tableContainerClasses = cx(
className,
`${prefix}--data-table-v2-container`
`${prefix}--data-table-container`
);
return (
<div {...rest} className={tableContainerClasses}>
{title && <h4 className={`${prefix}--data-table-v2-header`}>{title}</h4>}
{title && (
<div className={`${prefix}--data-table-header`}>
<h4 className={`${prefix}--data-table-header__title`}>{title}</h4>
<p className={`${prefix}--data-table-header__description`}>
{description}
</p>
</div>
)}
{children}
</div>
);
Expand All @@ -32,6 +45,11 @@ TableContainer.propTypes = {
* Provide a title for the Table
*/
title: PropTypes.node,

/**
* Optional description text for the Table
*/
description: PropTypes.node,
};

export default TableContainer;
29 changes: 9 additions & 20 deletions src/components/DataTable/TableExpandRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
import cx from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';
import { iconChevronRight } from 'carbon-icons';
import ChevronRight16 from '@carbon/icons-react/lib/chevron--right/16';
import { settings } from 'carbon-components';
import Icon from '../Icon';
import TableCell from './TableCell';
import { componentsX } from '../../internal/FeatureFlags';

const { prefix } = settings;

Expand All @@ -30,9 +27,9 @@ const TableExpandRow = ({
}) => {
const className = cx(
{
[`${prefix}--parent-row-v2`]: true,
[`${prefix}--expandable-row-v2`]: isExpanded,
[`${prefix}--data-table-v2--selected`]: isSelected,
[`${prefix}--parent-row`]: true,
[`${prefix}--expandable-row`]: isExpanded,
[`${prefix}--data-table--selected`]: isSelected,
},
rowClassName
);
Expand All @@ -41,26 +38,18 @@ const TableExpandRow = ({
return (
<tr {...rest} className={className} data-parent-row>
<TableCell
className={`${prefix}--table-expand-v2`}
className={`${prefix}--table-expand`}
data-previous-value={previousValue}
headers={expandHeader}>
<button
className={`${prefix}--table-expand-v2__button`}
className={`${prefix}--table-expand__button`}
onClick={onExpand}
title={expandIconDescription}
aria-label={ariaLabel}>
{componentsX ? (
<ChevronRight16
className={`${prefix}--table-expand-v2__svg`}
aria-label={expandIconDescription}
/>
) : (
<Icon
className={`${prefix}--table-expand-v2__svg`}
icon={iconChevronRight}
description={expandIconDescription}
/>
)}
<ChevronRight16
className={`${prefix}--table-expand__svg`}
aria-label={expandIconDescription}
/>
</button>
</TableCell>
{children}
Expand Down
36 changes: 32 additions & 4 deletions src/components/DataTable/TableExpandedRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,43 @@

import cx from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';
import React, { useRef } from 'react';
import { settings } from 'carbon-components';
import TableCell from './TableCell';

const { prefix } = settings;

const TableExpandedRow = ({
className: customClassName,
children,
colSpan,
...rest
}) => {
const className = cx(`${prefix}--expandable-row-v2`, customClassName);
const rowRef = useRef(null);
const className = cx(`${prefix}--expandable-row`, customClassName);

const toggleParentHoverClass = eventType => {
if (rowRef && rowRef.current && rowRef.current.previousElementSibling) {
const parentNode = rowRef.current.previousElementSibling;
if (eventType === 'enter') {
parentNode.classList.add(`${prefix}--expandable-row--hover`);
} else {
parentNode.classList.remove(`${prefix}--expandable-row--hover`);
}
}
};

return (
<tr {...rest} className={className} data-child-row>
{children}
<tr
ref={rowRef}
onMouseEnter={() => toggleParentHoverClass('enter')}
onMouseLeave={() => toggleParentHoverClass('leave')}
{...rest}
className={className}
data-child-row>
<TableCell colSpan={colSpan}>
<div className={`${prefix}--child-row-inner-container`}>{children}</div>
</TableCell>
</tr>
);
};
Expand All @@ -35,6 +58,11 @@ TableExpandedRow.propTypes = {
* Specify an optional className to be applied to the container node
*/
className: PropTypes.string,

/**
* The width of the expanded row's internal cell
*/
colSpan: PropTypes.number.isRequired,
};

export default TableExpandedRow;
52 changes: 23 additions & 29 deletions src/components/DataTable/TableHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
import cx from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';
import { iconCaretUp } from 'carbon-icons';
import { settings } from 'carbon-components';
import Icon from '../Icon';
import CaretUpGlyph from '@carbon/icons-react/lib/caret--up';
import Arrow from '@carbon/icons-react/lib/arrow--up/20';
import Arrows from '@carbon/icons-react/lib/arrows--vertical/20';
import { sortStates } from './state/sorting';
import { componentsX } from '../../internal/FeatureFlags';

const { prefix } = settings;

Expand Down Expand Up @@ -67,10 +65,10 @@ const TableHeader = ({
}

const className = cx(headerClassName, {
[`${prefix}--table-sort-v2`]: true,
[`${prefix}--table-sort-v2--active`]:
[`${prefix}--table-sort`]: true,
[`${prefix}--table-sort--active`]:
isSortHeader && sortDirection !== sortStates.NONE,
[`${prefix}--table-sort-v2--ascending`]:
[`${prefix}--table-sort--ascending`]:
isSortHeader && sortDirection === sortStates.DESC,
});
const ariaSort = !isSortHeader ? 'none' : sortDirections[sortDirection];
Expand All @@ -79,28 +77,24 @@ const TableHeader = ({
<th scope={scope} className={headerClassName} aria-sort={ariaSort}>
<button className={className} onClick={onClick} {...rest}>
<span className={`${prefix}--table-header-label`}>{children}</span>
{componentsX ? (
<CaretUpGlyph
className={`${prefix}--table-sort-v2__icon`}
aria-label={t('carbon.table.header.icon.description', {
header: children,
sortDirection,
isSortHeader,
sortStates,
})}
/>
) : (
<Icon
className={`${prefix}--table-sort-v2__icon`}
icon={iconCaretUp}
description={t('carbon.table.header.icon.description', {
header: children,
sortDirection,
isSortHeader,
sortStates,
})}
/>
)}
<Arrow
className={`${prefix}--table-sort__icon`}
aria-label={t('carbon.table.header.icon.description', {
header: children,
sortDirection,
isSortHeader,
sortStates,
})}
/>
<Arrows
className={`${prefix}--table-sort__icon-unsorted`}
aria-label={t('carbon.table.header.icon.description', {
header: children,
sortDirection,
isSortHeader,
sortStates,
})}
/>
</button>
</th>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/DataTable/TableRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const TableRow = props => {
// Remove unnecessary props if provided to this component, these are
// only useful in `TableExpandRow`
const className = cx(props.className, {
[`${prefix}--data-table-v2--selected`]: props.isSelected,
[`${prefix}--data-table--selected`]: props.isSelected,
});
const cleanProps = {
...omit(props, ['ariaLabel', 'onExpand', 'isExpanded', 'isSelected']),
Expand Down
7 changes: 6 additions & 1 deletion src/components/DataTable/TableSelectAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
import PropTypes from 'prop-types';
import React from 'react';
import InlineCheckbox from '../InlineCheckbox';
import cx from 'classnames';

import { settings } from 'carbon-components';

const { prefix } = settings;

const TableSelectAll = ({
ariaLabel,
Expand All @@ -19,7 +24,7 @@ const TableSelectAll = ({
disabled,
className,
}) => (
<th scope="col" className={className}>
<th scope="col" className={cx(`${prefix}--table-column-checkbox`, className)}>
<InlineCheckbox
ariaLabel={ariaLabel}
checked={checked}
Expand Down
18 changes: 9 additions & 9 deletions src/components/DataTable/__tests__/DataTable-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
*/

import React from 'react';
import { iconDownload, iconEdit, iconSettings } from 'carbon-icons';
import Download16 from '@carbon/icons-react/lib/download/16';
import Edit16 from '@carbon/icons-react/lib/edit/16';
import Settings16 from '@carbon/icons-react/lib/settings/16';
import Button from '../../Button';
import DataTable, {
Table,
Expand Down Expand Up @@ -82,17 +84,17 @@ describe('DataTable', () => {
<TableToolbarSearch onChange={onInputChange} id="custom-id" />
<TableToolbarContent>
<TableToolbarAction
icon={iconDownload}
renderIcon={Download16}
iconDescription="Download"
onClick={jest.fn()}
/>
<TableToolbarAction
icon={iconEdit}
renderIcon={Edit16}
iconDescription="Edit"
onClick={jest.fn()}
/>
<TableToolbarAction
icon={iconSettings}
renderIcon={Settings16}
iconDescription="Settings"
onClick={jest.fn()}
/>
Expand Down Expand Up @@ -533,11 +535,9 @@ describe('DataTable', () => {
))}
</TableExpandRow>
{row.isExpanded && (
<TableExpandedRow>
<TableCell colSpan={headers.length + 3}>
<h1>Expandable row content</h1>
<p>Description here</p>
</TableCell>
<TableExpandedRow colSpan={headers.length + 3}>
<h1>Expandable row content</h1>
<p>Description here</p>
</TableExpandedRow>
)}
</React.Fragment>
Expand Down
4 changes: 2 additions & 2 deletions src/components/DataTable/__tests__/Table-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('DataTable.Table', () => {

it('should support disable zebra stripe', () => {
const wrapper = shallow(<Table zebra={false} />);
expect(wrapper.hasClass('bx--data-table-v2')).toBe(true);
expect(wrapper.hasClass('bx--data-table-v2--zebra')).toBe(false);
expect(wrapper.hasClass('bx--data-table')).toBe(true);
expect(wrapper.hasClass('bx--data-table--zebra')).toBe(false);
});
});
Loading

0 comments on commit 4196a39

Please sign in to comment.