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>
);
}
}