简体   繁体   English

在 React js 中有条件地设置数据切换属性

[英]conditionally set data-toggle attributes in React js

I have a button ( type='submit' ) inside a form that has an attribute ( data-toggle='modal' ).我在具有属性( data-toggle='modal' )的表单中有一个按钮( type='submit' )。 onclick the button triggers the modal immediately. onclick 按钮立即触发模态。 But I want to validate the form first and then trigger the modal if all input field is validated.但我想先验证表单,然后在验证所有输入字段时触发模式。

so what I've tried.所以我试过了。 Below is the simplified version of the form.以下是表格的简化版本。

* *

           <form onSubmit={this.handleSubmit}>
             <button
              data-toggle={this.state.validatedForm ? "modal" : ""}
              data-target="#request_search_submit_popup_door_to_door"
              type="submit"
              className="search-button"
            >
              <span>
                <i className="icofont-search-1"></i>
              </span>
            </button>
            </form>

here I am initializing the state as below在这里,我正在初始化 state,如下所示

this.state={
    validatedForm:false
}

if all the input field is validated then only the handleSubmit method will be called.如果所有输入字段都已验证,则只会调用handleSubmit方法。 And in there I am changing the state to在那里我将 state 更改为

handleSubmit =(event)=>{
   event.preventDefault()
   this.setState({validatedForm:true})
}

but for the first submit the button doesn't trigger the modal.但是对于第一次提交,按钮不会触发模式。 I am assuming the data-toggle attribute is only checking the value of validatedForm once.Doesn't check the value of validatedForm again after the handleSubmit is called.我假设 data-toggle 属性只检查一次validatedForm的值。在调用handleSubmit 后不再检查validatedForm的值。 How can I achieve the functionality without using jquery.如何在不使用 jquery 的情况下实现该功能。 and point to be noted if I click on the button twice it works fine.并指出,如果我单击该按钮两次它可以正常工作。

render() {
  const conditionals = {};
  if (this.state.validatedForm === "modal") {
    conditionals['data-toggle'] = 'modal';
  }

  return <button data-target={"xxx"} { ...conditionals} />
}

I know it's not the cleanest solution.我知道这不是最干净的解决方案。 but I did a simple trick to get thing working for me.但我做了一个简单的技巧让事情为我工作。 In handleSubmit method just click on the button again.在handleSubmit 方法中,只需再次单击该按钮。

           <form onSubmit={this.handleSubmit}>
             <button
              id="form-submit-button"
              {...(this.state.validatedForm ?{'data-toggle': "modal"} : {})}
              data-target="#request_search_submit_popup_door_to_door"
              type="submit"
              className="search-button"
            >
              <span>
                <i className="icofont-search-1"></i>
              </span>
            </button>
           </form>


     handleSubmit = (event) => {
        event.preventDefault();
        this.setState({ validatedForm: true }, () => {
          document.getElementById("form-submit-button").click();
        });
     }

In initial render this.state.validateFrom=false .在初始渲染this.state.validateFrom=false so no data-toggle attribute is there on submit button.所以提交按钮上没有数据切换属性。 users clicks on the submit button if the form is not validated handleSubmit is not called.如果未验证表单,则用户单击提交按钮 handleSubmit 未调用。 but if the form is validated handleSubmit method gets called.但如果表单经过验证,则调用 handleSubmit 方法。 state is changed to validatedForm = true . state 更改为 validForm = true Now data-toggle attribute is there on the submit button.现在提交按钮上有 data-toggle 属性。 just clicking on the button again triggered the modal for me.只需再次单击按钮即可触发我的模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM