feat: add form support template
parent
51718d7ffc
commit
090f9cba5d
|
@ -57,6 +57,7 @@ export const FormProps = {
|
||||||
// onSubmit: React.FormEventHandler<any>;
|
// onSubmit: React.FormEventHandler<any>;
|
||||||
prefixCls: PropTypes.string,
|
prefixCls: PropTypes.string,
|
||||||
hideRequiredMark: PropTypes.bool,
|
hideRequiredMark: PropTypes.bool,
|
||||||
|
formRef: PropTypes.func,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ValidationRule = {
|
export const ValidationRule = {
|
||||||
|
@ -178,7 +179,7 @@ export default {
|
||||||
|
|
||||||
render () {
|
render () {
|
||||||
const {
|
const {
|
||||||
prefixCls, hideRequiredMark, layout, onSubmit, $slots,
|
prefixCls, hideRequiredMark, layout, onSubmit, $slots, formRef,
|
||||||
} = this
|
} = this
|
||||||
|
|
||||||
const formClassName = classNames(prefixCls, {
|
const formClassName = classNames(prefixCls, {
|
||||||
|
@ -187,6 +188,26 @@ export default {
|
||||||
[`${prefixCls}-inline`]: layout === 'inline',
|
[`${prefixCls}-inline`]: layout === 'inline',
|
||||||
[`${prefixCls}-hide-required-mark`]: hideRequiredMark,
|
[`${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 <form onSubmit={onSubmit} class={formClassName}>{$slots.default}</form>
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return <NewForm />
|
||||||
|
}
|
||||||
|
|
||||||
return <form onSubmit={onSubmit} class={formClassName}>{$slots.default}</form>
|
return <form onSubmit={onSubmit} class={formClassName}>{$slots.default}</form>
|
||||||
},
|
},
|
||||||
|
|
|
@ -20,6 +20,8 @@ export const FormItemProps = {
|
||||||
hasFeedback: PropTypes.bool,
|
hasFeedback: PropTypes.bool,
|
||||||
required: PropTypes.bool,
|
required: PropTypes.bool,
|
||||||
colon: PropTypes.bool,
|
colon: PropTypes.bool,
|
||||||
|
fieldDecoratorId: PropTypes.string,
|
||||||
|
fieldDecoratorOptions: PropTypes.object,
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
@ -33,6 +35,7 @@ export default {
|
||||||
}),
|
}),
|
||||||
inject: {
|
inject: {
|
||||||
FormProps: { default: {}},
|
FormProps: { default: {}},
|
||||||
|
NewFormProps: { default: {}},
|
||||||
},
|
},
|
||||||
data () {
|
data () {
|
||||||
return { helpShow: false }
|
return { helpShow: false }
|
||||||
|
@ -90,7 +93,7 @@ export default {
|
||||||
},
|
},
|
||||||
|
|
||||||
getOnlyControl () {
|
getOnlyControl () {
|
||||||
const child = this.getControls(this.$slots.default, false)[0]
|
const child = this.getControls(this.slotDefault, false)[0]
|
||||||
return child !== undefined ? child : null
|
return child !== undefined ? child : null
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -303,12 +306,20 @@ export default {
|
||||||
) : null
|
) : null
|
||||||
},
|
},
|
||||||
renderChildren () {
|
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 [
|
return [
|
||||||
this.renderLabel(),
|
this.renderLabel(),
|
||||||
this.renderWrapper(
|
this.renderWrapper(
|
||||||
this.renderValidateWrapper(
|
this.renderValidateWrapper(
|
||||||
filterEmpty($slots.default || []),
|
this.slotDefault,
|
||||||
this.renderHelp(),
|
this.renderHelp(),
|
||||||
this.renderExtra(),
|
this.renderExtra(),
|
||||||
),
|
),
|
||||||
|
@ -333,6 +344,13 @@ export default {
|
||||||
},
|
},
|
||||||
|
|
||||||
render () {
|
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()
|
const children = this.renderChildren()
|
||||||
return this.renderFormItem(children)
|
return this.renderFormItem(children)
|
||||||
},
|
},
|
||||||
|
|
|
@ -0,0 +1,70 @@
|
||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<a-form @submit="handleSubmit" :formRef="(form)=>{this.form = form}">
|
||||||
|
<a-form-item
|
||||||
|
label='Note'
|
||||||
|
:labelCol="{ span: 5 }"
|
||||||
|
:wrapperCol="{ span: 12 }"
|
||||||
|
fieldDecoratorId="note"
|
||||||
|
:fieldDecoratorOptions="{rules: [{ required: true, message: 'Please input your note!' }]}"
|
||||||
|
>
|
||||||
|
<a-input />
|
||||||
|
</a-form-item>
|
||||||
|
<a-form-item
|
||||||
|
label='Gender'
|
||||||
|
:labelCol="{ span: 5 }"
|
||||||
|
:wrapperCol="{ span: 12 }"
|
||||||
|
fieldDecoratorId="gender"
|
||||||
|
:fieldDecoratorOptions="{rules: [{ required: true, message: 'Please select your gender!' }]}"
|
||||||
|
>
|
||||||
|
<a-select
|
||||||
|
placeholder='Select a option and change input text above'
|
||||||
|
@change="this.handleSelectChange"
|
||||||
|
>
|
||||||
|
<a-select-option value='male'>male</a-select-option>
|
||||||
|
<a-select-option value='female'>female</a-select-option>
|
||||||
|
</a-select>
|
||||||
|
</a-form-item>
|
||||||
|
<a-form-item
|
||||||
|
:wrapperCol="{ span: 12, offset: 5 }"
|
||||||
|
>
|
||||||
|
<a-button type='primary' htmlType='submit'>
|
||||||
|
Submit
|
||||||
|
</a-button>
|
||||||
|
</a-form-item>
|
||||||
|
</a-form>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
export default {
|
||||||
|
data () {
|
||||||
|
return {
|
||||||
|
formLayout: 'horizontal',
|
||||||
|
rules: {
|
||||||
|
test: [{
|
||||||
|
type: 'email', message: 'The input is not valid E-mail!',
|
||||||
|
}, {
|
||||||
|
required: true, message: 'Please input your E-mail!',
|
||||||
|
}],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
handleSubmit (e) {
|
||||||
|
e.preventDefault()
|
||||||
|
this.form.validateFields((err, values) => {
|
||||||
|
if (!err) {
|
||||||
|
console.log('Received values of form: ', values)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
handleSelectChange (value) {
|
||||||
|
console.log(value)
|
||||||
|
this.form.setFieldsValue({
|
||||||
|
note: `Hi, ${value === 'male' ? 'man' : 'lady'}!`,
|
||||||
|
})
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
</script>
|
|
@ -10,7 +10,7 @@ import Api from './components/api'
|
||||||
import './components'
|
import './components'
|
||||||
import demoBox from './components/demoBox'
|
import demoBox from './components/demoBox'
|
||||||
import demoContainer from './components/demoContainer'
|
import demoContainer from './components/demoContainer'
|
||||||
import Test from '../components/list/demo/test'
|
import Test from '../components/form/demo/test'
|
||||||
|
|
||||||
Vue.use(VueClipboard)
|
Vue.use(VueClipboard)
|
||||||
Vue.use(VueRouter)
|
Vue.use(VueRouter)
|
||||||
|
|
Loading…
Reference in New Issue