使用 setFieldValue 根据按钮单击发送不同的值
当用户单击位于的 Save andContinue
按钮时,我尝试在 handleSubmit
回调中发送不同的 requestStatuses
值(即 10) formik 表单如下所示:
<div className="form-field">
<Button size="large"
variant="contained"
color="primary"
style={{marginLeft: '5px'}}
type="submit">
Save & Continue
</Button>
</div>
仅供参考:当用户点击 Submit
按钮时,我想将 requestStatuses
的值保留为 6
,其中这行代码props.SimpleRequest && props.SimpleRequest.statusId || '6',
.:
我尝试过的事情:
场景 1:
我计划使用 formik 的 setFieldValue
来更改像这样将值设置为10
setFieldValue ('requestStatuses',10,false);
,为此我修改了保存并继续
按钮,如下所示:
<Button size="large"
variant="contained"
color="primary"
style={{marginLeft: '5px'}}
type="button"
onClick={handleSaveAndContinue}>
Save & Continue
</Button>
将按钮类型更改为
按钮
来自提交
定义了
onClick
处理程序并调用了单独的函数handleSaveAndContinue
定义了
handleSaveAndContinue
如下,并尝试调用 SubmitForm prop 但没有任何反应:const handleSaveAndContinue = () =>; { setFieldValue('requestStatuses',10,false); props.submitForm; }
在这种情况下,我确实尝试将setFieldValue
作为第二个参数包含在handleSubmit
中,例如以下:
handleSubmit(values,{setFieldValue} {props, resetForm, setErrors, setSubmitting}) {
props.handleSubmit(values)
setSubmitting(false)
},
场景 2:
<Button size="large"
variant="contained"
color="primary"
style={{marginLeft: '5px'}}
type="button"
onClick={props.submitForm}>
Save & Continue
</Button>
- 将按钮类型从
submit
更改为button
- 定义 onClick 处理程序并调用
submitForm
属性单击。
这确实会触发 handleSubmit
回调,但我不明白在这种情况下在哪里定义 setFieldValue ('requestStatuses',10,false);
。
我是否正确使用 setFieldValue
来实现我的目标?
带有相关Formik表单信息的子组件的最小代码:
const SimpleRequestForm = (props) => {
const {values, setFieldValue, touched, errors, isSubmitting, handleReset, handleChange} =
props;
const growl = React.createRef()
return (
<div>
<div id="formDiv">
<Growl ref={growl}/>
<Form className="form-column-3">
<div className="SimpleRequest-form">
<div className="form-field">
<CustomSelectField name="requestStatuses" disabled type="select" placeholder="Status"/>
</div>
<div className="form-field">
<CustomSelectField name="requestPrioritieses" type="select" value='Normal' placeholder="Priority"/>
</div>
<div className="form-field">
<CustomSelectField name="assignees" type="select" placeholder="Assignee"/>
</div>
<div className="form-field">
<Button size="large" variant="contained" color="primary" style={{marginLeft: '5px'}} type="submit">Save & Continue</Button>
</div>
</div>
<div className="btn-group-right">
<Button size="large" variant="contained" color="primary"
type="submit">Submit</Button>
<Button size="large" variant="contained" color="primary" onClick={handleReset}
style={{marginLeft: '5px'}} type="button">Reset</Button>
<Button size="large" variant="contained" color="primary" onClick={props.onCancel}
style={{marginLeft: '5px'}} type="button">Cancel</Button>
</div>
</Form>
</div>
</div>
)
};
export const SimpleRequestEnhancedForm = withFormik(
{
mapPropsToValues: props => {
return {
requestId: props.SimpleRequest && props.SimpleRequest.requestId || '',
requestStatuses: props.SimpleRequest && props.SimpleRequest.statusId || '6',
requestPrioritieses: props.SimpleRequest && props.SimpleRequest.priorityId || '3',
assignees: props.SimpleRequest && props.SimpleRequest.assigneeId || '',
}
},
validationSchema:validationSchema,
handleSubmit(values, {props, resetForm, setErrors, setSubmitting}) {
props.handleSubmit(values)
setSubmitting(false)
},
setFieldValue(field, value, shouldVal) {
console.log('In setFieldValue')
},
displayName: 'Simple Request Form',
})(SimpleRequestForm)
基于评论的测试结果:
我的功能:
const handleSaveAndContinue = () => {
setFieldValue ('requestStatuses',10,false);
props.submitForm();
}
我的按钮:
<Button size="large"
variant="contained"
color="primary"
style={{marginLeft: '5px'}}
type="button"
onClick={handleSaveAndContinue}>
Save & Continue
</Button>
我的handleSubmit< /code>:
handleSubmit(values,{setFieldValue}, {props, resetForm, setErrors, setSubmitting}) {
props.handleSubmit(values)
props.setFieldValue()
setSubmitting(false)
},
收到错误:未捕获(承诺中)TypeError:_ref6 未定义
。调试器显示以下内容:
handleSubmit: function handleSubmit(values, _ref5, _ref6) {
var setFieldValue = _ref5.setFieldValue;
var props = _ref6.props,
resetForm = _ref6.resetForm,
setErrors = _ref6.setErrors,
setSubmitting = _ref6.setSubmitting;
props.handleSubmit(values);
props.setFieldValue();
setSubmitting(false);
},
I am trying to send a different value of requestStatuses
(i.e. 10) in the handleSubmit
callback when a user clicks Save and Continue
button which is in the formik form as shown below:
<div className="form-field">
<Button size="large"
variant="contained"
color="primary"
style={{marginLeft: '5px'}}
type="submit">
Save & Continue
</Button>
</div>
FYI: When a user hits Submit
button, I want to keep the value of requestStatuses
as 6
which is in this line of the code props.SimpleRequest && props.SimpleRequest.statusId || '6',
.:
Things that I have tried:
Scenario 1:
I was planning of using setFieldValue
of formik to change the value to 10
like this
setFieldValue ('requestStatuses',10,false);
and for this I modified the Save and Continue
button as follows:
<Button size="large"
variant="contained"
color="primary"
style={{marginLeft: '5px'}}
type="button"
onClick={handleSaveAndContinue}>
Save & Continue
</Button>
Changed the type of button to
button
fromsubmit
Defined an
onClick
handler and called a separate functionhandleSaveAndContinue
Defined
handleSaveAndContinue
like following and tried to call submitForm prop but nothing happens:const handleSaveAndContinue = () => { setFieldValue ('requestStatuses',10,false); props.submitForm; }
In this case I did try to include setFieldValue
as second parameter in handleSubmit
like following:
handleSubmit(values,{setFieldValue} {props, resetForm, setErrors, setSubmitting}) {
props.handleSubmit(values)
setSubmitting(false)
},
Scenario 2:
<Button size="large"
variant="contained"
color="primary"
style={{marginLeft: '5px'}}
type="button"
onClick={props.submitForm}>
Save & Continue
</Button>
- Changed the type of button to
button
fromsubmit
- Defined an onClick handler and called
submitForm
props onclick.
This does trigger the handleSubmit
callback but I don't understand where to define setFieldValue ('requestStatuses',10,false);
in this case.
Am I proceeding with the usage of setFieldValue
correctly to achieve my goal?
Minimal Code of Child component with relavent Formik form information:
const SimpleRequestForm = (props) => {
const {values, setFieldValue, touched, errors, isSubmitting, handleReset, handleChange} =
props;
const growl = React.createRef()
return (
<div>
<div id="formDiv">
<Growl ref={growl}/>
<Form className="form-column-3">
<div className="SimpleRequest-form">
<div className="form-field">
<CustomSelectField name="requestStatuses" disabled type="select" placeholder="Status"/>
</div>
<div className="form-field">
<CustomSelectField name="requestPrioritieses" type="select" value='Normal' placeholder="Priority"/>
</div>
<div className="form-field">
<CustomSelectField name="assignees" type="select" placeholder="Assignee"/>
</div>
<div className="form-field">
<Button size="large" variant="contained" color="primary" style={{marginLeft: '5px'}} type="submit">Save & Continue</Button>
</div>
</div>
<div className="btn-group-right">
<Button size="large" variant="contained" color="primary"
type="submit">Submit</Button>
<Button size="large" variant="contained" color="primary" onClick={handleReset}
style={{marginLeft: '5px'}} type="button">Reset</Button>
<Button size="large" variant="contained" color="primary" onClick={props.onCancel}
style={{marginLeft: '5px'}} type="button">Cancel</Button>
</div>
</Form>
</div>
</div>
)
};
export const SimpleRequestEnhancedForm = withFormik(
{
mapPropsToValues: props => {
return {
requestId: props.SimpleRequest && props.SimpleRequest.requestId || '',
requestStatuses: props.SimpleRequest && props.SimpleRequest.statusId || '6',
requestPrioritieses: props.SimpleRequest && props.SimpleRequest.priorityId || '3',
assignees: props.SimpleRequest && props.SimpleRequest.assigneeId || '',
}
},
validationSchema:validationSchema,
handleSubmit(values, {props, resetForm, setErrors, setSubmitting}) {
props.handleSubmit(values)
setSubmitting(false)
},
setFieldValue(field, value, shouldVal) {
console.log('In setFieldValue')
},
displayName: 'Simple Request Form',
})(SimpleRequestForm)
Testing Results based on comment:
My function:
const handleSaveAndContinue = () => {
setFieldValue ('requestStatuses',10,false);
props.submitForm();
}
My Button:
<Button size="large"
variant="contained"
color="primary"
style={{marginLeft: '5px'}}
type="button"
onClick={handleSaveAndContinue}>
Save & Continue
</Button>
My handleSubmit
:
handleSubmit(values,{setFieldValue}, {props, resetForm, setErrors, setSubmitting}) {
props.handleSubmit(values)
props.setFieldValue()
setSubmitting(false)
},
Error Received : Uncaught (in promise) TypeError: _ref6 is undefined
. Debugger shows the following:
handleSubmit: function handleSubmit(values, _ref5, _ref6) {
var setFieldValue = _ref5.setFieldValue;
var props = _ref6.props,
resetForm = _ref6.resetForm,
setErrors = _ref6.setErrors,
setSubmitting = _ref6.setSubmitting;
props.handleSubmit(values);
props.setFieldValue();
setSubmitting(false);
},
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,您可以做的是将两个按钮(“保存并继续”和“提交”)类型保留为提交,将 onClick 处理程序附加到 setFieldValue,根据单击的按钮修改值!
和
so what you can do is to keep both of button ('Save and Continue' and 'Submit') types as submit, attach an onClick handler to setFieldValue modifying the values based off of the button that was clicked!
and