简体   繁体   English

如何为我的组件使用自定义模式?

[英]How to use custom pattern for my Component?

i write this component for input ,when i use input on Change work correctly but if i use this component not work correct, error show correctly but type number in input 我写这个组件的输入,当我使用输入更改正常工作,但如果我使用此组件无法正常工作,错误显示正确但输入中的类型编号

-----Component textBox------- -----组件textBox -------

        <div className="form-group">
            <label className={"d-block"} htmlFor={this.props.id}>{this.props.label}</label>
            <input type={this.props.type}
                   name={this.props.id}
                   onClick={this.props.clicked}
                   onChange={this.props.onChange}
                   placeholder={this.props.placeholder}
                   className={"form-control"}
                   id={this.props.id}
                  />
            <span className="text-danger" aria-live="polite">{this.props.error}</span>
        </div>

-------Handle onChange------- -------处理改变-------

 handleChangezipCode=(e) =>{
        if (e.target.value.match("^[a-zA-Z ]*$") != null) {
            this.setState({
                zipCode: e.target.value,
                zipCodeError: "correct"
            })

        } else {
            this.setState({
                zipCodeError: "error"
            })
        }
    }

-----------form ------------ -----------形式------------

            <TextBox
                type="text"
                label="zipCode"
                placeholder="zipCode"
                id="zipCode"
                ref="zipCode"
                error={this.state.zipCodeError}
                value={this.state.zipCode}
                onChange={this.handleChangezipCode}
            />

Since you are not controlling the input value just validating it outside of textbox component, you are not able to control what you enter inside the input with type as text . 由于您没有控制输入值只是在文本框组件之外验证它,因此您无法控制输入内输入的内容类型为text Passing a value attribute to input which is assigned from props will work 将值属性传递给从props分配的input将起作用

   <div className="form-group">
        <label className={"d-block"} htmlFor={this.props.id}>{this.props.label}</label>
        <input type={this.props.type}
               name={this.props.id}
               onClick={this.props.clicked}
               onChange={this.props.onChange}
               placeholder={this.props.placeholder}
               className={"form-control"}
               value={this.props.value || undefined}
               id={this.props.id}
              />
        <span className="text-danger" aria-live="polite">{this.props.error}</span>
    </div>

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

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