From b7789c9907d82f4956eccbf37e0f0c0ccd67cbbd Mon Sep 17 00:00:00 2001 From: tjz <415800467@qq.com> Date: Fri, 22 Jun 2018 21:35:14 +0800 Subject: [PATCH] feat: add form support template --- components/form/Form.jsx | 23 +++++++++++- components/form/FormItem.jsx | 24 ++++++++++-- components/form/demo/test.vue | 70 +++++++++++++++++++++++++++++++++++ site/dev.js | 2 +- 4 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 components/form/demo/test.vue diff --git a/components/form/Form.jsx b/components/form/Form.jsx index 63de4a8e3..761d55f04 100755 --- a/components/form/Form.jsx +++ b/components/form/Form.jsx @@ -57,6 +57,7 @@ export const FormProps = { // onSubmit: React.FormEventHandler; prefixCls: PropTypes.string, hideRequiredMark: PropTypes.bool, + formRef: PropTypes.func, } export const ValidationRule = { @@ -178,7 +179,7 @@ export default { render () { const { - prefixCls, hideRequiredMark, layout, onSubmit, $slots, + prefixCls, hideRequiredMark, layout, onSubmit, $slots, formRef, } = this const formClassName = classNames(prefixCls, { @@ -187,6 +188,26 @@ export default { [`${prefixCls}-inline`]: layout === 'inline', [`${prefixCls}-hide-required-mark`]: hideRequiredMark, }) + if (formRef) { + const NewForm = createDOMForm({ + fieldNameProp: 'id', + fieldMetaProp: FIELD_META_PROP, + fieldDataProp: FIELD_DATA_PROP, + })({ + provide () { + return { + NewFormProps: this.$props, + } + }, + mounted () { + formRef(this.form) + }, + render () { + return
{$slots.default}
+ }, + }) + return + } return
{$slots.default}
}, diff --git a/components/form/FormItem.jsx b/components/form/FormItem.jsx index cff6fa92f..ad36b65b5 100644 --- a/components/form/FormItem.jsx +++ b/components/form/FormItem.jsx @@ -20,6 +20,8 @@ export const FormItemProps = { hasFeedback: PropTypes.bool, required: PropTypes.bool, colon: PropTypes.bool, + fieldDecoratorId: PropTypes.string, + fieldDecoratorOptions: PropTypes.object, } export default { @@ -33,6 +35,7 @@ export default { }), inject: { FormProps: { default: {}}, + NewFormProps: { default: {}}, }, data () { return { helpShow: false } @@ -90,7 +93,7 @@ export default { }, getOnlyControl () { - const child = this.getControls(this.$slots.default, false)[0] + const child = this.getControls(this.slotDefault, false)[0] return child !== undefined ? child : null }, @@ -303,12 +306,20 @@ export default { ) : null }, renderChildren () { - const { $slots } = this + // const { $slots, FormProps, NewFormProps, prop } = this + // const child = filterEmpty($slots.default || []) + // if (NewFormProps.form && prop && child.length) { + // const getFieldDecorator = NewFormProps.form.getFieldDecorator + // const rules = FormProps.rules[prop] || [] + // child[0] = getFieldDecorator(prop, { + // rules, + // })(child[0]) + // } return [ this.renderLabel(), this.renderWrapper( this.renderValidateWrapper( - filterEmpty($slots.default || []), + this.slotDefault, this.renderHelp(), this.renderExtra(), ), @@ -333,6 +344,13 @@ export default { }, render () { + const { $slots, NewFormProps, fieldDecoratorId, fieldDecoratorOptions = {}} = this + const child = filterEmpty($slots.default || []) + if (NewFormProps.form && fieldDecoratorId && child.length) { + const getFieldDecorator = NewFormProps.form.getFieldDecorator + child[0] = getFieldDecorator(fieldDecoratorId, fieldDecoratorOptions)(child[0]) + } + this.slotDefault = child const children = this.renderChildren() return this.renderFormItem(children) }, diff --git a/components/form/demo/test.vue b/components/form/demo/test.vue new file mode 100644 index 000000000..20b30d0ca --- /dev/null +++ b/components/form/demo/test.vue @@ -0,0 +1,70 @@ + + + diff --git a/site/dev.js b/site/dev.js index 56cfa6a0b..a1d27fefd 100644 --- a/site/dev.js +++ b/site/dev.js @@ -10,7 +10,7 @@ import Api from './components/api' import './components' import demoBox from './components/demoBox' import demoContainer from './components/demoContainer' -import Test from '../components/list/demo/test' +import Test from '../components/form/demo/test' Vue.use(VueClipboard) Vue.use(VueRouter)