Skip to content

Commit

Permalink
Add closeOnClick option to DropDownMenu component (#2418)
Browse files Browse the repository at this point in the history
* Add closeOnClick option to DropDownMenu component

* Fix lint issue - move handleDropdownToggle after componentWillReceiveProps
  • Loading branch information
spencern authored and mikemurray committed Jun 16, 2017
1 parent 5d0fc79 commit e4bade3
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
43 changes: 41 additions & 2 deletions imports/plugins/core/ui/client/components/menu/dropDownMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,55 @@ class DropDownMenu extends Component {
};
}

componentWillReceiveProps(nextProps) {
if (this.isControlled) {
this.setState({
isOpen: nextProps.isOpen
});
}
}

handleDropdownToggle = () => {
this.setState({
isOpen: !this.state.isOpen
});
}

get isOpen() {
return this.props.isOpen || this.state.isOpen;
}

get isControlled() {
return typeof this.props.isOpen === "boolean";
}

handleMenuItemChange = (event, value, menuItem) => {
this.setState({
label: menuItem.props.label || value,
isOpen: false
});

if (this.props.closeOnClick) {
this.handleOpen(false);
}

if (this.props.onChange) {
this.props.onChange(event, value);
}
}

handleOpen = (isOpen) => {
if (this.isControlled) {
if (this.props.onRequestOpen) {
this.props.onRequestOpen(isOpen);
}
} else {
this.setState({
isOpen: isOpen
});
}
}

get label() {
let label = this.state.label;
Children.forEach(this.props.children, (element) => {
Expand All @@ -54,6 +86,7 @@ class DropDownMenu extends Component {
render() {
return (
<Popover
attachment={this.props.attachment}
buttonElement={
this.props.buttonElement ||
<Button
Expand All @@ -62,9 +95,9 @@ class DropDownMenu extends Component {
label={this.label}
/>
}
isOpen={this.isOpen}
onClick={this.handleDropdownToggle}
isOpen={this.state.isOpen}
attachment={this.props.attachment}
onRequestOpen={this.handleOpen}
targetAttachment={this.props.targetAttachment}
>
<Menu
Expand All @@ -85,8 +118,14 @@ DropDownMenu.propTypes = {
buttonElement: PropTypes.node,
children: PropTypes.node,
className: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
closeOnClick: PropTypes.bool,
isEnabled: PropTypes.bool,
isOpen: PropTypes.bool,
menuStyle: PropTypes.object,
onChange: PropTypes.func,
onPublishClick: PropTypes.func,
onRequestOpen: PropTypes.func,
revisions: PropTypes.arrayOf(PropTypes.object),
targetAttachment: PropTypes.string,
translation: PropTypes.shape({
lang: PropTypes.string
Expand Down
47 changes: 46 additions & 1 deletion imports/plugins/core/ui/client/components/popover/popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ import PopoverContent from "./popoverContent";
import { Button, ButtonGroup } from "/imports/plugins/core/ui/client/components/";

class Popover extends Component {
state = {
isOpen: false
}

componentWillReceiveProps(nextProps) {
if (this.isControlled) {
this.setState({
isOpen: nextProps.isOpen
});
}
}

get isOpen() {
return this.props.isOpen || this.state.isOpen;
}

get isControlled() {
return typeof this.props.isOpen === "boolean";
}

/**
* attachment
* @description Return the attachment for the tooltip or the default
Expand All @@ -21,8 +41,32 @@ class Popover extends Component {
}
}

handleOpen = () => {
if (this.isControlled) {
if (this.props.onRequestOpen) {
this.props.onRequestOpen(true);
}
} else {
this.setState({
isOpen: true
});
}
}

handleClickOutside = () => {
if (this.isControlled) {
if (this.props.onRequestOpen) {
this.props.onRequestOpen(false);
}
} else {
this.setState({
isOpen: false
});
}
}

renderPopoverChildren() {
if (this.props.isOpen) {
if (this.isOpen) {
return (
<PopoverContent
children={this.props.children}
Expand Down Expand Up @@ -84,6 +128,7 @@ Popover.propTypes = {
isOpen: PropTypes.bool,
onClick: PropTypes.func,
onDisplayButtonClick: PropTypes.func,
onRequestOpen: PropTypes.func,
showArrow: PropTypes.bool,
showDropdownButton: PropTypes.bool,
targetAttachment: PropTypes.string,
Expand Down

0 comments on commit e4bade3

Please sign in to comment.