使用 setFieldValue 根据按钮单击发送不同的值

发布于 2025-01-17 02:52:42 字数 7182 浏览 1 评论 0原文

当用户单击位于的 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>
  1. 将按钮类型更改为按钮来自提交

  2. 定义了 onClick 处理程序并调用了单独的函数 handleSaveAndContinue

  3. 定义了 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>
  1. 将按钮类型从 submit 更改为 button
  2. 定义 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>
  1. Changed the type of button to button from submit

  2. Defined an onClick handler and called a separate function handleSaveAndContinue

  3. 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>
  1. Changed the type of button to button from submit
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疾风者 2025-01-24 02:52:42

因此,您可以做的是将两个按钮(“保存并继续”和“提交”)类型保留为提交,将 onClick 处理程序附加到 setFieldValue,根据单击的按钮修改值!

<button
    size="large"
    variant="contained"
    color="primary"
    type="submit"
    onClick={() => {
        props.setFieldValue("requestStatuses", 6, false);
    }}
>
Submit
</button>

<button
    size="large"
    variant="contained"
    color="primary"
    style={{ marginLeft: "5px" }}
    type="submit"
    onClick={() => {
        props.setFieldValue("requestStatuses", 10, false);
    }}
>
Save & Continue
</button>

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!

<button
    size="large"
    variant="contained"
    color="primary"
    type="submit"
    onClick={() => {
        props.setFieldValue("requestStatuses", 6, false);
    }}
>
Submit
</button>

and

<button
    size="large"
    variant="contained"
    color="primary"
    style={{ marginLeft: "5px" }}
    type="submit"
    onClick={() => {
        props.setFieldValue("requestStatuses", 10, false);
    }}
>
Save & Continue
</button>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文