Commit 257f80f1c2a84b8edffeb3b2e69d9d1fa9b18e84
1 parent
531cd165
support Form
support Form
Showing
7 changed files
with
52 additions
and
43 deletions
Show diff stats
README.md
examples/app.vue
| @@ -48,6 +48,7 @@ li + li { border-left: solid 1px #bbb; padding-left: 10px; margin-left: 10px; } | @@ -48,6 +48,7 @@ li + li { border-left: solid 1px #bbb; padding-left: 10px; margin-left: 10px; } | ||
| 48 | <li><router-link to="/page">Page</router-link></li> | 48 | <li><router-link to="/page">Page</router-link></li> |
| 49 | <li><router-link to="/transfer">Transfer</router-link></li> | 49 | <li><router-link to="/transfer">Transfer</router-link></li> |
| 50 | <li><router-link to="/date">Date</router-link></li> | 50 | <li><router-link to="/date">Date</router-link></li> |
| 51 | + <li><router-link to="/form">Form</router-link></li> | ||
| 51 | </ul> | 52 | </ul> |
| 52 | </nav> | 53 | </nav> |
| 53 | <router-view></router-view> | 54 | <router-view></router-view> |
examples/main.js
| @@ -42,12 +42,12 @@ const router = new VueRouter({ | @@ -42,12 +42,12 @@ const router = new VueRouter({ | ||
| 42 | component: require('./routers/checkbox.vue') | 42 | component: require('./routers/checkbox.vue') |
| 43 | }, | 43 | }, |
| 44 | { | 44 | { |
| 45 | - path: '/steps', | ||
| 46 | - component: require('./routers/steps.vue') | 45 | + path: '/steps', |
| 46 | + component: require('./routers/steps.vue') | ||
| 47 | }, | 47 | }, |
| 48 | { | 48 | { |
| 49 | - path: '/timeline', | ||
| 50 | - component: require('./routers/timeline.vue') | 49 | + path: '/timeline', |
| 50 | + component: require('./routers/timeline.vue') | ||
| 51 | }, | 51 | }, |
| 52 | { | 52 | { |
| 53 | path: '/switch', | 53 | path: '/switch', |
| @@ -156,7 +156,11 @@ const router = new VueRouter({ | @@ -156,7 +156,11 @@ const router = new VueRouter({ | ||
| 156 | { | 156 | { |
| 157 | path: '/date', | 157 | path: '/date', |
| 158 | component: require('./routers/date.vue') | 158 | component: require('./routers/date.vue') |
| 159 | - } | 159 | + }, |
| 160 | + { | ||
| 161 | + path: '/form', | ||
| 162 | + component: require('./routers/form.vue') | ||
| 163 | + }, | ||
| 160 | ] | 164 | ] |
| 161 | }); | 165 | }); |
| 162 | 166 |
examples/routers/form.vue
| 1 | <template> | 1 | <template> |
| 2 | - <i-form v-ref:form-validate :model="formValidate" :rules="ruleValidate" :label-width="100"> | ||
| 3 | - <Form-item label="输入框" prop="input"> | ||
| 4 | - <i-input :value.sync="formValidate.input" placeholder="请输入"></i-input> | 2 | + <i-form ref="formInline" :model="formInline" :rules="ruleInline" inline> |
| 3 | + <Form-item prop="user"> | ||
| 4 | + <Input type="text" v-model="formInline.user" placeholder="Username"> | ||
| 5 | + <Icon type="ios-person-outline" slot="prepend"></Icon> | ||
| 6 | + </Input> | ||
| 5 | </Form-item> | 7 | </Form-item> |
| 6 | - <Form-item label="Ajax:" prop="ajax"> | ||
| 7 | - <div slot="label"> | ||
| 8 | - <span>Ajax</span> | ||
| 9 | - <Tooltip content="基于 axios"> | ||
| 10 | - <Icon type="ios-help" size="14" color="#3399ff"></Icon> | ||
| 11 | - </Tooltip> | ||
| 12 | - <span>:</span> | ||
| 13 | - </div> | ||
| 14 | - <Switch :checked.sync="formValidate.ajax"></Switch> | 8 | + <Form-item prop="password"> |
| 9 | + <Input type="password" v-model="formInline.password" placeholder="Password"> | ||
| 10 | + <Icon type="ios-locked-outline" slot="prepend"></Icon> | ||
| 11 | + </Input> | ||
| 15 | </Form-item> | 12 | </Form-item> |
| 16 | <Form-item> | 13 | <Form-item> |
| 17 | - <i-button type="primary" @click="handleSubmit('formValidate')">提交</i-button> | ||
| 18 | - <i-button type="ghost" @click="handleReset('formValidate')" style="margin-left: 8px">重置</i-button> | 14 | + <Button type="primary" @click.native="handleSubmit('formInline')">登录</Button> |
| 19 | </Form-item> | 15 | </Form-item> |
| 20 | </i-form> | 16 | </i-form> |
| 21 | </template> | 17 | </template> |
| @@ -23,27 +19,30 @@ | @@ -23,27 +19,30 @@ | ||
| 23 | export default { | 19 | export default { |
| 24 | data () { | 20 | data () { |
| 25 | return { | 21 | return { |
| 26 | - formValidate: { | ||
| 27 | - input: '123', | ||
| 28 | - ajax: true | 22 | + formInline: { |
| 23 | + user: '', | ||
| 24 | + password: '' | ||
| 29 | }, | 25 | }, |
| 30 | - ruleValidate: { | ||
| 31 | - | 26 | + ruleInline: { |
| 27 | + user: [ | ||
| 28 | + { required: true, message: '请填写用户名', trigger: 'blur' } | ||
| 29 | + ], | ||
| 30 | + password: [ | ||
| 31 | + { required: true, message: '请填写密码', trigger: 'blur' }, | ||
| 32 | + { type: 'string', min: 6, message: '密码长度不能小于6位', trigger: 'blur' } | ||
| 33 | + ] | ||
| 32 | } | 34 | } |
| 33 | } | 35 | } |
| 34 | }, | 36 | }, |
| 35 | methods: { | 37 | methods: { |
| 36 | - handleSubmit (name) { | 38 | + handleSubmit(name) { |
| 37 | this.$refs[name].validate((valid) => { | 39 | this.$refs[name].validate((valid) => { |
| 38 | if (valid) { | 40 | if (valid) { |
| 39 | - this.$Message.success('提交成功!'); | 41 | + console.log('success'); |
| 40 | } else { | 42 | } else { |
| 41 | - this.$Message.error('表单验证失败!'); | 43 | + console.log('fail') |
| 42 | } | 44 | } |
| 43 | }) | 45 | }) |
| 44 | - }, | ||
| 45 | - handleReset (name) { | ||
| 46 | - this.$refs[name].resetFields(); | ||
| 47 | } | 46 | } |
| 48 | } | 47 | } |
| 49 | } | 48 | } |
src/components/form/form-item.vue
| @@ -3,7 +3,9 @@ | @@ -3,7 +3,9 @@ | ||
| 3 | <label :class="[prefixCls + '-label']" :style="labelStyles" v-if="label"><slot name="label">{{ label }}</slot></label> | 3 | <label :class="[prefixCls + '-label']" :style="labelStyles" v-if="label"><slot name="label">{{ label }}</slot></label> |
| 4 | <div :class="[prefixCls + '-content']" :style="contentStyles"> | 4 | <div :class="[prefixCls + '-content']" :style="contentStyles"> |
| 5 | <slot></slot> | 5 | <slot></slot> |
| 6 | - <div transition="fade" :class="[prefixCls + '-error-tip']" v-if="validateState === 'error' && showMessage && form.showMessage">{{ validateMessage }}</div> | 6 | + <transition name="fade"> |
| 7 | + <div :class="[prefixCls + '-error-tip']" v-if="validateState === 'error' && showMessage && form.showMessage">{{ validateMessage }}</div> | ||
| 8 | + </transition> | ||
| 7 | </div> | 9 | </div> |
| 8 | </div> | 10 | </div> |
| 9 | </template> | 11 | </template> |
| @@ -11,6 +13,7 @@ | @@ -11,6 +13,7 @@ | ||
| 11 | // https://github.com/ElemeFE/element/blob/dev/packages/form/src/form-item.vue | 13 | // https://github.com/ElemeFE/element/blob/dev/packages/form/src/form-item.vue |
| 12 | 14 | ||
| 13 | import AsyncValidator from 'async-validator'; | 15 | import AsyncValidator from 'async-validator'; |
| 16 | + import Emitter from '../../mixins/emitter'; | ||
| 14 | 17 | ||
| 15 | const prefixCls = 'ivu-form-item'; | 18 | const prefixCls = 'ivu-form-item'; |
| 16 | 19 | ||
| @@ -38,6 +41,8 @@ | @@ -38,6 +41,8 @@ | ||
| 38 | } | 41 | } |
| 39 | 42 | ||
| 40 | export default { | 43 | export default { |
| 44 | + name: 'FormItem', | ||
| 45 | + mixins: [ Emitter ], | ||
| 41 | props: { | 46 | props: { |
| 42 | label: { | 47 | label: { |
| 43 | type: String, | 48 | type: String, |
| @@ -206,9 +211,9 @@ | @@ -206,9 +211,9 @@ | ||
| 206 | this.validate('change'); | 211 | this.validate('change'); |
| 207 | } | 212 | } |
| 208 | }, | 213 | }, |
| 209 | - ready () { | 214 | + mounted () { |
| 210 | if (this.prop) { | 215 | if (this.prop) { |
| 211 | - this.$dispatch('on-form-item-add', this); | 216 | + this.dispatch('iForm', 'on-form-item-add', this); |
| 212 | 217 | ||
| 213 | Object.defineProperty(this, 'initialValue', { | 218 | Object.defineProperty(this, 'initialValue', { |
| 214 | value: this.fieldValue | 219 | value: this.fieldValue |
| @@ -229,7 +234,7 @@ | @@ -229,7 +234,7 @@ | ||
| 229 | } | 234 | } |
| 230 | }, | 235 | }, |
| 231 | beforeDestroy () { | 236 | beforeDestroy () { |
| 232 | - this.$dispatch('on-form-item-remove', this); | 237 | + this.dispatch('iForm', 'on-form-item-remove', this); |
| 233 | } | 238 | } |
| 234 | }; | 239 | }; |
| 235 | </script> | 240 | </script> |
| 236 | \ No newline at end of file | 241 | \ No newline at end of file |
src/components/form/form.vue
| @@ -82,15 +82,15 @@ | @@ -82,15 +82,15 @@ | ||
| 82 | this.validate(); | 82 | this.validate(); |
| 83 | } | 83 | } |
| 84 | }, | 84 | }, |
| 85 | - events: { | ||
| 86 | - 'on-form-item-add' (field) { | 85 | + created () { |
| 86 | + this.$on('on-form-item-add', (field) => { | ||
| 87 | if (field) this.fields.push(field); | 87 | if (field) this.fields.push(field); |
| 88 | return false; | 88 | return false; |
| 89 | - }, | ||
| 90 | - 'on-form-item-remove' (field) { | 89 | + }); |
| 90 | + this.$on('on-form-item-remove', (field) => { | ||
| 91 | if (field.prop) this.fields.splice(this.fields.indexOf(field), 1); | 91 | if (field.prop) this.fields.splice(this.fields.indexOf(field), 1); |
| 92 | return false; | 92 | return false; |
| 93 | - } | 93 | + }); |
| 94 | } | 94 | } |
| 95 | }; | 95 | }; |
| 96 | </script> | 96 | </script> |
| 97 | \ No newline at end of file | 97 | \ No newline at end of file |
src/index.js
| @@ -15,7 +15,7 @@ import Circle from './components/circle'; | @@ -15,7 +15,7 @@ import Circle from './components/circle'; | ||
| 15 | import Collapse from './components/collapse'; | 15 | import Collapse from './components/collapse'; |
| 16 | import DatePicker from './components/date-picker'; | 16 | import DatePicker from './components/date-picker'; |
| 17 | import Dropdown from './components/dropdown'; | 17 | import Dropdown from './components/dropdown'; |
| 18 | -// import Form from './components/form'; | 18 | +import Form from './components/form'; |
| 19 | import Icon from './components/icon'; | 19 | import Icon from './components/icon'; |
| 20 | import Input from './components/input'; | 20 | import Input from './components/input'; |
| 21 | import InputNumber from './components/input-number'; | 21 | import InputNumber from './components/input-number'; |
| @@ -67,8 +67,8 @@ const iview = { | @@ -67,8 +67,8 @@ const iview = { | ||
| 67 | Dropdown, | 67 | Dropdown, |
| 68 | DropdownItem: Dropdown.Item, | 68 | DropdownItem: Dropdown.Item, |
| 69 | DropdownMenu: Dropdown.Menu, | 69 | DropdownMenu: Dropdown.Menu, |
| 70 | - // iForm: Form, | ||
| 71 | - // FormItem: Form.Item, | 70 | + iForm: Form, |
| 71 | + FormItem: Form.Item, | ||
| 72 | iCol: Col, | 72 | iCol: Col, |
| 73 | Collapse, | 73 | Collapse, |
| 74 | Icon, | 74 | Icon, |