-
Notifications
You must be signed in to change notification settings - Fork 1
/
AnList.vue
140 lines (133 loc) · 3.31 KB
/
AnList.vue
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
<template>
<ol v-if="listData.length" class="an-list">
<template v-if="!$store.state.printMode">
<li v-for="i in listData.length" :key="i">
<input
ref="listItems"
v-model="listData[i - 1]"
type="text"
@keydown.enter="enterHandler(i - 1)"
@input="inputHandler($event, i - 1)"
/>
</li>
</template>
<template v-else>
<li v-for="(item, index) in field_data" :key="index">
{{ item }}
</li>
</template>
</ol>
</template>
<script>
import field from '@/mixins/field.js';
export default {
name: 'AnList',
mixins: [field],
props: {
fieldMin: { type: Number, default: undefined },
fieldMax: { type: Number, default: undefined }
},
data: () => ({
listData: ['']
}),
watch: {
listData(newList) {
// add next empty entry
if (
newList.length > 0 &&
newList[newList.length - 1].length !== 0 &&
(!this.fieldMax || newList.length < this.fieldMax)
) {
newList.push('');
}
// remove empty entries at the end
if (
newList.length > 1 &&
newList[newList.length - 1].length === 0 &&
newList[newList.length - 2].length === 0
) {
newList.splice(newList.length - 1, 1);
}
this.field_data =
newList[newList.length - 1].length === 0
? newList.slice(0, -1)
: [...newList];
}
},
created() {
if (this.field_data?.length) this.listData = [...this.field_data];
},
methods: {
enterHandler(index) {
// set focus to next input
if (index < this.listData.length - 1) {
this.$refs.listItems[index + 1].focus();
}
// create new input if no next input
else if (
this.listData.length &&
this.listData[this.listData.length - 1].length > 0 &&
(!this.fieldMax || this.listData.length < this.fieldMax)
) {
this.listData.push('');
this.$nextTick(function () {
this.$refs.listItems[this.$refs.listItems.length - 1].focus();
});
}
},
inputHandler(event, index) {
if (this.listData[index].match(/^\s+/)) {
this.listData[index] = this.listData[index].replace(/^\s/, '');
this.$nextTick(() => {
event.target.setSelectionRange(0, 0); // prevent that cursor jumps to the end
});
}
},
validate(value) {
let valid = true;
if (
value === undefined ||
value.length === 0 ||
(this.fieldMax !== undefined && value?.length > this.fieldMax) ||
(this.fieldMin !== undefined && value?.length < this.fieldMin)
) {
valid = false;
}
this.$emit('update:field_valid', valid);
}
}
};
</script>
<style lang="scss" scoped>
.an-list__add {
margin-bottom: $spacer * 2;
font-size: 1.1rem;
}
ol {
@media screen {
list-style-position: inside;
}
}
li:not(:last-child) {
@media screen {
margin-bottom: $spacer * 2;
}
}
input {
border-radius: $border-radius;
padding: $spacer;
width: 100%;
max-width: 40ch;
border-width: 1px;
border-style: solid;
margin-left: 5px;
border-color: $color-theme-darkgrey;
.an-checkboxes_activefields &,
.an-field__subfields &,
.an-plain-data & {
@media screen {
border: 2px solid $color-theme-lightgrey;
}
}
}
</style>