forked from Happy-Coding-Clans/vue-easytable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.jsx
193 lines (175 loc) · 4.92 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import VeDropdown from "vue-easytable/packages/ve-dropdown";
import { COMPS_NAME, EMIT_EVENTS } from "./util/constant";
import { clsName } from "./util/index";
import VeIcon from "vue-easytable/packages/ve-icon";
import { ICON_NAMES } from "../../src/utils/constant";
export default {
name: COMPS_NAME.VE_SELECT,
props: {
width: {
type: Number,
default: 90,
},
// select的最大宽度(超出隐藏)
maxWidth: {
type: Number,
default: 0,
},
// 如果为true 会包含 checkbox
isMultiple: {
type: Boolean,
default: false,
},
// 用户传入v-model 的值 [{value/label/selected}]
value: {
type: Array,
default: null,
},
// 占位符
placeholder: {
type: String,
default: "请选择",
validator: function (value) {
return value.length > 0;
},
},
// 文本居中方式 left|center|right
textAlign: {
type: String,
default: "left",
},
// 是否支持输入input
isInput: {
type: Boolean,
default: false,
},
// popper append to element
popperAppendTo: {
type: [String, HTMLElement],
default: function () {
return document.body;
},
},
},
data() {
return {
visible: false,
internalOptions: [],
inputValue: "",
// dorpdown visible
dropdownVisible: false,
};
},
computed: {
// icon class
iconClass() {
return {
[clsName("show")]: this.dropdownVisible,
[clsName("toggle-icon")]: true,
};
},
},
watch: {
value: function () {
this.init();
},
},
methods: {
// 初始化
init() {
this.internalOptions = Object.assign([], this.value);
},
// 显示选中的信息
showSelectInfo() {
var result, labels;
labels = this.selectedLabels();
if (Array.isArray(labels) && labels.length > 0) {
result = labels.join();
} else {
result = this.placeholder;
}
return result;
},
// 当前选中项的label
selectedLabels() {
return this.internalOptions
.filter((x) => x.selected)
.map((x) => {
if (x.selected) {
return x.label;
}
});
},
// dropdown change event
dropdownChange() {
// 使用户传入的v-model 生效
this.$emit("input", this.internalOptions);
this.$emit(EMIT_EVENTS.SELECT_CHANGE, this.internalOptions);
},
},
created() {
this.init();
},
render() {
const { isInput } = this;
const props = {
class: "ve-select",
props: {
isSelect: true,
width: this.width,
maxWidth: this.maxWidth,
isMultiple: this.isMultiple,
textAlign: this.textAlign,
isInput: this.isInput,
// v-model
value: this.internalOptions,
hideByItemClick: true,
popperAppendTo: this.popperAppendTo,
},
style: {
width: this.width,
},
on: {
//change: this.dropdownChange,
// v-model
input: (val) => {
this.internalOptions = val;
this.dropdownChange();
},
// dropdown visible change
"dropdown-visible-change": (visible) => {
this.dropdownVisible = visible;
},
},
};
let content = "";
if (isInput) {
content = (
<input
class={clsName("input")}
placeholder={this.placeholder}
type="text"
v-model={this.inputValue}
/>
);
} else {
content = (
<span class={clsName("selected-span")}>
{this.showSelectInfo()}
</span>
);
}
return (
<VeDropdown {...props}>
<span>
{content}
<VeIcon
name={ICON_NAMES.BOTTOM_ARROW}
class={this.iconClass}
/>
{/* <i class={[this.iconClass]}></i> */}
</span>
</VeDropdown>
);
},
};