From bb79cb5d840ae5a5ab74e566e4d22dfc0312d0e4 Mon Sep 17 00:00:00 2001 From: Franco Correa Date: Mon, 11 Feb 2019 17:04:09 -0300 Subject: [PATCH] Fix sidebar not changing styles when selected link prop changed (#1500) * Sidebar on change props fix * Add interactive example story --- .../molecules/sidebar/sidebar-link-group.js | 20 ++++- .../molecules/sidebar/sidebar.story.js | 85 +++++++++++++++++++ 2 files changed, 102 insertions(+), 3 deletions(-) diff --git a/core/components/molecules/sidebar/sidebar-link-group.js b/core/components/molecules/sidebar/sidebar-link-group.js index 0e67e1f69..007b5f09c 100644 --- a/core/components/molecules/sidebar/sidebar-link-group.js +++ b/core/components/molecules/sidebar/sidebar-link-group.js @@ -8,15 +8,29 @@ import Automation from '../../_helpers/automation-attribute' class SidebarLinkGroup extends React.Component { constructor(props) { super(props) - const open = props.defaultOpen || false - let subItemSelected = false + this.state = { open: props.defaultOpen || false, subItemSelected: false } + this.evaluateSubItemSelection(props, { mounted: false }) + } + + evaluateSubItemSelection(props, { mounted }) { + let subItemSelected = false React.Children.forEach(props.children, child => { /* group should be open and parent be selected */ if (child && child.props && child.props.selected) subItemSelected = true }) - this.state = { open, subItemSelected } + if (this.state.subItemSelected !== subItemSelected) { + if (mounted) { + this.setState({ subItemSelected }) + } else { + this.state.subItemSelected = subItemSelected + } + } + } + + componentDidUpdate() { + this.evaluateSubItemSelection(this.props, { mounted: true }) } handleClick = () => { diff --git a/core/components/molecules/sidebar/sidebar.story.js b/core/components/molecules/sidebar/sidebar.story.js index 22e4701aa..f2315165f 100644 --- a/core/components/molecules/sidebar/sidebar.story.js +++ b/core/components/molecules/sidebar/sidebar.story.js @@ -9,6 +9,85 @@ const StyledExample = styled(Example)` height: 500px; ` +class InteractiveExample extends React.Component { + constructor(props) { + super(props) + + this.state = { selected: 'dashboard' } + this.select = this.select.bind(this) + } + + select(selected) { + this.setState({ selected }) + } + + render() { + const { selected } = this.state + return ( + + this.select('dashboard')} + selected={selected === 'dashboard'} + icon="dashboard" + label="Dashboard" + /> + this.select('clients')} + selected={selected === 'clients'} + icon="clients" + label="Applications" + /> + this.select('apis')} + selected={selected === 'apis'} + icon="apis" + label="APIs" + /> + this.select('sso')} + selected={selected === 'sso'} + icon="sso-integrations" + label="SSO Integrations" + /> + + this.select('database')} + selected={selected === 'database'} + label="Database" + /> + this.select('social')} + selected={selected === 'social'} + label="Social" + /> + this.select('enterprise')} + selected={selected === 'enterprise'} + label="Enterprise" + /> + this.select('passwordless')} + selected={selected === 'passwordless'} + label="Passwordless" + /> + + this.select('users')} + selected={selected === 'users'} + icon="users" + label="Users" + /> + this.select('rules')} + selected={selected === 'rules'} + icon="rules" + label="Rules" + /> + + ) + } +} + storiesOf('Sidebar', module).add('default', () => ( @@ -28,6 +107,12 @@ storiesOf('Sidebar', module).add('default', () => ( )) +storiesOf('Sidebar', module).add('interactive example', () => ( + + + +)) + storiesOf('Sidebar', module).add('selected item', () => (