Commit 21dad188c11ae33e6cb4a940a5ea1ae01b23fee2

Authored by 梁灏
1 parent 4a5d5cc9

prevent dispatch form event

when component import an Input, prevent dispatch event from Input to
Form
examples/routers/form.vue
1 <template> 1 <template>
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>  
7 - </Form-item>  
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>  
12 - </Form-item>  
13 - <Form-item>  
14 - <Button type="primary" @click.native="handleSubmit('formInline')">登录</Button>  
15 - </Form-item>  
16 - </i-form> 2 + <div>
  3 + date: {{ formInline.date }}
  4 + <i-form ref="formInline" :model="formInline" :rules="ruleInline">
  5 + <Form-item prop="date">
  6 + <Date-picker type="date" placeholder="选择日期" v-model="formInline.date"></Date-picker>
  7 + </Form-item>
  8 + <Form-item prop="user">
  9 + <Input v-model="formInline.user">
  10 + </Form-item>
  11 + <Form-item>
  12 + <i-button type="primary" @click.native="handleSubmit('formInline')">登录</i-button>
  13 + </Form-item>
  14 + </i-form>
  15 + </div>
17 </template> 16 </template>
18 <script> 17 <script>
19 export default { 18 export default {
20 data () { 19 data () {
21 return { 20 return {
22 formInline: { 21 formInline: {
23 - user: '',  
24 - password: '' 22 + date: new Date(),
  23 + user: ''
25 }, 24 },
26 ruleInline: { 25 ruleInline: {
27 - user: [  
28 - { required: true, message: '请填写用户名', trigger: 'blur' } 26 + date: [
  27 + {
  28 + required: true,
  29 + type: 'date',
  30 + message: '请选择日期',
  31 + trigger: 'change'
  32 + }
29 ], 33 ],
30 - password: [  
31 - { required: true, message: '请填写密码', trigger: 'blur' },  
32 - { type: 'string', min: 6, message: '密码长度不能小于6位', trigger: 'blur' } 34 + user: [
  35 + {
  36 + required: true,
  37 + message: '请输入',
  38 + trigger: 'change',
  39 + min: 10
  40 + },
  41 + {
  42 + required: true,
  43 + message: '请输入2',
  44 + trigger: 'blur'
  45 + }
33 ] 46 ]
34 } 47 }
35 } 48 }
@@ -38,11 +51,14 @@ @@ -38,11 +51,14 @@
38 handleSubmit(name) { 51 handleSubmit(name) {
39 this.$refs[name].validate((valid) => { 52 this.$refs[name].validate((valid) => {
40 if (valid) { 53 if (valid) {
41 - console.log('success'); 54 + this.$Message.success('提交成功!');
42 } else { 55 } else {
43 - console.log('fail') 56 + this.$Message.error('表单验证失败!');
44 } 57 }
45 }) 58 })
  59 + },
  60 + handleInput (val) {
  61 + console.log(val)
46 } 62 }
47 } 63 }
48 } 64 }
examples/routers/message.vue
1 <template> 1 <template>
2 - <i-button @click.native="time">显示一个10秒的提示</i-button> 2 + <div>
  3 + <i-button @click.native="success">显示成功提示</i-button>
  4 + <i-button @click.native="warning">显示警告提示</i-button>
  5 + <i-button @click.native="error">显示错误提示</i-button>
  6 + </div>
3 </template> 7 </template>
4 <script> 8 <script>
5 export default { 9 export default {
6 methods: { 10 methods: {
7 - time () {  
8 - this.$Message.info('我将在10秒后消失', 3, () => {  
9 - console.log(1111)  
10 - }); 11 + success () {
  12 + this.$Message.success('这是一条成功的提示');
  13 + },
  14 + warning () {
  15 + this.$Message.warning('这是一条警告的提示');
  16 + },
  17 + error () {
  18 + this.$Message.error('对方不想说话,并且向你抛出了一个异常');
11 } 19 }
12 - },  
13 - mounted () {  
14 - this.$Message.config({  
15 - top: 50,  
16 - duration: 3  
17 - });  
18 } 20 }
19 } 21 }
20 </script> 22 </script>
src/components/base/notification/notification.vue
@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 <div :class="classes" :style="styles"> 2 <div :class="classes" :style="styles">
3 <Notice 3 <Notice
4 v-for="notice in notices" 4 v-for="notice in notices"
5 - :key="notice" 5 + :key="notice.name"
6 :prefix-cls="prefixCls" 6 :prefix-cls="prefixCls"
7 :styles="notice.styles" 7 :styles="notice.styles"
8 :content="notice.content" 8 :content="notice.content"
@@ -15,7 +15,6 @@ @@ -15,7 +15,6 @@
15 </div> 15 </div>
16 </template> 16 </template>
17 <script> 17 <script>
18 - // todo :key="notice"  
19 import Notice from './notice.vue'; 18 import Notice from './notice.vue';
20 19
21 const prefixCls = 'ivu-notification'; 20 const prefixCls = 'ivu-notification';
src/components/date-picker/panel/time-range.vue
@@ -57,6 +57,7 @@ @@ -57,6 +57,7 @@
57 const timePrefixCls = 'ivu-time-picker'; 57 const timePrefixCls = 'ivu-time-picker';
58 58
59 export default { 59 export default {
  60 + name: 'TimePicker',
60 mixins: [ Mixin, Locale ], 61 mixins: [ Mixin, Locale ],
61 components: { TimeSpinner, Confirm }, 62 components: { TimeSpinner, Confirm },
62 data () { 63 data () {
src/components/date-picker/panel/time.vue
@@ -36,6 +36,7 @@ @@ -36,6 +36,7 @@
36 const timePrefixCls = 'ivu-time-picker'; 36 const timePrefixCls = 'ivu-time-picker';
37 37
38 export default { 38 export default {
  39 + name: 'TimePicker',
39 mixins: [ Mixin, Locale ], 40 mixins: [ Mixin, Locale ],
40 components: { TimeSpinner, Confirm }, 41 components: { TimeSpinner, Confirm },
41 data () { 42 data () {
src/components/date-picker/picker.vue
@@ -139,6 +139,7 @@ @@ -139,6 +139,7 @@
139 }; 139 };
140 140
141 export default { 141 export default {
  142 + name: 'CalendarPicker',
142 mixins: [ Emitter ], 143 mixins: [ Emitter ],
143 components: { iInput, Drop }, 144 components: { iInput, Drop },
144 directives: { clickoutside }, 145 directives: { clickoutside },
@@ -344,6 +345,7 @@ @@ -344,6 +345,7 @@
344 this.visualValue = correctValue; 345 this.visualValue = correctValue;
345 event.target.value = correctValue; 346 event.target.value = correctValue;
346 this.internalValue = correctDate; 347 this.internalValue = correctDate;
  348 + this.currentValue = correctDate;
347 349
348 if (correctValue !== oldValue) this.emitChange(correctDate); 350 if (correctValue !== oldValue) this.emitChange(correctDate);
349 }, 351 },
@@ -418,6 +420,12 @@ @@ -418,6 +420,12 @@
418 this.picker.resetView && this.picker.resetView(); 420 this.picker.resetView && this.picker.resetView();
419 }, 421 },
420 emitChange (date) { 422 emitChange (date) {
  423 + const newDate = this.formattingDate(date);
  424 +
  425 + this.$emit('on-change', newDate);
  426 + this.dispatch('FormItem', 'on-form-change', newDate);
  427 + },
  428 + formattingDate (date) {
421 const type = this.type; 429 const type = this.type;
422 const format = this.format || DEFAULT_FORMATS[type]; 430 const format = this.format || DEFAULT_FORMATS[type];
423 const formatter = ( 431 const formatter = (
@@ -429,9 +437,7 @@ @@ -429,9 +437,7 @@
429 if (type === 'daterange' || type === 'timerange') { 437 if (type === 'daterange' || type === 'timerange') {
430 newDate = [newDate.split(RANGE_SEPARATOR)[0], newDate.split(RANGE_SEPARATOR)[1]]; 438 newDate = [newDate.split(RANGE_SEPARATOR)[0], newDate.split(RANGE_SEPARATOR)[1]];
431 } 439 }
432 -  
433 - this.$emit('on-change', newDate);  
434 - this.dispatch('FormItem', 'on-form-change', newDate); 440 + return newDate;
435 } 441 }
436 }, 442 },
437 watch: { 443 watch: {
@@ -450,7 +456,7 @@ @@ -450,7 +456,7 @@
450 if (!val && this.picker && typeof this.picker.handleClear === 'function') { 456 if (!val && this.picker && typeof this.picker.handleClear === 'function') {
451 this.picker.handleClear(); 457 this.picker.handleClear();
452 } 458 }
453 - this.$emit('input', val); 459 +// this.$emit('input', val);
454 }, 460 },
455 value (val) { 461 value (val) {
456 this.currentValue = val; 462 this.currentValue = val;
@@ -492,14 +498,5 @@ @@ -492,14 +498,5 @@
492 mounted () { 498 mounted () {
493 if (this.open !== null) this.visible = this.open; 499 if (this.open !== null) this.visible = this.open;
494 } 500 }
495 - // todo 事件  
496 -// events: {  
497 -// 'on-form-blur' () {  
498 -// return false;  
499 -// },  
500 -// 'on-form-change' () {  
501 -// return false;  
502 -// }  
503 -// }  
504 }; 501 };
505 </script> 502 </script>
src/components/input/input.vue
@@ -43,7 +43,7 @@ @@ -43,7 +43,7 @@
43 </div> 43 </div>
44 </template> 44 </template>
45 <script> 45 <script>
46 - import { oneOf } from '../../utils/assist'; 46 + import { oneOf, findComponentUpward } from '../../utils/assist';
47 import calcTextareaHeight from '../../utils/calcTextareaHeight'; 47 import calcTextareaHeight from '../../utils/calcTextareaHeight';
48 import Emitter from '../../mixins/emitter'; 48 import Emitter from '../../mixins/emitter';
49 49
@@ -152,7 +152,9 @@ @@ -152,7 +152,9 @@
152 }, 152 },
153 handleBlur () { 153 handleBlur () {
154 this.$emit('on-blur'); 154 this.$emit('on-blur');
155 - this.dispatch('FormItem', 'on-form-blur', this.currentValue); 155 + if (!findComponentUpward(this, ['DatePicker', 'TimePicker'])) {
  156 + this.dispatch('FormItem', 'on-form-blur', this.currentValue);
  157 + }
156 }, 158 },
157 handleInput (event) { 159 handleInput (event) {
158 const value = event.target.value; 160 const value = event.target.value;
@@ -169,7 +171,9 @@ @@ -169,7 +171,9 @@
169 this.resizeTextarea(); 171 this.resizeTextarea();
170 }); 172 });
171 this.currentValue = value; 173 this.currentValue = value;
172 - this.dispatch('FormItem', 'on-form-change', value); 174 + if (!findComponentUpward(this, ['DatePicker', 'TimePicker'])) {
  175 + this.dispatch('FormItem', 'on-form-change', value);
  176 + }
173 }, 177 },
174 resizeTextarea () { 178 resizeTextarea () {
175 const autosize = this.autosize; 179 const autosize = this.autosize;
src/utils/assist.js
@@ -165,4 +165,22 @@ export function scrollTop(el, from = 0, to, duration = 500) { @@ -165,4 +165,22 @@ export function scrollTop(el, from = 0, to, duration = 500) {
165 window.requestAnimationFrame(() => scroll(d, end, step)); 165 window.requestAnimationFrame(() => scroll(d, end, step));
166 } 166 }
167 scroll(from, to, step); 167 scroll(from, to, step);
168 -}  
169 \ No newline at end of file 168 \ No newline at end of file
  169 +}
  170 +
  171 +// Find components upward
  172 +function findComponentUpward (content, componentName, componentNames) {
  173 + if (typeof componentName === 'string') {
  174 + componentNames = [componentName];
  175 + } else {
  176 + componentNames = componentName;
  177 + }
  178 +
  179 + let parent = content.$parent;
  180 + let name = parent.$options.name;
  181 + while (parent && (!name || componentNames.indexOf(name) < 0)) {
  182 + parent = parent.$parent;
  183 + if (parent) name = parent.$options.name;
  184 + }
  185 + return parent;
  186 +}
  187 +export {findComponentUpward};
170 \ No newline at end of file 188 \ No newline at end of file