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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<template>
<div :class="{'app-array-box': true, 'is-disabled': disabled || readonly}">
<template v-if="items.length == 0">
<Icon class="app-array-box__icon" type="md-add" @click="addItem()" />
</template>
<template v-else>
<div v-for="(item, index) in items" :key="item.key" class="app-array-box__item">
<input-box
v-if="Object.is(editorType, 'default')"
:type="type"
:size="size"
:value="item.value"
:placeholder="placeholder"
:class="{'app-inupt-box--correct': item.correct}"
:disabled="disabled || readonly"
@enter="enter"
@blur="blur"
@focus="focus"
@change="(value) => handleChange(value, index)" />
<Tooltip
v-else
:disabled="!item.value || disabled || readonly"
placement="bottom"
trigger="hover">
<input-box
type="text"
:size="size"
:value="item.value"
:placeholder="placeholder"
:disabled="disabled || readonly"
:class="{'app-inupt-box--correct': item.correct}"
:prepend="Object.is(editorType, 'url') ? prepend : null"
:append="Object.is(editorType, 'url') ? append : null"
:maxlength="Object.is(editorType, 'url') ? maxlength : null"
:showWordLimit="Object.is(editorType, 'url') ? showWordLimit : null"
@enter="enter"
@blur="blur"
@focus="focus"
@change="(value) => handleChange(value, index)" />
<template #content>
<el-image v-if="Object.is(editorType, 'img')" :src="item.value">
<div slot='error' class='image-slot'>
<img src="/assets/img/picture.png">
</div>
</el-image>
<el-link v-else type="primary" :href="getUrl(item.value)" :target="target">{{getUrl(item.value)}}</el-link>
</template>
</Tooltip>
<div v-if="!(disabled || readonly)" class="app-array-box__icons">
<Icon v-if="!limit || items.length < limit" class="app-array-box__icon" type="md-add" @click="addItem(index + 1)" />
<Icon class="app-array-box__icon" type="md-remove" @click="removeItem(index)" />
</div>
</div>
</template>
</div>
</template>
<script lang="ts">
import schema from 'async-validator';
import { Vue, Component, Prop, Watch, Emit } from "vue-property-decorator";
@Component({})
export default class AppArrayBox extends Vue {
/**
* 名称
*
* @type {string}
* @memberof AppArrayBox
*/
@Prop() public name!: string;
/**
* 传入值
*
* @type {string[]}
* @memberof AppArrayBox
*/
@Prop() public value?: any[];
/**
* 编辑器类型
*
* @type {'default' | 'img' | 'url'}
* @memberof AppArrayBox
*/
@Prop({ default: 'default' }) public editorType!: 'default' | 'img' | 'url';
/**
* 输入框类型
*
* @type {'text' | 'number'}
* @memberof AppArrayBox
*/
@Prop({ default: 'text' }) type!: 'text' | 'number';
/**
* placeholder值
*
* @type {String}
* @memberof AppArrayBox
*/
@Prop() public placeholder?: string;
/**
* 是否禁用
*
* @type {boolean}
* @memberof AppArrayBox
*/
@Prop() public disabled?: boolean;
/**
* 是否只读
*
* @type {boolean}
* @memberof AppArrayBox
*/
@Prop() public readonly?: boolean;
/**
* 大小
*
* @type {string}
* @memberof AppArrayBox
*/
@Prop() public size?: string;
/**
* 数组大小限制
*
* @type {string}
* @memberof AppArrayBox
*/
@Prop({ default: 0 }) public limit?: number;
/**
* 输入内容最长长度
*
* @type {number}
* @memberof AppArrayBox
*/
@Prop() public maxlength?: number;
/**
* 是否显示输入内容长度
*
* @type {boolean}
* @memberof AppArrayBox
*/
@Prop() public showWordLimit?: boolean;
/**
* 输入内容前缀
*
* @type {string}
* @memberof AppArrayBox
*/
@Prop() public prepend?: any;
/**
* 输入内容后缀
*
* @type {string}
* @memberof AppArrayBox
*/
@Prop() public append?: any;
/**
* 超链接打开方式
*
* @type {'_blank' | '_self' | '_parent' | '_top' | 'framename'}
* @memberof AppArrayBox
*/
@Prop({ default: '_blank' }) public target!: '_blank' | '_self' | '_parent' | '_top' | 'framename';
/**
* 值规则
*
* @type {any[]}
* @memberof AppArrayBox
*/
@Prop() public rules?: any[];
/**
* 输入内容项集合
*
* @type {Array}
* @memberof AppArrayBox
*/
public items: any[] = [];
@Watch('value')
onValueChange(newVal: string[] | number[]) {
if (newVal) {
if (this.items.length == 0) {
const items = newVal.map((value: any) => {return {value, key: this.$util.createUUID()}});
if (Object.is(this.editorType, 'url') && items.length > 0) {
items.forEach((item: any) => {
if (this.prepend && item.value.startsWith(this.prepend)) {
item.value.replace(this.prepend, '');
}
if (this.append && item.value.endsWith(this.append)) {
item.value.replace(this.append, '');
}
});
}
this.items = items;
}
}
}
/**
* 校验值
*
* @param value 值
* @memberof AppArrayBox
*/
public async validate(value: string): Promise<boolean> {
if (this.rules && this.rules.length > 0) {
const validator = new schema({ [this.name]: this.rules });
try {
await validator.validate({ [this.name]: value });
return true;
} catch (error) {
return false
}
}
return true;
}
/**
* 获取Url路径
*
* @param value url值
* @memberof AppArrayBox
*/
public getUrl(value: string): string {
let tempValue = value;
if (tempValue) {
if (this.prepend) {
tempValue = this.prepend + tempValue;
}
if (this.append) {
tempValue = tempValue + this.append;
}
}
return tempValue;
}
/**
* 新增项
*
* @param index 添加索引
* @memberof AppArrayBox
*/
public addItem(index?: number) {
if (this.disabled || this.readonly) {
return;
}
const tempLink = {
key: this.$util.createUUID(),
value: ""
}
if (index) {
this.items.splice(index, 0, tempLink);
} else {
this.items.push(tempLink);
}
this.onEmit();
}
/**
* 删除项
*
* @type {string}
* @memberof AppArrayBox
*/
public removeItem(index: number) {
this.items.splice(index, 1);
this.onEmit();
}
/**
* 处理值改变
*
* @type {string}
* @memberof AppArrayBox
*/
public handleChange(value: string | number, index: number) {
let tempValue = value;
if (Object.is(this.editorType, 'url') && tempValue) {
if (this.prepend) {
tempValue = this.prepend + tempValue;
}
if (this.append) {
tempValue = tempValue + this.append;
}
}
this.items[index].value = value;
this.onEmit();
}
/**
* 抛值
*
* @memberof AppArrayBox
*/
public async onEmit() {
const result = this.items.map((item:any)=>item.value);
this.$emit('change', result);
for (let index = 0; index < this.items.length; index++) {
this.items[index].correct = await this.validate(this.items[index].value);
}
this.$forceUpdate();
}
/**
* 回车事件
*
* @param {*} $event
* @memberof InputBox
*/
@Emit()
public enter($event: any) {
if (!$event || $event.keyCode !== 13) {
return;
}
const result = this.items.map((item:any)=>item.value);
return result;
}
/**
* 失去焦点事件
*
* @param {*} $event
* @memberof InputBox
*/
@Emit()
public blur(event: any) {
const result = this.items.map((item:any)=>item.value);
return result;
}
/**
* 失去焦点事件
*
* @param {*} $event
* @memberof InputBox
*/
@Emit()
public focus(event: any) {
const result = this.items.map((item:any)=>item.value);
return result;
}
}
</script>
<style lang='less'>
@import './app-array-box.less';
</style>