forked from openWB/openwb-ui-settings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenwbBaseNumberInput.vue
126 lines (120 loc) · 2.83 KB
/
OpenwbBaseNumberInput.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
<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', 'calculator']"
/>
</div>
</div>
<input
type="number"
class="form-control"
v-model.number="value"
v-bind="$attrs"
/>
<div v-if="unit" class="input-group-append">
<div class="input-group-text">
{{ unit }}
</div>
</div>
</div>
</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,
faCalculator as fasCalculator,
} 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, fasCalculator);
export default {
name: "OpenwbNumberInput",
inheritAttrs: false,
props: {
title: String,
modelValue: { type: Number },
unit: String,
precision: { type: Number, default: undefined },
emptyValue: { required: false, default: null },
},
emits: ["update:modelValue"],
data() {
return {
showHelp: false,
};
},
computed: {
value: {
get() {
if (this.precision !== undefined) {
return parseFloat(
Math.round(
this.modelValue * Math.pow(10, this.precision),
) / Math.pow(10, this.precision),
);
}
return this.modelValue;
},
set(newValue) {
if (isNaN(newValue) || typeof newValue != "number") {
newValue = this.emptyValue;
}
this.$emit("update:modelValue", newValue);
},
},
},
methods: {
toggleHelp() {
this.showHelp = !this.showHelp && this.$slots.help !== undefined;
},
},
components: {
FontAwesomeIcon,
},
};
</script>
<style scoped>
input.invalid,
input:invalid {
background-color: pink;
}
/* hide spinner buttons if input element is readonly or disabled */
/* Chrome, Safari, Edge, Opera */
input[readonly]::-webkit-outer-spin-button,
input[readonly]::-webkit-inner-spin-button,
input[disabled]::-webkit-outer-spin-button,
input[disabled]::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
/* Firefox */
input[readonly][type="number"],
input[disabled][type="number"] {
appearance: textfield;
-moz-appearance: textfield;
}
</style>