1 つの配列から、1 つのコンポーネント (Box.js) にリストとして表示し、別のコンポーネント (Search.js) の反応選択に格納します。どちらも親コンポーネント(trial.js)に属する同レベルの子です。
最終的に、リストからクリックするか、反応選択から変更/選択して、オブジェクトを表示したいと思います。イベント ハンドラーを親に持ち上げて、選択したオブジェクトを個別に表示することに成功しました。
ただし、onClick を onChange と同期できないようです。具体的には、クリック イベント ハンドラーで、選択したリストを太字にして、react-strap で表示されるアイテムを変更したり、その逆をしたりします。反応ホームページに示されているリフティング状態と同期イベント ハンドラーの例では、テキスト入力を使用していますが、これは私がやろうとしていることにはあまり役に立ちません..
親) 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;
リストを持つ子)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;
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 の子:
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;