diff --git a/components/form/Form.jsx b/components/form/Form.jsx index 761d55f04..c118eec42 100755 --- a/components/form/Form.jsx +++ b/components/form/Form.jsx @@ -57,7 +57,8 @@ export const FormProps = { // onSubmit: React.FormEventHandler; prefixCls: PropTypes.string, hideRequiredMark: PropTypes.bool, - formRef: PropTypes.func, + autoFormCreate: PropTypes.func, + options: PropTypes.object, } export const ValidationRule = { @@ -144,23 +145,6 @@ export default { fieldDataProp: FIELD_DATA_PROP, }) }, - - // constructor (props) { - // super(props) - - // warning(!props.form, 'It is unnecessary to pass `form` to `Form` after antd@1.7.0.') - // } - - // shouldComponentUpdate(...args) { - // return PureRenderMixin.shouldComponentUpdate.apply(this, args); - // } - - // getChildContext () { - // const { layout } = this.props - // return { - // vertical: layout === 'vertical', - // } - // }, provide () { return { FormProps: this.$props, @@ -179,7 +163,7 @@ export default { render () { const { - prefixCls, hideRequiredMark, layout, onSubmit, $slots, formRef, + prefixCls, hideRequiredMark, layout, onSubmit, $slots, autoFormCreate, options = {}, } = this const formClassName = classNames(prefixCls, { @@ -188,25 +172,45 @@ export default { [`${prefixCls}-inline`]: layout === 'inline', [`${prefixCls}-hide-required-mark`]: hideRequiredMark, }) - if (formRef) { - const NewForm = createDOMForm({ + if (autoFormCreate) { + const saveFormRef = (ref) => { + this.domForm = ref + } + const DomForm = this.DomForm || createDOMForm({ fieldNameProp: 'id', + ...options, fieldMetaProp: FIELD_META_PROP, fieldDataProp: FIELD_DATA_PROP, + templateContext: this.$parent, })({ provide () { return { - NewFormProps: this.$props, + decoratorFormProps: this.$props, + } + }, + data () { + return { + children: $slots.default, + formClassName: formClassName, + submit: onSubmit, } }, mounted () { - formRef(this.form) + autoFormCreate(this.form) }, render () { - return
{$slots.default}
+ const { children, formClassName, submit } = this + return
{children}
}, }) - return + if (this.domForm) { + this.domForm.children = $slots.default + this.domForm.submit = onSubmit + this.domForm.formClassName = formClassName + } + this.DomForm = DomForm + + return saveFormRef(inst)}/> } return
{$slots.default}
diff --git a/components/form/FormItem.jsx b/components/form/FormItem.jsx index ad36b65b5..aaa5157cd 100644 --- a/components/form/FormItem.jsx +++ b/components/form/FormItem.jsx @@ -5,7 +5,7 @@ import Row from '../grid/Row' import Col, { ColProps } from '../grid/Col' import warning from '../_util/warning' import { FIELD_META_PROP, FIELD_DATA_PROP } from './constants' -import { initDefaultProps, getComponentFromProp, filterEmpty, getSlotOptions, getSlots } from '../_util/props-util' +import { initDefaultProps, getComponentFromProp, filterEmpty, getSlotOptions, getSlots, isEmptyElement } from '../_util/props-util' import getTransitionProps from '../_util/getTransitionProps' import BaseMixin from '../_util/BaseMixin' export const FormItemProps = { @@ -35,14 +35,14 @@ export default { }), inject: { FormProps: { default: {}}, - NewFormProps: { default: {}}, + decoratorFormProps: { default: {}}, }, data () { return { helpShow: false } }, mounted () { warning( - this.getControls(this.$slots.default, true).length <= 1, + this.getControls(this.slotDefault, true).length <= 1, '`Form.Item` cannot generate `validateStatus` and `help` automatically, ' + 'while there are more than one `getFieldDecorator` in it.', ) @@ -306,10 +306,10 @@ export default { ) : null }, renderChildren () { - // const { $slots, FormProps, NewFormProps, prop } = this + // const { $slots, FormProps, decoratorFormProps, prop } = this // const child = filterEmpty($slots.default || []) - // if (NewFormProps.form && prop && child.length) { - // const getFieldDecorator = NewFormProps.form.getFieldDecorator + // if (decoratorFormProps.form && prop && child.length) { + // const getFieldDecorator = decoratorFormProps.form.getFieldDecorator // const rules = FormProps.rules[prop] || [] // child[0] = getFieldDecorator(prop, { // rules, @@ -344,10 +344,10 @@ export default { }, render () { - const { $slots, NewFormProps, fieldDecoratorId, fieldDecoratorOptions = {}} = this + const { $slots, decoratorFormProps, fieldDecoratorId, fieldDecoratorOptions = {}} = this const child = filterEmpty($slots.default || []) - if (NewFormProps.form && fieldDecoratorId && child.length) { - const getFieldDecorator = NewFormProps.form.getFieldDecorator + if (decoratorFormProps.form && fieldDecoratorId && child.length) { + const getFieldDecorator = decoratorFormProps.form.getFieldDecorator child[0] = getFieldDecorator(fieldDecoratorId, fieldDecoratorOptions)(child[0]) } this.slotDefault = child diff --git a/components/form/demo/coordinated.vue b/components/form/demo/coordinated.vue index 33365f78a..359155f7c 100644 --- a/components/form/demo/coordinated.vue +++ b/components/form/demo/coordinated.vue @@ -9,10 +9,49 @@ Use `setFieldsValue` to set other control's value programmaticly. - + diff --git a/components/form/demo/dynamic-rule.vue b/components/form/demo/dynamic-rule.vue index fccac58ca..2d1ee0ff6 100644 --- a/components/form/demo/dynamic-rule.vue +++ b/components/form/demo/dynamic-rule.vue @@ -9,9 +9,39 @@ Perform different check rules according to different situations. - + diff --git a/components/form/demo/test.vue b/components/form/demo/test.vue index 20b30d0ca..24aabaea1 100644 --- a/components/form/demo/test.vue +++ b/components/form/demo/test.vue @@ -1,57 +1,73 @@ diff --git a/components/vc-form/src/createBaseForm.jsx b/components/vc-form/src/createBaseForm.jsx index 151f5c750..f128fe88c 100644 --- a/components/vc-form/src/createBaseForm.jsx +++ b/components/vc-form/src/createBaseForm.jsx @@ -35,6 +35,7 @@ function createBaseForm (option = {}, mixins = []) { fieldDataProp, formPropName = 'form', props = {}, + templateContext, } = option return function decorate (WrappedComponent) { @@ -327,7 +328,11 @@ function createBaseForm (option = {}, mixins = []) { .reduce((acc, name) => set(acc, name, this.fieldsStore.getField(name)), {}) onFieldsChange(this, changedFields, this.fieldsStore.getNestedAllFields()) } - this.$forceUpdate() + if (templateContext) { + templateContext.$forceUpdate() + } else { + this.$forceUpdate() + } this.$nextTick(() => { callback && callback() })