-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathInput.vue
More file actions
69 lines (67 loc) · 1.8 KB
/
Input.vue
File metadata and controls
69 lines (67 loc) · 1.8 KB
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
<script>
import InputDescription from './InputDescription.vue';
import InputLabel from './InputLabel.vue';
import validator from '../../utils/validator';
export default {
components: { InputLabel, InputDescription },
props: {
schema: {
type: Object,
required: true,
},
currentValue: true,
},
data() {
const initialValue = typeof this.currentValue !== 'undefined' ? this.currentValue : null;
return {
value: typeof initialValue === 'object' ? JSON.parse(JSON.stringify(initialValue)) : initialValue,
showDescription: false,
};
},
computed: {
label() {
return this.schema.label || this.schema.param || this.schema.paramName;
},
field() {
return this.schema.paramName;
},
placeholder() {
return this.schema.placeholder;
},
description() {
return this.schema.description;
},
hasDescription() {
return !!this.description;
},
isValid() {
return !this.hasErrors;
},
hasErrors() {
return this.errors.length;
},
errors() {
if (Object.prototype.hasOwnProperty.call(validator, this.schema.type)) return validator[this.schema.type](this.value, this.schema);
return [];
},
errorText() {
return this.errors.map(error => `Value is ${error}!`).join(' ');
},
},
watch: {
value: {
handler: 'update',
deep: true,
},
},
methods: {
update() {
const value = typeof this.value === 'object' ? JSON.parse(JSON.stringify(this.value)) : this.value;
this.$emit('update', value, this.field);
},
toggleDescription() {
this.showDescription = !this.showDescription;
},
},
};
</script>