我们将表单字段包装到包含其验证逻辑的其自己的组件中。 例
UsernameInput.js
class UsernameInput extends Component {
render() {
return this.props.form.getFieldDecorator(this.props.id, {
rules: [...]
})(InputComponent);
}
}
我们想以这样的形式使用它们
// imports here
class CustomForm {
render() {
return (<Form>
<Form.Item>
<UsernameInput form={this.props.form} />
</Form.Item>
</Form>);
}
}
export default Form.create()(CustomForm);
这不起作用,因为FormItem.getControls不会将组件检测为控件。 只是将包装视为UsernameInput。
FormItem
来检测UsernameInput
并用作任何输入控件。 显示错误消息,反馈,帮助等。
FormItem
在内部找不到控件。
解决这个问题很困难。 您是否可以在此处发布您的建议(如何实施)?
我们通过将组件作为函数来解决
CustomForm.js
// imports here
class CustomForm {
render() {
return (<Form>
<Form.Item>
{UsernameInput({ form: this.props.form })}
</Form.Item>
</Form>);
}
}
export default Form.create()(CustomForm);
但是,如果您愿意接受想法,我认为可能的解决方案是在FormItem
定义上下文,并且Input
可以在存在时注册自己。 例
FormItem.js
class FormItem extends Component {
static childContextTypes = {
registerControl: PropTypes.func
};
getChildContext() {
return { registerControl: this.registerControl };
}
registerControl(control) {
this.controls.push(control);
}
}
Input.js
class Input extends Component {
static contextTypes = {
registerControl: PropTypes.func
};
componentDidMount() {
if (this.context.registerControl) {
this.registerControl(this);
}
}
}
我将尝试找到解决方案。
@gaastonsr您可以通过这种解决方法显示有关UsernameInput的完整代码吗?
@gaastonsr您的解决方案对我有很大帮助!
这仍然是有效的问题吗? 上下文建议对我来说听起来不错。
今天就在[email protected]。上遇到这个@gaastonsr的功能解决方法比要求所有表单组件检查上下文更优雅。
Hello @gaastonsr. We totally like your proposal/feedback, welcome to send us Pull Request for it. Please send your Pull Request to proper branch (feature branch for new feature, master for bugfix and other changes), fill the [Pull Request Template] here, provide changelog/TypeScript/documentation/test cases if needed and make sure CI passed, we will review it soon. Appreciate it advance and we are looking forword to your contribution!
你好 @gaastonsr, 我们完全同意你的提议/反馈,欢迎直接在此仓库 创建一个 Pull Request 来解决这个问题。请将 Pull Request 发到正确的分支(新特性发到 feature 分支,其他发到 master 分支),务必填写 Pull Request 内的预设模板,提供改动所需相应的 changelog、TypeScript 定义、测试用例、文档等,并确保 CI 通过,我们会尽快进行 Review,提前感谢和期待您的贡献!
请检查antd v4中的新表单组件,v3表单将不会在短期内维护。
最有用的评论
我们通过将组件作为函数来解决
CustomForm.js
但是,如果您愿意接受想法,我认为可能的解决方案是在
FormItem
定义上下文,并且Input
可以在存在时注册自己。 例FormItem.js
Input.js