From 56e3b8287a333e82c5c742cc5f04d843c31fff37 Mon Sep 17 00:00:00 2001 From: Boaz Shuster Date: Tue, 18 Dec 2018 15:20:06 +0200 Subject: [PATCH] fix(DataList): Add dropdown and button actions to DataList Signed-off-by: Boaz Shuster --- .../src/components/DataList/DataList.docs.js | 5 ++ .../src/components/DataList/DataList.test.js | 34 ++++++-- .../components/DataList/DataListAction.d.ts | 3 +- .../src/components/DataList/DataListAction.js | 57 +++++++++++--- .../__snapshots__/DataList.test.js.snap | 78 +++++++++++++++++++ .../DataList/examples/ActionsDataList.js | 72 +++++++++++++++++ 6 files changed, 234 insertions(+), 15 deletions(-) create mode 100644 packages/patternfly-4/react-core/src/components/DataList/examples/ActionsDataList.js diff --git a/packages/patternfly-4/react-core/src/components/DataList/DataList.docs.js b/packages/patternfly-4/react-core/src/components/DataList/DataList.docs.js index df86525e4ae..65c54f6761a 100644 --- a/packages/patternfly-4/react-core/src/components/DataList/DataList.docs.js +++ b/packages/patternfly-4/react-core/src/components/DataList/DataList.docs.js @@ -11,6 +11,7 @@ import Simple from './examples/SimpleDataList'; import CheckboxAction from './examples/CheckboxActionDataList'; import Expandable from './examples/ExpandableDataList'; import Modifiers from './examples/ModifiersDataList'; +import Actions from './examples/ActionsDataList'; export default { title: 'DataList', @@ -33,6 +34,10 @@ export default { component: CheckboxAction, title: 'Data List Checkboxes, Actions and Additional Cells' }, + { + component: Actions, + title: 'Data List Actions: single and multiple' + }, { component: Expandable, title: 'Data List Expandable' diff --git a/packages/patternfly-4/react-core/src/components/DataList/DataList.test.js b/packages/patternfly-4/react-core/src/components/DataList/DataList.test.js index 928e0acb124..0801a620393 100644 --- a/packages/patternfly-4/react-core/src/components/DataList/DataList.test.js +++ b/packages/patternfly-4/react-core/src/components/DataList/DataList.test.js @@ -2,9 +2,11 @@ import React from 'react'; import { shallow } from 'enzyme'; import DataList from './DataList'; import DataListItem from './DataListItem'; +import DataListAction from './DataListAction'; import DataListCell from './DataListCell'; import DataListToggle from './DataListToggle'; import { Button } from '../Button'; +import { DropdownItem } from '../Dropdown'; describe('DataList', () => { test('List default', () => { @@ -78,13 +80,35 @@ describe('DataList', () => { }); test('Toggle expanded', () => { + const view = shallow(); + expect(view.find(Button).props()['aria-expanded']).toBe(true); + }); + + test('DataListAction dropdown', () => { const view = shallow( - + action-1 + , + + action-2 + + ]} /> ); - expect(view.find(Button).props()['aria-expanded']).toBe(true); + expect(view).toMatchSnapshot(); + }); + + test('DataListAction button', () => { + const view = shallow( + + + + ); + expect(view).toMatchSnapshot(); }); }); diff --git a/packages/patternfly-4/react-core/src/components/DataList/DataListAction.d.ts b/packages/patternfly-4/react-core/src/components/DataList/DataListAction.d.ts index a41e88f211b..9ac6fd597c6 100644 --- a/packages/patternfly-4/react-core/src/components/DataList/DataListAction.d.ts +++ b/packages/patternfly-4/react-core/src/components/DataList/DataListAction.d.ts @@ -1,9 +1,10 @@ -import { FunctionComponent, HTMLProps } from 'react'; +import { FunctionComponent, HTMLProps, ReactNode } from 'react'; export interface DataListActionProps extends HTMLProps { 'aria-labelledby': string; 'aria-label': string; id: string; + actions: ReactNode[]; } declare const DataListAction: FunctionComponent; diff --git a/packages/patternfly-4/react-core/src/components/DataList/DataListAction.js b/packages/patternfly-4/react-core/src/components/DataList/DataListAction.js index d3a36409c3e..a14bca29525 100644 --- a/packages/patternfly-4/react-core/src/components/DataList/DataListAction.js +++ b/packages/patternfly-4/react-core/src/components/DataList/DataListAction.js @@ -2,22 +2,61 @@ import React from 'react'; import { css } from '@patternfly/react-styles'; import PropTypes from 'prop-types'; import styles from '@patternfly/patternfly/components/DataList/data-list.css'; -import { EllipsisVIcon } from '@patternfly/react-icons'; -import { Button, ButtonVariant } from '../Button'; +import { Dropdown, DropdownPosition, KebabToggle } from '../Dropdown'; -const DataListAction = ({ className, id, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, rowid, ...props }) => ( -
- -
-); +class DataListAction extends React.Component { + constructor(props) { + super(props); + this.state = { + isOpen: false + }; + } + + onToggle = isOpen => { + this.setState({ isOpen }); + }; + + onSelect = event => { + this.setState(prevState => ({ + isOpen: !prevState.isOpen + })); + }; + + render() { + const { + children, + actions, + className, + id, + 'aria-label': ariaLabel, + 'aria-labelledby': ariaLabelledBy, + ...props + } = this.props; + + return children ? ( + children + ) : ( +
+ } + dropdownItems={actions} + /> +
+ ); + } +} DataListAction.propTypes = { /** Content rendered inside the DataList list */ children: PropTypes.node, /** Additional classes added to the DataList list */ className: PropTypes.string, + /** DataList actions to show in the dropdown */ + actions: PropTypes.arrayOf(PropTypes.node).isRequired, /** Identify the DataList toggle number */ id: PropTypes.string.isRequired, /** Adds accessible text to the DataList item */ diff --git a/packages/patternfly-4/react-core/src/components/DataList/__snapshots__/DataList.test.js.snap b/packages/patternfly-4/react-core/src/components/DataList/__snapshots__/DataList.test.js.snap index 1d667172ea6..23e998c90c0 100644 --- a/packages/patternfly-4/react-core/src/components/DataList/__snapshots__/DataList.test.js.snap +++ b/packages/patternfly-4/react-core/src/components/DataList/__snapshots__/DataList.test.js.snap @@ -31,6 +31,84 @@ exports[`DataList Cell default 1`] = ` `; +exports[`DataList DataListAction button 1`] = ` + +`; + +exports[`DataList DataListAction dropdown 1`] = ` +.pf-c-data-list__action { + display: block; + flex: 0 0 auto; + margin-left: 1.5rem; +} + +
+ + action-1 + , + + action-2 + , + ] + } + isOpen={false} + isPlain={true} + onSelect={[Function]} + position="right" + toggle={ + + } + /> +
+`; + exports[`DataList Item 1`] = ` .pf-c-data-list__item.data-list-item-custom { display: flex; diff --git a/packages/patternfly-4/react-core/src/components/DataList/examples/ActionsDataList.js b/packages/patternfly-4/react-core/src/components/DataList/examples/ActionsDataList.js new file mode 100644 index 00000000000..4d20dbe91c4 --- /dev/null +++ b/packages/patternfly-4/react-core/src/components/DataList/examples/ActionsDataList.js @@ -0,0 +1,72 @@ +import React from 'react'; +import { + Button, + Dropdown, + KebabToggle, + DropdownItem, + DataList, + DataListItem, + DataListCell, + DataListCheck, + DataListAction +} from '@patternfly/react-core'; + +class ActionsDataList extends React.Component { + state = { isOpen: false, isDeleted: false }; + render() { + return ( + + + {!this.state.isDeleted && ( + + + Single actionable Primary content + + Single actionable Secondary content + + + + + )} + + + Multi actions Primary content + + Multi actions Secondary content + Link, + + Action + , + + Disabled Link + + ]} + /> + } /> + + + + ); + } +} + +export default ActionsDataList;