Skip to content

Commit

Permalink
feat: bulkactions and tableactions now take functions
Browse files Browse the repository at this point in the history
  • Loading branch information
manny-m committed Sep 22, 2021
1 parent 0b40d3f commit 3c6213d
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 27 deletions.
5 changes: 4 additions & 1 deletion src/DataTable/BulkActions.jsx
Expand Up @@ -6,7 +6,10 @@ import DataTableContext from './DataTableContext';
import Actions from './CollapsibleButtonGroup';

const BulkActions = ({ className }) => {
const { bulkActions } = useContext(DataTableContext);
const { bulkActions, selectedFlatRows } = useContext(DataTableContext);
if (typeof bulkActions === 'function') {
return <div className={classNames('pgn__bulk-actions', className)}>{bulkActions(selectedFlatRows)}</div>;
}
return (
<Actions
className={classNames('pgn__bulk-actions', className)}
Expand Down
5 changes: 4 additions & 1 deletion src/DataTable/TableActions.jsx
Expand Up @@ -8,9 +8,12 @@ import Actions from './CollapsibleButtonGroup';
const TableActions = ({ className }) => {
const tableInstance = useContext(DataTableContext);
const { tableActions } = tableInstance;
if (typeof tableActions === 'function') {
return <div className={classNames('pgn__bulk-actions', className)}>{tableActions()}</div>;
}
return (
<Actions
className={classNames('pgn__table-actions', className)}
className={classNames('pgn__bulk-actions', className)}
actions={tableActions}
tableInstance={tableInstance}
/>
Expand Down
59 changes: 34 additions & 25 deletions src/DataTable/index.jsx
Expand Up @@ -220,31 +220,40 @@ DataTable.propTypes = {
itemCount: PropTypes.number.isRequired,
/** Actions to be performed on selected rows of the table. Called with the selected rows.
* Only displayed if rows are selected. */
bulkActions: PropTypes.arrayOf(PropTypes.shape({
/** Bulk action button text */
buttonText: PropTypes.string.isRequired,
/** handleClick will be passed the selected rows */
handleClick: PropTypes.func.isRequired,
/** classnames for button class */
className: PropTypes.string,
/** optional button variant; only relevant for the first two buttons */
variant: PropTypes.string,
/** disables button */
disabled: PropTypes.disabled,
})),
/** Actions to be performed on the table. Called with the table instance. Not displayed if rows are selected. */
tableActions: PropTypes.arrayOf(PropTypes.shape({
/** Bulk action button text */
buttonText: PropTypes.string.isRequired,
/** handleClick will be passed the selected rows */
handleClick: PropTypes.func.isRequired,
/** classnames for button class */
className: PropTypes.string,
/** optional button variant; only relevant for the first two buttons */
variant: PropTypes.string,
/** disables button */
disabled: PropTypes.disabled,
})),
bulkActions: PropTypes.oneOfType([
/** Function that will be passed selected rows as first parameter */
PropTypes.arrayOf(PropTypes.shape({
/** Bulk action button text */
buttonText: PropTypes.string.isRequired,
/** handleClick will be passed the selected rows */
handleClick: PropTypes.func.isRequired,
/** classnames for button class */
className: PropTypes.string,
/** optional button variant; only relevant for the first two buttons */
variant: PropTypes.string,
/** disables button */
disabled: PropTypes.disabled,
})),
/** Function that will be passed selected rows as first parameter */
PropTypes.func,
]),
/** Function for rendering custom components, called with the table instance */
tableActions: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.shape({
/** Bulk action button text */
buttonText: PropTypes.string.isRequired,
/** handleClick will be passed the selected rows */
handleClick: PropTypes.func.isRequired,
/** classnames for button class */
className: PropTypes.string,
/** optional button variant; only relevant for the first two buttons */
variant: PropTypes.string,
/** disables button */
disabled: PropTypes.disabled,
})),
/** Function for rendering custom components */
PropTypes.func,
]),
/** Number between one and four filters that can be shown on the top row. */
numBreakoutFilters: PropTypes.oneOf([1, 2, 3, 4]),
/** Component to be displayed when the table is empty */
Expand Down
11 changes: 11 additions & 0 deletions src/DataTable/tests/BulkActions.test.jsx
Expand Up @@ -30,6 +30,8 @@ const twoActions = [
secondAction,
];

const buttonFunction = (a) => <Button>{a.length}</Button>;

const instance = {
selectedFlatRows,
controlledTableSelections: [
Expand Down Expand Up @@ -276,4 +278,13 @@ describe('<BulkActions />', () => {
expect(icon.props().screenReaderText).toEqual(SMALL_SCREEN_DROPDOWN_BUTTON_TEXT);
});
});

describe('with function', () => {
it('passed correct number of selected rows', () => {
const wrapper = mount(<BulkActionsWrapper value={{ ...instance, bulkActions: buttonFunction }} />);
const button = wrapper.find(Button);
expect(button.length).toEqual(1);
expect(button.text()).toEqual('2');
});
});
});
11 changes: 11 additions & 0 deletions src/DataTable/tests/TableActions.test.jsx
Expand Up @@ -28,6 +28,8 @@ const twoActions = [
secondAction,
];

const buttonFunction = () => <Button>foo</Button>;

const instance = {
randomInstanceVar: 'foo',
tableActions: [
Expand Down Expand Up @@ -245,4 +247,13 @@ describe('<TableActions />', () => {
expect(icon.props().screenReaderText).toEqual(SMALL_SCREEN_DROPDOWN_BUTTON_TEXT);
});
});

describe('with function', () => {
it('passed correct number of selected rows', () => {
const wrapper = mount(<TableActionsWrapper value={{ ...instance, tableActions: buttonFunction }} />);
const button = wrapper.find(Button);
expect(button.length).toEqual(1);
expect(button.text()).toEqual('foo');
});
});
});
1 change: 1 addition & 0 deletions src/Input/index.jsx
Expand Up @@ -40,6 +40,7 @@ class Input extends React.Component {
checkHasLabel() {
if (this.inputEl.labels.length > 0) { return; }
if (this.inputEl.getAttribute('aria-label') !== null) { return; }
if (this.inputEl.getAttribute('aria-labelledby') !== null) { return; }

if (console) {
// eslint-disable-next-line no-console
Expand Down

0 comments on commit 3c6213d

Please sign in to comment.