0
第一次選擇時,應該過濾第二次,或通過向Api請求流行。如何填充選擇選項並篩選Redux表單中的FieldArray中的第二個選擇?
我遇到的問題是,當進入第二個字段時,狀態被共享給選項,並且當修改任何字段時,修改所有選項。
class FieldCentroCusto extends Component {
constructor(){
super()
this.state = {
itensObra: []
}
this.toggleItens = this.toggleItens.bind(this)
}
toggleItens(event) {
this.props.optionsObras.map(obra => {
if (event.target.value === obra.nome) {
return this.setState({
itensObra: obra.itens
})
}
return false
})
}
render() {
const renderItens = ({ fields, optionsObras }) => (
<div>
<Button color="primary" type="button" onClick={() => fields.push({})}>
<i className="fa fa-plus"></i> Adicionar Item de Custo
</Button>
<br /><br />
<table className="table table-bordered table-hover">
<thead className="thead-inverse">
<tr>
<th>Obra</th>
<th>Centro de Custo</th>
</tr>
</thead>
<tbody>
{fields.map((item, itemIndex) =>
<tr key={itemIndex}>
<td>
<Field name={`${item}.obra`} component={LabelAndSelect}
onChange={this.toggleItens} options={
optionsObras.map((option, optionIndex) => {
return {value: `${option.nome === undefined?'':
option.nome}`, label: `${option.nome === undefined?'':
option.nome}`}
}
)} />
</td>
<td>
<Field name={`${item}.itemObra`} component={LabelAndSelect}
options={this.state.itensObra.map((option, optionIndex) => {
return {value: `${option.descricao === undefined?'':
option.descricao}`, label: `${option.descricao === undefined?'':
option.descricao}`}
}
)} />
</td>
</tr>
)}
</tbody>
</table>
</div>
)
return (
<FieldArray name="centrosCusto"
optionsObras={this.props.optionsObras}
component={renderItens} />
)
}
}