0a48ac45
梁灏
Input add readonl...
|
1
|
<template>
|
c463ab87
梁灏
add Cascader
|
2
|
<div :class="classes" v-clickoutside="handleClose">
|
75e5c6a5
梁灏
Cascader support ...
|
3
4
5
6
7
|
<div :class="[prefixCls + '-rel']" @click="toggleOpen">
<slot>
<i-input
readonly
:disabled="disabled"
|
47a7f21d
梁灏
support Cascader
|
8
|
v-model="displayRender"
|
75e5c6a5
梁灏
Cascader support ...
|
9
10
|
:size="size"
:placeholder="placeholder"></i-input>
|
47a7f21d
梁灏
support Cascader
|
11
|
<Icon type="ios-close" :class="[prefixCls + '-arrow']" v-show="showCloseIcon" @click.native.stop="clearSelect"></Icon>
|
75e5c6a5
梁灏
Cascader support ...
|
12
13
14
|
<Icon type="arrow-down-b" :class="[prefixCls + '-arrow']"></Icon>
</slot>
</div>
|
47a7f21d
梁灏
support Cascader
|
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<transition name="slide-up">
<Drop v-show="visible">
<div>
<Caspanel
ref="caspanel"
:prefix-cls="prefixCls"
:data="data"
:disabled="disabled"
:change-on-select="changeOnSelect"
:trigger="trigger"></Caspanel>
</div>
</Drop>
</transition>
|
6ff31952
梁灏
optimize Input sh...
|
28
|
</div>
|
0a48ac45
梁灏
Input add readonl...
|
29
30
|
</template>
<script>
|
6ff31952
梁灏
optimize Input sh...
|
31
|
import iInput from '../input/input.vue';
|
47a7f21d
梁灏
support Cascader
|
32
|
import Drop from '../select/dropdown.vue';
|
c463ab87
梁灏
add Cascader
|
33
34
|
import Icon from '../icon/icon.vue';
import Caspanel from './caspanel.vue';
|
6ff31952
梁灏
optimize Input sh...
|
35
|
import clickoutside from '../../directives/clickoutside';
|
c463ab87
梁灏
add Cascader
|
36
|
import { oneOf } from '../../utils/assist';
|
47a7f21d
梁灏
support Cascader
|
37
|
import Emitter from '../../mixins/emitter';
|
6ff31952
梁灏
optimize Input sh...
|
38
39
40
|
const prefixCls = 'ivu-cascader';
|
0a48ac45
梁灏
Input add readonl...
|
41
|
export default {
|
34ee7b4a
梁灏
support Tree & ad...
|
42
|
name: 'Cascader',
|
47a7f21d
梁灏
support Cascader
|
43
44
|
mixins: [ Emitter ],
components: { iInput, Drop, Icon, Caspanel },
|
c463ab87
梁灏
add Cascader
|
45
|
directives: { clickoutside },
|
0a48ac45
梁灏
Input add readonl...
|
46
|
props: {
|
6ff31952
梁灏
optimize Input sh...
|
47
48
49
|
data: {
type: Array,
default () {
|
b0893113
jingsam
add eslint
|
50
|
return [];
|
6ff31952
梁灏
optimize Input sh...
|
51
52
53
|
}
},
value: {
|
9ec927b1
梁灏
update Cascader
|
54
55
|
type: Array,
default () {
|
b0893113
jingsam
add eslint
|
56
|
return [];
|
9ec927b1
梁灏
update Cascader
|
57
|
}
|
6ff31952
梁灏
optimize Input sh...
|
58
59
60
61
62
63
64
|
},
disabled: {
type: Boolean,
default: false
},
clearable: {
type: Boolean,
|
c463ab87
梁灏
add Cascader
|
65
|
default: true
|
6ff31952
梁灏
optimize Input sh...
|
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
},
placeholder: {
type: String,
default: '请选择'
},
size: {
validator (value) {
return oneOf(value, ['small', 'large']);
}
},
trigger: {
validator (value) {
return oneOf(value, ['click', 'hover']);
},
default: 'click'
},
changeOnSelect: {
type: Boolean,
default: false
},
renderFormat: {
type: Function,
|
05b5dd7b
梁灏
update Cascader
|
88
|
default (label) {
|
bd4e3b9b
梁灏
update Cascader
|
89
|
return label.join(' / ');
|
6ff31952
梁灏
optimize Input sh...
|
90
91
|
}
}
|
0a48ac45
梁灏
Input add readonl...
|
92
93
94
|
},
data () {
return {
|
6ff31952
梁灏
optimize Input sh...
|
95
|
prefixCls: prefixCls,
|
c463ab87
梁灏
add Cascader
|
96
97
|
visible: false,
selected: [],
|
2810d8c7
梁灏
fixed #183
|
98
|
tmpSelected: [],
|
47a7f21d
梁灏
support Cascader
|
99
100
|
updatingValue: false, // to fix set value in changeOnSelect type
currentValue: this.value
|
b0893113
jingsam
add eslint
|
101
|
};
|
0a48ac45
梁灏
Input add readonl...
|
102
103
|
},
computed: {
|
c463ab87
梁灏
add Cascader
|
104
105
106
107
|
classes () {
return [
`${prefixCls}`,
{
|
165bb7c9
梁灏
update Cascader
|
108
|
[`${prefixCls}-show-clear`]: this.showCloseIcon,
|
05b5dd7b
梁灏
update Cascader
|
109
110
|
[`${prefixCls}-visible`]: this.visible,
[`${prefixCls}-disabled`]: this.disabled
|
c463ab87
梁灏
add Cascader
|
111
|
}
|
b0893113
jingsam
add eslint
|
112
|
];
|
c463ab87
梁灏
add Cascader
|
113
114
|
},
showCloseIcon () {
|
65b41a2d
梁灏
fixed #635
|
115
|
return this.currentValue && this.currentValue.length && this.clearable && !this.disabled;
|
c463ab87
梁灏
add Cascader
|
116
|
},
|
6ff31952
梁灏
optimize Input sh...
|
117
118
119
120
121
122
|
displayRender () {
let label = [];
for (let i = 0; i < this.selected.length; i++) {
label.push(this.selected[i].label);
}
|
165bb7c9
梁灏
update Cascader
|
123
|
return this.renderFormat(label, this.selected);
|
6ff31952
梁灏
optimize Input sh...
|
124
|
}
|
0a48ac45
梁灏
Input add readonl...
|
125
126
|
},
methods: {
|
c463ab87
梁灏
add Cascader
|
127
|
clearSelect () {
|
65b41a2d
梁灏
fixed #635
|
128
|
if (this.disabled) return false;
|
47a7f21d
梁灏
support Cascader
|
129
130
|
const oldVal = JSON.stringify(this.currentValue);
this.currentValue = this.selected = this.tmpSelected = [];
|
165bb7c9
梁灏
update Cascader
|
131
|
this.handleClose();
|
47a7f21d
梁灏
support Cascader
|
132
133
134
|
this.emitValue(this.currentValue, oldVal);
// this.$broadcast('on-clear');
this.broadcast('Caspanel', 'on-clear');
|
c463ab87
梁灏
add Cascader
|
135
136
137
138
|
},
handleClose () {
this.visible = false;
},
|
75e5c6a5
梁灏
Cascader support ...
|
139
|
toggleOpen () {
|
2aa0aa6e
Rijn
fix bug: cascader...
|
140
|
if (this.disabled) return false;
|
75e5c6a5
梁灏
Cascader support ...
|
141
142
143
144
145
146
|
if (this.visible) {
this.handleClose();
} else {
this.onFocus();
}
},
|
c463ab87
梁灏
add Cascader
|
147
148
|
onFocus () {
this.visible = true;
|
47a7f21d
梁灏
support Cascader
|
149
150
|
if (!this.currentValue.length) {
this.broadcast('Caspanel', 'on-clear');
|
165bb7c9
梁灏
update Cascader
|
151
|
}
|
c463ab87
梁灏
add Cascader
|
152
153
|
},
updateResult (result) {
|
bd4e3b9b
梁灏
update Cascader
|
154
155
|
this.tmpSelected = result;
},
|
165bb7c9
梁灏
update Cascader
|
156
157
|
updateSelected (init = false) {
if (!this.changeOnSelect || init) {
|
47a7f21d
梁灏
support Cascader
|
158
159
160
|
this.broadcast('Caspanel', 'on-find-selected', {
value: this.currentValue
});
|
165bb7c9
梁灏
update Cascader
|
161
162
163
164
|
}
},
emitValue (val, oldVal) {
if (JSON.stringify(val) !== oldVal) {
|
47a7f21d
梁灏
support Cascader
|
165
|
this.$emit('on-change', this.currentValue, JSON.parse(JSON.stringify(this.selected)));
|
cc419499
梁灏
fixed #525
|
166
167
168
169
170
|
this.$nextTick(() => {
this.dispatch('FormItem', 'on-form-change', {
value: this.currentValue,
selected: JSON.parse(JSON.stringify(this.selected))
});
|
cd78c9c4
梁灏
some comps suppor...
|
171
|
});
|
165bb7c9
梁灏
update Cascader
|
172
|
}
|
c463ab87
梁灏
add Cascader
|
173
174
|
}
},
|
47a7f21d
梁灏
support Cascader
|
175
|
mounted () {
|
165bb7c9
梁灏
update Cascader
|
176
|
this.updateSelected(true);
|
47a7f21d
梁灏
support Cascader
|
177
178
179
180
181
182
183
|
this.$on('on-result-change', (params) => {
// lastValue: is click the final val
// fromInit: is this emit from update value
const lastValue = params.lastValue;
const changeOnSelect = params.changeOnSelect;
const fromInit = params.fromInit;
|
bd4e3b9b
梁灏
update Cascader
|
184
|
if (lastValue || changeOnSelect) {
|
47a7f21d
梁灏
support Cascader
|
185
|
const oldVal = JSON.stringify(this.currentValue);
|
bd4e3b9b
梁灏
update Cascader
|
186
187
188
189
190
191
|
this.selected = this.tmpSelected;
let newVal = [];
this.selected.forEach((item) => {
newVal.push(item.value);
});
|
c463ab87
梁灏
add Cascader
|
192
|
|
165bb7c9
梁灏
update Cascader
|
193
|
if (!fromInit) {
|
2810d8c7
梁灏
fixed #183
|
194
|
this.updatingValue = true;
|
47a7f21d
梁灏
support Cascader
|
195
196
|
this.currentValue = newVal;
this.emitValue(this.currentValue, oldVal);
|
bd4e3b9b
梁灏
update Cascader
|
197
198
199
200
201
|
}
}
if (lastValue && !fromInit) {
this.handleClose();
}
|
47a7f21d
梁灏
support Cascader
|
202
|
});
|
bd4e3b9b
梁灏
update Cascader
|
203
|
},
|
bd4e3b9b
梁灏
update Cascader
|
204
205
206
|
watch: {
visible (val) {
if (val) {
|
47a7f21d
梁灏
support Cascader
|
207
|
if (this.currentValue.length) {
|
bd4e3b9b
梁灏
update Cascader
|
208
209
210
|
this.updateSelected();
}
}
|
bb1a3c38
梁灏
fixed #593
|
211
|
this.$emit('on-visible-change', val);
|
f46ebc38
梁灏
fixed #130
|
212
|
},
|
47a7f21d
梁灏
support Cascader
|
213
214
|
value (val) {
this.currentValue = val;
|
3bbb6b5f
梁灏
fixed #488
|
215
|
if (!val.length) this.selected = [];
|
47a7f21d
梁灏
support Cascader
|
216
217
218
|
},
currentValue () {
this.$emit('input', this.currentValue);
|
2810d8c7
梁灏
fixed #183
|
219
220
221
222
223
|
if (this.updatingValue) {
this.updatingValue = false;
return;
}
this.updateSelected(true);
|
48af1359
梁灏
fixed #553
|
224
225
226
|
},
data () {
this.$nextTick(() => this.updateSelected());
|
bd4e3b9b
梁灏
update Cascader
|
227
|
}
|
0a48ac45
梁灏
Input add readonl...
|
228
|
}
|
b0893113
jingsam
add eslint
|
229
230
|
};
</script>
|