forked from EnergySage/es-ds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEsFormInput.vue
166 lines (164 loc) · 4.33 KB
/
EsFormInput.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
<template>
<div
class="input-wrapper justify-content-end"
:required="required">
<!-- eslint-disable-next-line vuejs-accessibility/label-has-for -->
<label
:for="id"
class="label justify-content-start"
:class="{ 'sr-only': labelSrOnly }">
<slot name="label" />
<span
v-if="required"
class="text-danger">
*
</span>
</label>
<p
v-if="hasExtraContext"
class="mb-25 font-size-75">
<slot name="extraContext" />
</p>
<div class="input-holder position-relative">
<!-- prefix icons are decorative only so are aria-hidden -->
<span
v-if="$slots.prefixIcon"
aria-hidden="true"
class="prefix-icon position-absolute">
<slot name="prefixIcon" />
</span>
<b-form-input
:id="id"
v-bind="$attrs"
v-mask="type === 'maskedTel' ? phoneMaskValue : null"
:type="type === 'maskedTel' ? 'tel' : type"
class="es-form-input w-100"
:class="{ 'has-prefix-icon': $slots.prefixIcon }"
:disabled="disabled"
:state="state"
v-on="$listeners" />
<b-form-text v-if="hasMessage && ((!hasSuccess && state) || state == null)">
<slot name="message" />
</b-form-text>
<b-form-invalid-feedback v-if="hasError || required">
<slot
v-if="hasError"
name="errorMessage" />
<template v-else-if="required">
This field is required.
</template>
</b-form-invalid-feedback>
<b-form-valid-feedback v-if="hasSuccess">
<slot name="successMessage" />
</b-form-valid-feedback>
</div>
</div>
</template>
<script lang="js">
import { mask } from 'vue-the-mask';
import {
BFormText,
BFormInput,
BFormInvalidFeedback,
BFormValidFeedback,
} from 'bootstrap-vue';
export default {
name: 'EsFormInput',
components: {
BFormText,
BFormInput,
BFormInvalidFeedback,
BFormValidFeedback,
},
directives: {
mask: {
bind(el, binding, vnode) {
if (binding.value && mask) {
mask(el, binding, vnode); // Apply the mask if a valid mask pattern is provided
}
},
},
},
// Prevents attributes from being applied to first <div>
// v-bind="$attr" is on the input instead
inheritAttrs: false,
props: {
/**
* Required
*/
required: {
type: Boolean,
default: false,
},
/**
* Disabled
*/
disabled: {
type: Boolean,
default: false,
},
/**
* Type
*/
type: {
type: String,
default: 'text',
validator: (val) => [
'text',
'number',
'email',
'number',
'password',
'tel',
'maskedTel',
].includes(val),
},
/**
* ID
* required
*/
id: {
type: String,
required: true,
},
/**
* state
*/
state: {
type: [Boolean, null],
default: null,
},
/**
* inline
*/
inline: {
type: Boolean,
default: false,
required: false,
},
labelSrOnly: {
type: Boolean,
default: false,
},
phoneMaskValue: {
type: String,
default: '(###) ###-####',
required: false,
},
},
computed: {
hasSuccess() {
return !!this.$slots.successMessage;
},
hasMessage() {
return !!this.$slots.message;
},
hasError() {
return !!this.$slots.errorMessage;
},
hasExtraContext() {
return !!this.$slots.extraContext;
},
},
};
</script>