Allows AddDropDownButton components to accept array of dropdownItems

This commit is contained in:
Alex Corey
2019-11-08 15:47:19 -05:00
parent 052f101a70
commit d91aa8c6cf
5 changed files with 66 additions and 78 deletions

View File

@@ -1,10 +1,9 @@
import React, { useState, useRef, useEffect } from 'react'; import React, { useState, useRef, useEffect } from 'react';
import { Link, withRouter } from 'react-router-dom'; import { Link } from 'react-router-dom';
import { withI18n } from '@lingui/react';
import { Dropdown, DropdownPosition } from '@patternfly/react-core'; import { Dropdown, DropdownPosition } from '@patternfly/react-core';
import { ToolbarAddButton } from '@components/PaginatedDataList'; import { ToolbarAddButton } from '@components/PaginatedDataList';
function AddDropDownButton({ topUrl, bottomUrl, topLabel, bottomLabel }) { function AddDropDownButton({ dropdownItems }) {
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
const element = useRef(null); const element = useRef(null);
@@ -28,22 +27,19 @@ function AddDropDownButton({ topUrl, bottomUrl, topLabel, bottomLabel }) {
isOpen={isOpen} isOpen={isOpen}
position={DropdownPosition.right} position={DropdownPosition.right}
toggle={<ToolbarAddButton onClick={() => setIsOpen(!isOpen)} />} toggle={<ToolbarAddButton onClick={() => setIsOpen(!isOpen)} />}
dropdownItems={[ dropdownItems={dropdownItems.map(item => (
<Link key="top" className="pf-c-dropdown__menu-item" to={`${topUrl}`}>
{`${topLabel}`}
</Link>,
<Link <Link
key="bottom"
className="pf-c-dropdown__menu-item" className="pf-c-dropdown__menu-item"
to={`${bottomUrl}`} key={item.key}
to={item.url}
> >
{`${bottomLabel}`} {item.label}
</Link>, </Link>
]} ))}
/> />
</div> </div>
); );
} }
export { AddDropDownButton as _AddDropDownButton }; export { AddDropDownButton as _AddDropDownButton };
export default withI18n()(withRouter(AddDropDownButton)); export default AddDropDownButton;

View File

@@ -3,20 +3,33 @@ import { mountWithContexts } from '@testUtils/enzymeHelpers';
import AddDropDownButton from './AddDropDownButton'; import AddDropDownButton from './AddDropDownButton';
describe('<AddDropDownButton />', () => { describe('<AddDropDownButton />', () => {
const dropdownItems = [
{
key: 'inventory',
label: 'Inventory',
url: `inventory/inventory/add/`,
},
];
test('should be closed initially', () => { test('should be closed initially', () => {
const wrapper = mountWithContexts(<AddDropDownButton />); const wrapper = mountWithContexts(
<AddDropDownButton dropdownItems={dropdownItems} />
);
expect(wrapper.find('Dropdown').prop('isOpen')).toEqual(false); expect(wrapper.find('Dropdown').prop('isOpen')).toEqual(false);
}); });
test('should render two links', () => { test('should render two links', () => {
const wrapper = mountWithContexts(<AddDropDownButton />); const wrapper = mountWithContexts(
<AddDropDownButton dropdownItems={dropdownItems} />
);
wrapper.find('button').simulate('click'); wrapper.find('button').simulate('click');
expect(wrapper.find('Dropdown').prop('isOpen')).toEqual(true); expect(wrapper.find('Dropdown').prop('isOpen')).toEqual(true);
expect(wrapper.find('Link')).toHaveLength(2); expect(wrapper.find('Link')).toHaveLength(dropdownItems.length);
}); });
test('should close when button re-clicked', () => { test('should close when button re-clicked', () => {
const wrapper = mountWithContexts(<AddDropDownButton />); const wrapper = mountWithContexts(
<AddDropDownButton dropdownItems={dropdownItems} />
);
wrapper.find('button').simulate('click'); wrapper.find('button').simulate('click');
expect(wrapper.find('Dropdown').prop('isOpen')).toEqual(true); expect(wrapper.find('Dropdown').prop('isOpen')).toEqual(true);
wrapper.find('button').simulate('click'); wrapper.find('button').simulate('click');

View File

@@ -1,4 +1 @@
export { export { default } from './AddDropDownButton';
default
}
from './AddDropDownButton'

View File

@@ -36,7 +36,6 @@ class InventoriesList extends Component {
selected: [], selected: [],
inventories: [], inventories: [],
itemCount: 0, itemCount: 0,
isAddOpen: false,
}; };
this.loadInventories = this.loadInventories.bind(this); this.loadInventories = this.loadInventories.bind(this);
@@ -44,7 +43,6 @@ class InventoriesList extends Component {
this.handleSelect = this.handleSelect.bind(this); this.handleSelect = this.handleSelect.bind(this);
this.handleInventoryDelete = this.handleInventoryDelete.bind(this); this.handleInventoryDelete = this.handleInventoryDelete.bind(this);
this.handleDeleteErrorClose = this.handleDeleteErrorClose.bind(this); this.handleDeleteErrorClose = this.handleDeleteErrorClose.bind(this);
this.handleAddToggle = this.handleAddToggle.bind(this);
} }
componentDidMount() { componentDidMount() {
@@ -78,18 +76,6 @@ class InventoriesList extends Component {
} }
} }
handleAddToggle(e) {
const { isAddOpen } = this.state;
if (this.node && this.node.contains(e.target) && isAddOpen) {
this.setState({ isAddOpen: false });
} else if (this.node && this.node.contains(e.target) && !isAddOpen) {
this.setState({ isAddOpen: true });
} else {
this.setState({ isAddOpen: false });
}
}
async handleInventoryDelete() { async handleInventoryDelete() {
const { selected, itemCount } = this.state; const { selected, itemCount } = this.state;
@@ -155,14 +141,29 @@ class InventoriesList extends Component {
inventories, inventories,
itemCount, itemCount,
selected, selected,
isAddOpen,
actions, actions,
} = this.state; } = this.state;
const { match, i18n } = this.props; const { match, i18n } = this.props;
const canAdd = const canAdd =
actions && Object.prototype.hasOwnProperty.call(actions, 'POST'); actions && Object.prototype.hasOwnProperty.call(actions, 'POST');
const isAllSelected = const isAllSelected = selected.length === inventories.length;
selected.length > 0 && selected.length === inventories.length; const addButton = (
<AddDropDownButton
key="add"
dropdownItems={[
{
key: 'inventory',
label: i18n._(t`Inventory`),
url: `${match.url}/inventory/add/`,
},
{
key: 'smart-inventory',
label: i18n._(t`Smart Inventory`),
url: `${match.url}/smart_inventory/add/`,
},
]}
/>
);
return ( return (
<PageSection> <PageSection>
<Card> <Card>
@@ -208,15 +209,7 @@ class InventoriesList extends Component {
itemsToDelete={selected} itemsToDelete={selected}
pluralizedItemName="Inventories" pluralizedItemName="Inventories"
/>, />,
canAdd && ( canAdd && addButton,
<AddDropDownButton
key="add"
topUrl={`${match.url}/inventory/add/`}
bottomdUrl={`${match.url}/smart_inventory/add/`}
topLabel={i18n._(t`Inventory`)}
bottomLabel={i18n._(t`Smart Inventory`)}
/>
),
]} ]}
/> />
)} )}
@@ -234,17 +227,7 @@ class InventoriesList extends Component {
isSelected={selected.some(row => row.id === inventory.id)} isSelected={selected.some(row => row.id === inventory.id)}
/> />
)} )}
emptyStateControls={ emptyStateControls={canAdd && addButton}
canAdd && (
<AddDropDownButton
key="add"
topUrl={`${match.url}/inventory/add/`}
bottomUrl={`${match.url}/smart_inventory/add/`}
topLabel={i18n._(t`Inventory`)}
bottomLabel={i18n._(t`Smart Inventory`)}
/>
)
}
/> />
</Card> </Card>
<AlertModal <AlertModal

View File

@@ -161,6 +161,23 @@ class TemplatesList extends Component {
actions && Object.prototype.hasOwnProperty.call(actions, 'POST'); actions && Object.prototype.hasOwnProperty.call(actions, 'POST');
const isAllSelected = const isAllSelected =
selected.length === templates.length && selected.length > 0; selected.length === templates.length && selected.length > 0;
const addButton = (
<AddDropDownButton
key="add"
dropdownItems={[
{
key: 'template',
label: i18n._(t`Template`),
url: `${match.url}/template/add/`,
},
{
key: 'workflow_template',
label: i18n._(t`Workflow Template`),
url: `${match.url}/_workflow/add/`,
},
]}
/>
);
return ( return (
<PageSection> <PageSection>
<Card> <Card>
@@ -206,15 +223,7 @@ class TemplatesList extends Component {
itemsToDelete={selected} itemsToDelete={selected}
pluralizedItemName="Templates" pluralizedItemName="Templates"
/>, />,
canAdd && ( canAdd && addButton,
<AddDropDownButton
key="add"
topUrl={`${match.url}/job_template/add/`}
bottomUrl={`${match.url}_workflow/add/`}
topLabel={i18n._(t`Job Template`)}
bottomLabel={i18n._(t`Workflow Template`)}
/>
),
]} ]}
/> />
)} )}
@@ -228,17 +237,7 @@ class TemplatesList extends Component {
isSelected={selected.some(row => row.id === template.id)} isSelected={selected.some(row => row.id === template.id)}
/> />
)} )}
emptyStateControls={ emptyStateControls={canAdd && addButton}
canAdd && (
<AddDropDownButton
key="add"
topUrl={`${match.url}/job_template/add/`}
bottomUrl={`${match.url}_workflow/add/`}
topLabel={i18n._(t`Job Template`)}
bottomLabel={i18n._(t`Workflow Template`)}
/>
)
}
/> />
</Card> </Card>
<AlertModal <AlertModal