Commit 257f80f1c2a84b8edffeb3b2e69d9d1fa9b18e84

Authored by 梁灏
1 parent 531cd165

support Form

support Form
README.md
... ... @@ -34,7 +34,7 @@
34 34 - [x] InputNumber
35 35 - [x] Rate
36 36 - [x] Upload
37   -- [ ] Form
  37 +- [x] Form
38 38 - [x] Alert
39 39 - [x] Card
40 40 - [ ] Message
... ...
examples/app.vue
... ... @@ -48,6 +48,7 @@ li + li { border-left: solid 1px #bbb; padding-left: 10px; margin-left: 10px; }
48 48 <li><router-link to="/page">Page</router-link></li>
49 49 <li><router-link to="/transfer">Transfer</router-link></li>
50 50 <li><router-link to="/date">Date</router-link></li>
  51 + <li><router-link to="/form">Form</router-link></li>
51 52 </ul>
52 53 </nav>
53 54 <router-view></router-view>
... ...
examples/main.js
... ... @@ -42,12 +42,12 @@ const router = new VueRouter({
42 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 53 path: '/switch',
... ... @@ -156,7 +156,11 @@ const router = new VueRouter({
156 156 {
157 157 path: '/date',
158 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 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 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 12 </Form-item>
16 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 15 </Form-item>
20 16 </i-form>
21 17 </template>
... ... @@ -23,27 +19,30 @@
23 19 export default {
24 20 data () {
25 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 37 methods: {
36   - handleSubmit (name) {
  38 + handleSubmit(name) {
37 39 this.$refs[name].validate((valid) => {
38 40 if (valid) {
39   - this.$Message.success('提交成功!');
  41 + console.log('success');
40 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 3 <label :class="[prefixCls + '-label']" :style="labelStyles" v-if="label"><slot name="label">{{ label }}</slot></label>
4 4 <div :class="[prefixCls + '-content']" :style="contentStyles">
5 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 9 </div>
8 10 </div>
9 11 </template>
... ... @@ -11,6 +13,7 @@
11 13 // https://github.com/ElemeFE/element/blob/dev/packages/form/src/form-item.vue
12 14  
13 15 import AsyncValidator from 'async-validator';
  16 + import Emitter from '../../mixins/emitter';
14 17  
15 18 const prefixCls = 'ivu-form-item';
16 19  
... ... @@ -38,6 +41,8 @@
38 41 }
39 42  
40 43 export default {
  44 + name: 'FormItem',
  45 + mixins: [ Emitter ],
41 46 props: {
42 47 label: {
43 48 type: String,
... ... @@ -206,9 +211,9 @@
206 211 this.validate('change');
207 212 }
208 213 },
209   - ready () {
  214 + mounted () {
210 215 if (this.prop) {
211   - this.$dispatch('on-form-item-add', this);
  216 + this.dispatch('iForm', 'on-form-item-add', this);
212 217  
213 218 Object.defineProperty(this, 'initialValue', {
214 219 value: this.fieldValue
... ... @@ -229,7 +234,7 @@
229 234 }
230 235 },
231 236 beforeDestroy () {
232   - this.$dispatch('on-form-item-remove', this);
  237 + this.dispatch('iForm', 'on-form-item-remove', this);
233 238 }
234 239 };
235 240 </script>
236 241 \ No newline at end of file
... ...
src/components/form/form.vue
... ... @@ -82,15 +82,15 @@
82 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 87 if (field) this.fields.push(field);
88 88 return false;
89   - },
90   - 'on-form-item-remove' (field) {
  89 + });
  90 + this.$on('on-form-item-remove', (field) => {
91 91 if (field.prop) this.fields.splice(this.fields.indexOf(field), 1);
92 92 return false;
93   - }
  93 + });
94 94 }
95 95 };
96 96 </script>
97 97 \ No newline at end of file
... ...
src/index.js
... ... @@ -15,7 +15,7 @@ import Circle from &#39;./components/circle&#39;;
15 15 import Collapse from './components/collapse';
16 16 import DatePicker from './components/date-picker';
17 17 import Dropdown from './components/dropdown';
18   -// import Form from './components/form';
  18 +import Form from './components/form';
19 19 import Icon from './components/icon';
20 20 import Input from './components/input';
21 21 import InputNumber from './components/input-number';
... ... @@ -67,8 +67,8 @@ const iview = {
67 67 Dropdown,
68 68 DropdownItem: Dropdown.Item,
69 69 DropdownMenu: Dropdown.Menu,
70   - // iForm: Form,
71   - // FormItem: Form.Item,
  70 + iForm: Form,
  71 + FormItem: Form.Item,
72 72 iCol: Col,
73 73 Collapse,
74 74 Icon,
... ...