【发布时间】:2016-10-02 03:53:40
【问题描述】:
当下拉值为others 时需要一个新的文本字段,但必须更改变量,因为building_type 将others 值放入新的文本字段中。在这种情况下,在哪里以及如何使两个变量相等?
import React from 'react'
import { connect } from 'react-redux'
import { reduxForm } from 'redux-form'
import { StandardForm, TextField, UsStatesSelectField, SelectField } from './../forms'
import { loadProperty, saveProperty } from './../../models/properties'
const validate = values => {
const required = value => value ? null : 'Required'
const errors = {}
errors.name = required(values.name)
errors.street_address = required(values.street_address)
errors.city = required(values.city)
errors.state = required(values.state)
errors.zip = required(values.zip)
errors.building_type = required(values.building_type)
return errors
}
const PropertyInfoForm = reduxForm({
form: 'propertyInfo',
fields: ['name', 'street_address', 'city', 'state', 'zip', 'building_type', 'building_other_type'],
validate
})(React.createClass({
render() {
return (
<StandardForm {...this.props} title="Property Info" resetButton="Reset">
<TextField {...this.props.fields.name} label="Property Name" />
<TextField {...this.props.fields.street_address} label="Property Address" />
<div className="three fields">
<TextField {...this.props.fields.city} label="City" />
<UsStatesSelectField {...this.props.fields.state} label="State" />
<TextField {...this.props.fields.zip} label="Zip Code" />
</div>
<SelectField {...this.props.fields.building_type}
label="Please select a Building Type for the junctionbox"
options={[
['office', 'Office'],
['private_home', 'Private Home'],
['residential', 'Residential'],
['retail', 'Retail'],
['restaurant', 'Restaurant'],
['school', 'School'],
['townhouse', 'Townhouse'],
['warehouse', 'Warehouse'],
['other', 'Other']
]}
/>
{((this.props.fields.building_type.value) === 'other') ? (
<TextField {...this.props.fields.building_other_type} label="Please Specify Other Building Type" />
) : ''}
</StandardForm>
)
}
}))
export default connect(
(state, ownProps) => ({
property: state.entities.property[ownProps.params.propertyId]
}),
dispatch => ({
dispatch
})
)(({ dispatch, property }) => (
<PropertyInfoForm
initialValues={property}
onSubmit={(values) => dispatch(saveProperty(property.id, values)).then(_ => dispatch(loadProperty(property.id)))}
/>
))
【问题讨论】:
-
SelectField是您的自定义组件吗?如果是这样,selectmanaged 的值在哪里? -
假设
SelectField正在使用onChange和value做它应该做的事情,你的代码看起来不错。你试过只显示{this.props.fields.building_type.value}吗? -
顺便说一句,你的问题的名字很糟糕。应该是,“如何根据 Redux Form 中的另一个输入有条件地显示一个输入?”
-
我建议创建一个单独的字段组件
SelectOtherField例如。该字段将管理值是来自选择还是文本输入。
标签: reactjs redux react-redux redux-form