Redux-form:如何在向导表单上设置初始值?
Redux-form: How to set initialvalues on a wizard form?
我有一个字段 Location
,它是 yhe API 的必填字段,因此不能提交空白。所以我试图将字段的 0
设置为 initialValue
。 Location
字段位于表单的第二步,在 WizardFormSecondPage
上设置 initialValues
将从状态中删除所有输入的先前输入数据。如何为 Location
字段设置 initialValue
并在第一步中保留所有数据?
位置组件:
export class GetLocation extends Component{
constructor(){
super();
this.getMyLocation = this.getMyLocation.bind(this);
}
getMyLocation = () => {
const location = window.navigator && window.navigator.geolocation;
if (location) {
location.getCurrentPosition((position) => {
this.props.onLocationChanged(position.coords);
},
(positionError) => {
console.log(positionError.message);
this.props.onLocationChanged("0")
},{maximumAge:0, timeout: 60000})
} else {
console.log();
this.props.onLocationChanged("0")
}
};
render(){
return(
<div>
<p>Your location is </p>
<Field
name="latitude"
component="input"
className="form-control" initialValues={0.0}
/>
<Field
name="longitude"
component="input"
className="form-control"
/><br/>
<button type="button" className="btn btn-success" onClick={this.getMyLocation.bind(this)}>Get Geolocation</button>
</div>
);
}
}
WizardFormSecondPage
let WizardFormSecondPage = props => {
const { handleSubmit, previousPage} = props;
const onLocationChanged = (loc) => {
props.change('location.latitude', loc.latitude);
props.change("location.longitude", loc.longitude);
};
return (
<form onSubmit={handleSubmit} className="form-horizontal">
<div className="panel">
<div className="form-group">
<label className="control-label col-sm-2" htmlFor="address">
Location
</label>
<div className="row">
<div className="col-sm-12">
<p className="label-lead">Own Address</p>
<FormSection name="location" component={Address}>
<Address />
</FormSection>
<p className="label-lead">Location Coordinates</p>
<FormSection name="location" component={GetLocation}>
<GetLocation onLocationChanged={onLocationChanged} />
</FormSection>
</div>
</div>
</div>
</div>
<div className="clearfix">
<button type="button" className="previous pull-left btn btn-default" onClick={previousPage}>
Previous
</button>
<button type="submit" className="next pull-right btn btn-primary">
Next
</button>
</div>
</div>
</form>
);
};
export default reduxForm({
form: "wizard", // <------ same form name
destroyOnUnmount: false, // <------ preserve form data
forceUnregisterOnUnmount: true, // <------ unregister fields on unmount
validate
})(WizardFormSecondPage);
非常感谢任何帮助。
事实证明,我的做法是错误的。我可以将 initialValue
设置为整个 WizardForm
,它不会再次初始化。因此,我没有尝试设置初始化 WizardFormSecondPage
,而是不得不在 WizardForm.js
上设置值。这是我的 WizardForm.js
:
class WizardForm extends Component {
constructor(props) {
super(props);
this.nextPage = this.nextPage.bind(this);
this.previousPage = this.previousPage.bind(this);
this.state = {
page: 1,
};
}
nextPage() {
this.setState({ page: this.state.page + 1 });
}
previousPage() {
this.setState({ page: this.state.page - 1 });
}
onSubmit(values, dispatch) {
return dispatch(saveData(values));
// Call the action creator which is responsible for saving data here.
}
render() {
const { onSubmit } = this.props;
const { page } = this.state;
return (
<div>
{page === 1 && <WizardFormFirstPage onSubmit={this.nextPage} />}
{page === 2 &&
<WizardFormSecondPage
previousPage={this.previousPage}
onSubmit={this.nextPage}
/>}
{page === 3 &&
<WizardFormPreview
previousPage={this.previousPage}
onSubmit={this.onSubmit}
/>}
</div>
);
}
}
WizardForm.propTypes = {
onSubmit: PropTypes.func.isRequired,
};
// this part sets the initial values. connect if you need store.
WizardForm = reduxForm ({
form: 'wizard',
initialValues: {
location: {
latitude: "0.0",
longitude: "0.0"
}
}
})(WizardForm)
export default WizardForm;
我有一个字段 Location
,它是 yhe API 的必填字段,因此不能提交空白。所以我试图将字段的 0
设置为 initialValue
。 Location
字段位于表单的第二步,在 WizardFormSecondPage
上设置 initialValues
将从状态中删除所有输入的先前输入数据。如何为 Location
字段设置 initialValue
并在第一步中保留所有数据?
位置组件:
export class GetLocation extends Component{
constructor(){
super();
this.getMyLocation = this.getMyLocation.bind(this);
}
getMyLocation = () => {
const location = window.navigator && window.navigator.geolocation;
if (location) {
location.getCurrentPosition((position) => {
this.props.onLocationChanged(position.coords);
},
(positionError) => {
console.log(positionError.message);
this.props.onLocationChanged("0")
},{maximumAge:0, timeout: 60000})
} else {
console.log();
this.props.onLocationChanged("0")
}
};
render(){
return(
<div>
<p>Your location is </p>
<Field
name="latitude"
component="input"
className="form-control" initialValues={0.0}
/>
<Field
name="longitude"
component="input"
className="form-control"
/><br/>
<button type="button" className="btn btn-success" onClick={this.getMyLocation.bind(this)}>Get Geolocation</button>
</div>
);
}
}
WizardFormSecondPage
let WizardFormSecondPage = props => {
const { handleSubmit, previousPage} = props;
const onLocationChanged = (loc) => {
props.change('location.latitude', loc.latitude);
props.change("location.longitude", loc.longitude);
};
return (
<form onSubmit={handleSubmit} className="form-horizontal">
<div className="panel">
<div className="form-group">
<label className="control-label col-sm-2" htmlFor="address">
Location
</label>
<div className="row">
<div className="col-sm-12">
<p className="label-lead">Own Address</p>
<FormSection name="location" component={Address}>
<Address />
</FormSection>
<p className="label-lead">Location Coordinates</p>
<FormSection name="location" component={GetLocation}>
<GetLocation onLocationChanged={onLocationChanged} />
</FormSection>
</div>
</div>
</div>
</div>
<div className="clearfix">
<button type="button" className="previous pull-left btn btn-default" onClick={previousPage}>
Previous
</button>
<button type="submit" className="next pull-right btn btn-primary">
Next
</button>
</div>
</div>
</form>
);
};
export default reduxForm({
form: "wizard", // <------ same form name
destroyOnUnmount: false, // <------ preserve form data
forceUnregisterOnUnmount: true, // <------ unregister fields on unmount
validate
})(WizardFormSecondPage);
非常感谢任何帮助。
事实证明,我的做法是错误的。我可以将 initialValue
设置为整个 WizardForm
,它不会再次初始化。因此,我没有尝试设置初始化 WizardFormSecondPage
,而是不得不在 WizardForm.js
上设置值。这是我的 WizardForm.js
:
class WizardForm extends Component {
constructor(props) {
super(props);
this.nextPage = this.nextPage.bind(this);
this.previousPage = this.previousPage.bind(this);
this.state = {
page: 1,
};
}
nextPage() {
this.setState({ page: this.state.page + 1 });
}
previousPage() {
this.setState({ page: this.state.page - 1 });
}
onSubmit(values, dispatch) {
return dispatch(saveData(values));
// Call the action creator which is responsible for saving data here.
}
render() {
const { onSubmit } = this.props;
const { page } = this.state;
return (
<div>
{page === 1 && <WizardFormFirstPage onSubmit={this.nextPage} />}
{page === 2 &&
<WizardFormSecondPage
previousPage={this.previousPage}
onSubmit={this.nextPage}
/>}
{page === 3 &&
<WizardFormPreview
previousPage={this.previousPage}
onSubmit={this.onSubmit}
/>}
</div>
);
}
}
WizardForm.propTypes = {
onSubmit: PropTypes.func.isRequired,
};
// this part sets the initial values. connect if you need store.
WizardForm = reduxForm ({
form: 'wizard',
initialValues: {
location: {
latitude: "0.0",
longitude: "0.0"
}
}
})(WizardForm)
export default WizardForm;