我已创建的表单验证与结构这样阵营JS:
var Signin = React.createClass({
render: function() {
return (
<Form>
<Input type="text" name="email" labelName="Email" rules="isEmail" error:"Email not valid" />
<Input type="password" name="password" labelName="Password" rules="isLength:6" error:"Passowrd not valid"/>
</Form>
);
}
});
,因为,例如,“电子邮件”输入将被在应用程序的不同部分中使用可重复使用的部件,我会避免添加每次都有相同的属性(名称,类型,标签名称,规则和错误)。所以,我想创建这样的
var InputEmail = React.createClass({
render: function() {
return (
<Input type="text" name="email" labelName="Email" rules="isEmail" error="Email not valid"/>
)
}
});
var InputPassword = React.createClass({
render: function() {
return (
<Input type="password" name="password" labelName="Password" rules="isLength:6" error="Passwordnot valid"/>
)
}
});
所以签到组件时,应
var Signin = React.createClass({
render: function() {
return (
<Form>
<InputEmail />
<InputPassword />
</Form>
);
}
});
但这种方式,我得到两个错误:
我找不到了在Form中输入props.name,因为 没有在InputEmail中;
- 在输入的渲染功能
状态是空
我怎么可以创建一个reausable /继承组件?我同时使用构图模式和
我将自己完全的代码混入失败:表单
var Form = React.createClass({
getInitialState: function() {
return {
isValid : false,
isSubmitting: false
}
},
componentWillMount: function(){
this.model = {};
this.inputs = {};
this.registerInputs(this.props.children);
},
registerInputs: function(children){
React.Children.forEach(children, function (child) {
if (child.props.name) {
child.props.attachToForm = this.attachToForm;
child.props.detachFromForm = this.detachFromForm;
child.props.validate = this.validate;
}
if (child.props.children) {
this.registerInputs(child.props.children);
}
}.bind(this));
},
attachToForm: function (component) {
this.inputs[component.props.name] = component;
this.model[component.props.name] = component.state.value;
this.validate(component);
},
detachFromForm: function (component) {
delete this.inputs[component.props.name];
delete this.model[component.props.name];
},
validate: function (component) {
var isValid = true;
// validation code
component.setState({
isValid: isValid,
}, this.validateForm);
},
validateForm: function() {
var formIsValid = true;
var inputs = this.inputs;
Object.keys(inputs).forEach(function (name) {
if (!inputs[name].state.isValid) {
formIsValid = false;
}
});
this.setState({
isValid: formIsValid
});
},
updateModel: function (component) {
Object.keys(this.inputs).forEach(function (name) {
this.model[name] = this.inputs[name].state.value;
}.bind(this));
},
submit: function (event) {
event.preventDefault();
this.setState({
isSubmitting : true
});
this.updateModel();
console.log(this.model);
},
render: function() {
return (
<form className="ui form" onSubmit={this.submit}>
{this.props.children}
<button className="ui button" type="submit" disabled={this.state.isSubmitting}>Accedi</button>
</form>
);
}
});
输入
var Input = React.createClass({
getInitialState: function(){
return {
value : this.props.value || "",
isValid: true
}
},
setValue: function (event) {
this.setState({
value: event.target.value
}, function() {
this.props.validate(this);
}.bind(this));
},
componentWillMount: function() {
if (this.props.required) {
this.props.validations = this.props.validations ? this.props.validations + ',' : '';
this.props.validations += 'isLength:1';
}
// ERROR: TypeError: this.props.attachToForm is not a function
this.props.attachToForm(this);
},
componentWillUnmount: function() {
this.props.detachFromForm(this);
},
render: function() {
var className = "field";
if(this.props.className){
className += " " + this.props.className;
}
if(this.props.required){
className += " required";
}
var Label;
if(this.props.labelName){
Label = (<label htmlFor={this.props.name}>{this.props.labelName}</label>);
}
var Error;
if(!this.state.isValid){
Error = (<div className="ui">{this.props.error || this.props.name + " not valid"}</div>);
};
return (
<div className={className}>
{Label}
<input type={this.props.type || "text"} id={this.props.name} name={this.props.name} onChange={this.setValue} value={this.state.value} />
{Error}
</div>
);
}
});
有了这个工作
ReactDOM.render(
<Form>
<Input type="text" name="email" labelName="Email" rules="isEmail" error:"Email not valid"/>
</Form>,
document.getElementById('app')
);
这样我获得: “TypeError:this.props.attachToForm不是函数 this.props.attachToForm(this);“
ReactDOM.render(
<Form>
<InputEmail/>
</Form>,
document.getElementById('app')
);
PS:我尝试对的jsfiddle添加此代码,但我得到 “类型错误:无法界定财产 ”attachToForm“:对象是不可扩展的”
这里的一个建议是不使用单独的组件来封装可重用的行为。您可以改为创建一个代表您想要重复使用的道具的对象,然后将它们传播到需要它们的输入中。var inputEmailProps = {},' – azium
对于试图帮助你解决你使用的其他库问题;看起来你正在使用React-bootstrap,对吧?请务必注意,由于这些API是 – markthethomas
@markthethomas问题的一部分,我正在使用语义UI和验证器的模块进行表单验证 – Webman