나는 반응 할 "새로운 것"입니다. 선택 메뉴에 항목 목록을 표시하는 사용자 지정 구성 요소를 만들려고합니다. 사용자는 메뉴에서 옵션을 선택할 수 있습니다. 완료되면 그 아래에 "추가"버튼이 있는데,이 버튼을 클릭하면 첫 번째 선택 메뉴의 나머지 선택되지 않은 옵션이 채워진 다른 선택 메뉴가 표시됩니다. 추가 옵션이 하나만 남을 때까지 추가 버튼을 클릭 할 때마다이 작업이 이상적으로 계속됩니다.동적 반응 선택 메뉴 구성 요소
초기 선택 메뉴를 표시하는 코드를 만들었지 만 어디로 가야할지 고민하고 있습니다.
import React from 'react';
import { Grid, Row, Col, Button, FormGroup, ControlLabel, FormControl } from 'react-bootstrap';
class App extends React.Component {
constructor(props) {
super(props);
this.props = props;
this.state = {
fruits: [
{ selected: false, fruit: 'Banana', value: 'banana' },
{ selected: false, fruit: 'Apple', value: 'apple' },
{ selected: false, fruit: 'Orange', value: 'orange' }
]
};
this._handleClick = this._handleClick.bind(this);
}
_handleClick(event) {
}
_handleChange(option) {
// this.setState({ value: option, selected: true });
}
render() {
return (
<div>
<Grid>
<Row>
<SelectMenu data={this.state.fruits} onChange={this._handleChange.bind(this)} />
</Row>
<Row>
<Col xs={12}>
<p>
<Button href="#" onClick={this._handleClick}>Add</Button>
</p>
</Col>
</Row>
</Grid>
</div>
);
}
}
class SelectMenu extends React.Component<any, any> {
constructor(props) {
super(props);
this.state = {
value: undefined
};
}
_handleChange(event) {
this.props.onChange(event.target.value);
}
render() {
const unselected = this.props.data.filter(fruit => fruit.selected == false);
return (
<Col sm={3}>
<FormGroup controlId="formControlsSelect">
<ControlLabel>Select</ControlLabel>
<FormControl componentClass="select" placeholder="select" onChange={this._handleChange.bind(this)} value={this.state.value}>
{this.props.data.map((option, index) => {
return <option key={index} value={option.value}>{option.location}</option>;
})};
</FormControl>
</FormGroup>
</Col>
);
}
}
export default App;
이것은 작동하지 않습니다. 나는 누군가가 이것을 창조하는 올바른 길에서 나를 시작하기를 바라고 있습니다.
"일하지 않습니까?"라고 할 때 무슨 뜻인지 자세히 설명해 주실 수 있습니까? '_handleChange'를 호출 된'onChange'에 건네지 만'setState'는'_handleChange' 함수에서 주석 처리됩니다. 우연히 jsFiddle을 만들 수 있습니까? – aug
내가 작동하지 않는다고 말하면 내가 찾고있는 방식대로 작동하지 않는다는 의미입니다. 주석 처리되지 않은 setState 메소드로도 select 요소를 렌더링하지만 _handleChange 함수를 렌더링하지 않는다면 찾고있는 기능이 도움이되지 않습니다. – addam