From one array, I have it displayed as a list on one component(Box.js) and stored in a react-select in another component(Search.js). Both of them are a same level children belonging to a parent component(trial.js).
Ultimately, I want to display the object either by clicking from the list or changing/selecting from the react-select. I've lifted the event handlers to their parents and succeeded in displaying the selected object independently.
However, I can't seem to sync the onClick with the onChange. In detail, I want the click event handler to make selected list bold and change the displayed item in react-strap and vice versa. The lifting state and syncing event handler example given in the react homepage uses text input, which doesn't really help with what I am trying to do..
Parent) Trial.js:
import React, { Component } from 'react';
import { colourOptions } from './data';
import { Grid, Row } from 'react-flexbox-grid';
import Box from './box';
import Remote from './remote';
class Trial extends Component{
constructor(props) {
super(props);
this.state = {
selected:'',
}
}
getValue(e){
this.setState({
selected: e
})
}
render() {
return (
<Grid fluid className="full-height">
<Row className="full-height">
<Box
colourOptions={colourOptions}
handleChange={this.getValue.bind(this)}
/>
<Remote
colourOptions={colourOptions}
selected={this.state.selected}
handleChange={this.getValue.bind(this)}
/>
</Row>
</Grid>
)
}
}
export default Trial;
Child with list) Box.js:
import React, { Component } from 'react';
import { Col } from 'react-flexbox-grid';
class Box extends Component{
constructor(props) {
super(props);
this.state = {
}
}
clicked(e){
this.props.handleChange(e)
}
render() {
return (
<Col md={9} className="col2">
<ul>
{this.props.colourOptions.map((r,i) =>
<li key={i} onClick={()=>this.clicked(r)}> {r.label} </li>
)}
</ul>
</Col>
)
}
}
export default Box;
child with react-select) remote.js:
import React, { Component } from 'react';
import Select from 'react-select';
import { Col } from 'react-flexbox-grid';
import RenderComp from './rendercomp';
class Remote extends Component{
constructor(props) {
super(props);
this.state = {
}
}
clicked(e){
this.props.handleChange(e)
}
render() {
return (
<Col md={3} className="col1">
<Select
options={this.props.colourOptions}
onChange={this.clicked.bind(this)}
/>
<RenderComp
selected={this.props.selected}
/>
</Col>
)
}
}
export default Remote;
remote.js's child to render the selected object:
import React, { Component } from 'react';
class Remote extends Component{
constructor(props) {
super(props);
this.state = {
}
}
renderComp(selected){
return(
<ul>
<li>
{selected.label}
</li>
<li>
{selected.color}
</li>
</ul>
)
}
render() {
if(this.props.selected){
return (
<div>
{this.renderComp(this.props.selected)}
</div>
);
}else{
return(
<div></div>
)
}
}
}
export default Remote;