forked from openWB/openwb-ui-settings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenwbBaseArrayInput.vue
205 lines (195 loc) · 3.83 KB
/
OpenwbBaseArrayInput.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
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
<template>
<div class="form-row mb-1">
<label v-on:click="toggleHelp" class="col-md-4 col-form-label">
{{ title }}
<font-awesome-icon
v-if="$slots.help"
:icon="
showHelp
? ['fas', 'question-circle']
: ['far', 'question-circle']
"
:class="showHelp ? 'text-info' : ''"
/>
</label>
<div class="col-md-8">
<div class="form-row">
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">
<font-awesome-icon
fixed-width
:icon="['fas', 'tag']"
/>
</div>
</div>
<input
ref="tagInput"
type="text"
class="form-control"
v-model="newTag"
v-bind="$attrs"
@keyup.enter="addTag"
/>
<div class="input-group-append">
<div
class="input-group-text"
:class="
newTagValid
? 'bg-success clickable'
: 'notClickable'
"
@click="addTag"
>
<font-awesome-icon
fixed-width
:icon="['fas', 'plus']"
/>
</div>
</div>
</div>
</div>
<div class="form-row tagList mt-1">
<span v-if="value.length == 0" class="noTag">
<font-awesome-icon :icon="['fas', 'info-circle']" />
{{ noElementsMessage }}
</span>
<span
class="tag"
v-for="(tag, index) in value"
v-bind:key="index"
>
<font-awesome-icon :icon="['fas', 'tag']" />
{{ tag }}
<font-awesome-icon
class="clickable"
:icon="['fas', 'times-circle']"
@click="removeTag(index)"
/>
</span>
</div>
<span v-if="showHelp" class="form-row alert alert-info my-1 small">
<slot name="help"></slot>
</span>
</div>
</div>
</template>
<script>
import { library } from "@fortawesome/fontawesome-svg-core";
import {
faQuestionCircle as fasQuestionCircle,
faTag as fasTag,
faTimesCircle as fasTimesCircle,
faPlus as fasPlus,
faInfoCircle as fasInfoCircle,
} from "@fortawesome/free-solid-svg-icons";
import { faQuestionCircle as farQuestionCircle } from "@fortawesome/free-regular-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
library.add(
fasQuestionCircle,
farQuestionCircle,
fasTag,
fasTimesCircle,
fasPlus,
fasInfoCircle,
);
export default {
name: "OpenwbArrayInput",
inheritAttrs: false,
props: {
title: String,
modelValue: {
type: Array,
default: () => {
return [];
},
},
noElementsMessage: {
type: String,
default: () => {
return "Keine Elemente zugeordnet.";
},
},
},
emits: ["update:modelValue"],
data() {
return {
newTag: "",
showHelp: false,
};
},
computed: {
value: {
get() {
return this.modelValue;
},
set(newValue) {
this.$emit("update:modelValue", newValue);
},
},
newTagValid: {
get() {
return (
this.newTag.length > 0 &&
this.value.indexOf(this.newTag) == -1
);
},
},
},
methods: {
toggleHelp() {
this.showHelp = !this.showHelp && this.$slots.help !== undefined;
},
addTag() {
if (this.newTagValid) {
let tempValue = this.value;
tempValue.push(this.newTag);
tempValue.sort();
this.value = tempValue;
this.newTag = "";
}
this.$refs.tagInput.focus();
},
removeTag(index) {
let tempValue = this.value;
tempValue.splice(index, 1);
this.value = tempValue;
},
},
components: {
FontAwesomeIcon,
},
};
</script>
<style scoped>
.clickable {
cursor: pointer;
}
.notClickable {
cursor: not-allowed;
}
input.invalid,
input:invalid {
border: 2px solid var(--danger);
}
.tagList {
background-color: #e9ecef;
border: 1px solid #ced4da;
border-radius: 0.25rem;
padding: 5px 5px 0 5px;
}
.tag,
.noTag {
border-radius: 10px;
padding: 2px 5px;
margin-right: 10px;
margin-bottom: 5px;
}
.noTag {
color: var(--gray);
font-style: italic;
}
.tag {
background-color: var(--success);
}
</style>