gpt4 book ai didi

javascript - 带有必填字段的 React-Toolbox 对话框表单

转载 作者:行者123 更新时间:2023-11-30 21:20:37 25 4
gpt4 key购买 nike

有没有办法在 react-toolbox componenets 对话框中使用所需的输入字段?

在那里你可以定义 Action :我想将 Action 定义为提交或阻止检查表单中是否填写了 html5 必填字段的默认 Action

http://react-toolbox.com/#/components/dialog

最佳答案

这就是我在 react-toolbox 中进行验证的方式。

class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
username: '',
active: false, // for Dialog open/close
errors: {}
};
this.updateState = this.updateState.bind(this);
this.validateFormField = this.validateFormField.bind(this);
}

updateState(value, e) {
this.setState({
[e.target.name]: value
});
}

validateFormField(e) {
const propName = e.target.name;
const value = e.target.value;
const errors = { ...this.state.errors };

errors[propName] = '';
switch (propName) {
case 'username':
if (value === '') {
errors[propName] = 'Username required';
}
// add other checks for username
break;
// add more for fields
default:
}

this.setState({
errors
});
}

render() {
const actions = [
{ label: 'Cancel', onClick: this.cancelAction },
{ label: 'Submit', onClick: this.submitForm }
];
return (
<Dialog
actions={actions}
active={this.state.active}
>
<Input
type="text"
name="username"
label="Username"
onChange={this.updateState}
value={this.state.username}
onBlur={this.validateFormField}
error={this.state.errors.username}
/>
</Dialog>
);
}
}

希望这对您有所帮助。

关于javascript - 带有必填字段的 React-Toolbox 对话框表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45220162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com