From 4e13bee52a20eaf962285abda395c49342d26ac3 Mon Sep 17 00:00:00 2001 From: Benjy Cui Date: Fri, 29 Jan 2016 10:32:52 +0800 Subject: [PATCH] feat: make Form validation work with Modal --- components/form/FormItem.jsx | 29 +++++++++++++++------ components/form/demo/validate-basic.md | 2 +- components/form/demo/validate-customized.md | 4 +-- components/form/index.md | 1 + 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/components/form/FormItem.jsx b/components/form/FormItem.jsx index 98fc8b9205..120f62bf09 100644 --- a/components/form/FormItem.jsx +++ b/components/form/FormItem.jsx @@ -1,5 +1,6 @@ import React from 'react'; import classNames from 'classnames'; +import objectAssign from 'object-assign'; function prefixClsFn(prefixCls, ...args) { return args.map((s) => { @@ -32,10 +33,6 @@ class FormItem extends React.Component { return this.props.children.props && this.props.children.props.id; } - getMeta() { - return this.props.children.props && this.props.children.props.__meta; - } - renderHelp() { const props = this.props; const prefixCls = props.prefixCls; @@ -97,8 +94,13 @@ class FormItem extends React.Component { isRequired() { if (this.context.form) { - const meta = this.getMeta() || {}; - return (meta.validate || []).some((item) => { + const meta = this.props.fieldOption || {}; + + // Have to merge manually, for children have no `__meta` now. + const validate = (meta.validate || []); + validate.push({ rules: meta.rules }); + + return validate.filter((item) => !!item.rules).some((item) => { return item.rules.some((rule) => rule.required); }); } @@ -120,12 +122,23 @@ class FormItem extends React.Component { } renderChildren() { + const context = this.context; const props = this.props; const children = React.Children.map(props.children, (child) => { + // If    , + // React will not convert     into component. + if (!child.type) { + return child; + } + + const childProps = {}; if (typeof child.type === 'function' && !child.props.size) { - return React.cloneElement(child, { size: 'large' }); + objectAssign(childProps, { size: 'large' }); + } + if (context.form && this.getId()) { + objectAssign(childProps, context.form.getFieldProps(this.getId(), props.fieldOption)); } - return child; + return React.cloneElement(child, childProps); }); return [ this.renderLabel(), diff --git a/components/form/demo/validate-basic.md b/components/form/demo/validate-basic.md index 16dc40e05f..9e11ec12a8 100644 --- a/components/form/demo/validate-basic.md +++ b/components/form/demo/validate-basic.md @@ -174,4 +174,4 @@ class BasicDemo extends React.Component { BasicDemo = createForm()(BasicDemo); ReactDOM.render(, mountNode); -```` \ No newline at end of file +```` diff --git a/components/form/demo/validate-customized.md b/components/form/demo/validate-customized.md index 9861c861ed..11b8a22833 100644 --- a/components/form/demo/validate-customized.md +++ b/components/form/demo/validate-customized.md @@ -138,7 +138,7 @@ let Demo = React.createClass({ ] })} onContextMenu={noop} onPaste={noop} onCopy={noop} onCut={noop} - autoComplete="off"/> + autoComplete="off" id="pass" /> @@ -163,7 +163,7 @@ let Demo = React.createClass({ }], })} onContextMenu={noop} onPaste={noop} onCopy={noop} onCut={noop} - autoComplete="off" /> + autoComplete="off" id="rePass" /> diff --git a/components/form/index.md b/components/form/index.md index b66aed0e64..3bf1ecfb42 100644 --- a/components/form/index.md +++ b/components/form/index.md @@ -105,6 +105,7 @@ CustomizedForm = Form.create({})(CustomizedForm); | hasFeedback | 配合 validateStatus 属性使用,是否展示校验状态图标 | bool | | false | | prefixCls | 样式类名,默认为 ant-form,通常您不需要设置 | string | | 'ant-form' | +`Form.Item` 内的菜单控件必须设置 `id: String`,且必须唯一。 ### Input