From 0d2278385d0d3b85a987b46ffa16dcdd52e032c7 Mon Sep 17 00:00:00 2001 From: mertsincan Date: Fri, 5 Mar 2021 13:35:52 +0300 Subject: [PATCH] Fixed #1846 - Add multiple property to PanelMenu --- src/components/panelmenu/PanelMenu.js | 146 ++++++++++++-------------- 1 file changed, 70 insertions(+), 76 deletions(-) diff --git a/src/components/panelmenu/PanelMenu.js b/src/components/panelmenu/PanelMenu.js index a2a3d3b1be..b3567ba4df 100644 --- a/src/components/panelmenu/PanelMenu.js +++ b/src/components/panelmenu/PanelMenu.js @@ -8,11 +8,13 @@ import ObjectUtils from '../utils/ObjectUtils'; class PanelMenuSub extends Component { static defaultProps = { - model: null + model: null, + multiple: false }; static propTypes = { - model: PropTypes.any + model: PropTypes.any, + multiple: PropTypes.bool }; constructor(props) { @@ -40,60 +42,56 @@ class PanelMenuSub extends Component { }); } - if (this.state.activeItem && this.state.activeItem === item) { + let activeItem = this.state.activeItem; + let active = this.isItemActive(item); + + if (active) { item.expanded = false; this.setState({ - activeItem: null + activeItem: this.props.multiple ? activeItem.filter(a_item => a_item !== item) : null }); } else { - let activeItem = this.state.activeItem; - if (activeItem) { + if (!this.props.multiple && activeItem) { activeItem.expanded = false; } item.expanded = true; this.setState({ - activeItem: item + activeItem: this.props.multiple ? [...(activeItem||[]), item] : item }); } } - updateActiveItemState(item, isActive) { - let activeItem = this.state.activeItem; - if (activeItem) { - activeItem.expanded = isActive; - } - - item.expanded = !isActive; - this.setState({ - activeItem: !isActive ? item : null - }); - } - findActiveItem() { - let activeItem = null; - if (this.props.model) { - this.props.model.forEach(item => { - if (item.expanded) { - if (!activeItem) { - activeItem = item; - } - else { - item.expanded = false; + if (this.props.multiple) { + return this.props.model.filter(item => item.expanded); + } + else { + let activeItem = null; + this.props.model.forEach(item => { + if (item.expanded) { + if (!activeItem) + activeItem = item; + else + item.expanded = false; } - } - }); + }); + + return activeItem; + } } - return activeItem; + return null; + } + + isItemActive(item) { + return this.state.activeItem && (this.props.multiple ? this.state.activeItem.indexOf(item) > -1: this.state.activeItem === item); } renderSeparator(index) { - return ( -
  • - ); + return
  • ; } renderSubmenu(item, active) { @@ -104,7 +102,7 @@ class PanelMenuSub extends Component { return (
    - +
    ); @@ -114,7 +112,7 @@ class PanelMenuSub extends Component { } renderMenuitem(item, index) { - const active = this.state.activeItem === item; + const active = this.isItemActive(item); const className = classNames('p-menuitem', item.className); const linkClassName = classNames('p-menuitem-link', { 'p-disabled': item.disabled }); const iconClassName = classNames('p-menuitem-icon', item.icon); @@ -191,14 +189,16 @@ export class PanelMenu extends Component { id: null, model: null, style: null, - className: null + className: null, + multiple: false }; static propTypes = { id: PropTypes.string, model: PropTypes.array, style: PropTypes.object, - className: PropTypes.string + className: PropTypes.string, + multiple: PropTypes.bool }; constructor(props) { @@ -228,63 +228,59 @@ export class PanelMenu extends Component { }); } - if (this.state.activeItem && this.state.activeItem === item) { + let activeItem = this.state.activeItem; + let active = this.isItemActive(item); + + if (active) { item.expanded = false; this.setState({ - activeItem: null + activeItem: this.props.multiple ? activeItem.filter(a_item => a_item !== item) : null }); } else { - let activeItem = this.state.activeItem; - if (activeItem) { + if (!this.props.multiple && activeItem) { activeItem.expanded = false; } item.expanded = true; this.setState({ - activeItem: item + activeItem: this.props.multiple ? [...(activeItem||[]), item] : item }); } } - updateActiveItemState(item, isActive) { - let activeItem = this.state.activeItem; - if (activeItem) { - activeItem.expanded = isActive; - } - - item.expanded = !isActive; - this.setState({ - activeItem: !isActive ? item : null - }); - } - findActiveItem() { - let activeItem = null; - if (this.props.model) { - this.props.model.forEach(item => { - if (item.expanded) { - if (!activeItem) { - activeItem = item; - } - else { - item.expanded = false; + if (this.props.multiple) { + return this.props.model.filter(item => item.expanded); + } + else { + let activeItem = null; + this.props.model.forEach(item => { + if (item.expanded) { + if (!activeItem) + activeItem = item; + else + item.expanded = false; } - } - }); + }); + + return activeItem; + } } - return activeItem; + return null; + } + + isItemActive(item) { + return this.state.activeItem && (this.props.multiple ? this.state.activeItem.indexOf(item) > -1: this.state.activeItem === item); } renderPanelIcon(item) { const className = classNames('p-menuitem-icon', item.icon); if (item.icon) { - return ( - - ); + return } return null; @@ -294,16 +290,14 @@ export class PanelMenu extends Component { const className = classNames('p-panelmenu-icon pi', { 'pi-chevron-right': !active, ' pi-chevron-down': active }); if (item.items) { - return ( - - ); + return ; } return null; } renderPanel(item, index) { - const active = this.state.activeItem === item; + const active = this.isItemActive(item); const className = classNames('p-panelmenu-panel', item.className); const headerClassName = classNames('p-component p-panelmenu-header', { 'p-highlight': active, 'p-disabled': item.disabled }); const toggleIcon = this.renderPanelToggleIcon(item, active); @@ -314,7 +308,7 @@ export class PanelMenu extends Component { return (
    - this.onItemClick(e, item)} aria-expanded={this.state.activeItem === item} + this.onItemClick(e, item)} aria-expanded={active} id={this.id + '_header'} aria-controls={this.id + 'content'}> {toggleIcon} {itemIcon} @@ -324,7 +318,7 @@ export class PanelMenu extends Component {
    - +