React中的单选按钮验证,用于动态数量的单选按钮

时间:2018-03-28 22:39:59

标签: javascript reactjs validation

我有以下React组件:

import React from 'react';
import classes from './surveytwo.css';
import { Link } from 'react-router-dom';


const surveyTwo = (props) => {
    return (
        <div className={ classes.screen2 } >

            <table className={ classes.initial__survey__details__table }>
                <thead>
                    <tr>
                        <td>
                            Gender    
                        </td>
                        <td>
                             Age    
                        </td>     
                    </tr>     
                </thead>
                <tbody>
                    <tr>
                        <td>
                            <input type="radio" name="customer_gender" />                                     
                            <label>Male</label>
                        </td>
                        <td>
                            <input type="radio" name="customer_name" />                                   
                            <label>Less than 35</label>
                        </td>     
                    </tr>
                    <tr>
                        <td>
                            <input type="radio" name="customer_gender" />                                    
                            <label>Female</label>
                        </td>
                        <td>
                            <input type="radio" name="customer_name" />                                    
                            <label>More than 35</label>
                        </td>     
                    </tr> 
                    <tr>
                        <td colSpan="2">
                            {/* <button className={ [classes.btn , classes["btn--fullwidth"] , classes.btn__next  ].join(' ') }>NEXT</button> */}
                            <Link to="/" className={ [classes.btn , classes["btn--fullwidth"] , classes.btn__next  ].join(' ') }>
                                Survey 1
                            </Link>
                        </td>   
                    </tr>     
                </tbody>   
            </table>

        </div>
    );
}

export default surveyTwo;

正如您所看到的,有两组单选按钮和一条Link路由可以返回主页。我希望在启用Link之前检查这两个单选按钮组,并且用户可以单击它,截至目前,用户可以在不检查单选按钮的情况下离开此页面。如何添加此验证?

此外,单选按钮可以在未来从2组增加到更多,所以如何在检查所有单选按钮之前添加此验证检查并禁用Link到主页?< / p>

1 个答案:

答案 0 :(得分:1)

通过快速草案,也许不是最好的解决方案,但希望它有助于背后的逻辑。

您可以将单选按钮的状态存储在组件的状态中:

state = {
   genderRadioClicked: false,
   ageRadioClicked: false
   // Add any extra group you create later
}

使用 setState 输入中使用onChange事件将其更新为true。

验证方法如下所示:

_handleRadioValidation = (e) => {

   // Get the state of all the radio buttons
   const radioStates = this.state

   // Iterate through the radio state object and return the value for 
   // every key and assign it to an array
   const checkedStatus = Object.keys(radioStates).map((key) => {
      return radioStates[key];
   });

   // Once you got the array with the value of the group radios (if 
   // the group is checked or not) filter the array to return only if the 
   // value is equal to false (which means that it's not checked)
   const filteredArray = checkedStatus.filter((value) => value === false)

   // If the length of the resulting array from the filtering is bigger
   // than 0 it means one of the group is false (unchecked)
   // Then you just prevent the default behaviour of the event, in this
   // case that is a link, it will disable the link.
   if (filteredArray.length > 0) {
      e.preventDefault();
      return;
   }

   // If all of them are checked it will execute like normal

}

最后将此方法附加到这样的组件:

<Link to="/" onClick={(e) => this._handleRadioValidation(e)}>

希望它有所帮助!