I have two dropdowns being passed in as props to components. I can control both with seperate states but I think this can all be done with one state?
Header
import React from 'react';
import DarkLabel from './DarkLabel';
import HeaderDropdown from './HeaderDropdown';
export default class Header extends React.Component {
componentWillMount() {
this.setState({
listOpen: false
})
}
render() {
...
return (
<div className="row header">
<div className="col-xs-10">
<DarkLabel classExtra="dark-label-lg" icon="/images/system-icons/document_empty.png"
content={taskCode}/>
<DarkLabel classExtra="dark-label-2x dark-label-lg" icon="/images/system-icons/building.png"
dropdown=<HeaderDropdown data={this.props.enquiry.entity ? this.props.enquiry.entity : null}/>
content={this.props.enquiry.entity ? this.props.enquiry.entity.name : 'ERROR'}
right_icon="/images/system-icons/bullet_arrow_down.png"/>
<DarkLabel classExtra="dark-label-md" icon="/images/system-icons/ceo.png"
dropdown=<HeaderDropdown data={this.props.enquiry.contact ? this.props.enquiry.contact : null}/>
content={this.props.enquiry.contact ? this.props.enquiry.contact.firstName + ' ' + this.props.enquiry.contact.lastName : '-'}
right_icon="/images/system-icons/bullet_arrow_down.png"/>
<DarkLabel classExtra="flag"
content={'/images/flags/large/'+this.props.enquiry.entity.country.countryCode+'.png'}
right_icon="/images/system-icons/cog.png" right_icon_callback={this.handleAddressModal.bind(this)}/>
</div>
</div>
)
}
}
HeaderDropdown
import React from 'react';
export default class HeaderDropdown extends React.Component {
componentWillMount() {
}
render() {
return (
<div>
<div className="dark-label-dropdown">
Test
</div>
</div>
);
}
}
How can I make it so only one can be open at a time and close all others if a different one is clicked? Do I need to store something from 'this' when I bind a click event to HeaderDropdown?
I have missed something from the question. This needs to happen when the user clicks on right_icon in the DarkLabel.
DarkLabel
import React from 'react';
export default class DarkLabel extends React.Component {
componentWillMount() {
}
truncate(limit) {
...
}
render() {
var icon = '', content = '';
var rightIcon = '';
...
return (
<div className={'pull-left dark-label-wrapper '+this.props.classExtra}>
{icon}
<div>{content}</div>
{rightIcon}
{this.props.dropdown}
</div>
);
}
}
What you could do is add a
onClickmethod to yourHeaderDropdownand in yourHeaderhandle the state for that. The proper way would be using a store of some kind (redux) but that might be out of scope for this example.So in short I suggest you alter you Header to this:
I added a state for the current active dropdown. In your dropdown you can then access the active state with
this.props.isActive.But propably this will not already be sufficient since every click will toggle the dropdown again, also when you click the options? But it might give you a good starting point.