forked from EnergySage/es-ds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEsZipCodeForm.vue
227 lines (223 loc) · 6.49 KB
/
EsZipCodeForm.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<template>
<div
class="EsZipCodeForm d-flex"
:class="{
'EsZipCodeForm--constrained': constrained,
'text-white': dark,
}"
v-bind="$attrs"
v-on="$listeners">
<div class="d-flex flex-column">
<b-form
ref="ctaForm"
class="justify-content-center w-100"
:class="{
invalid: $v.$dirty && $v.$invalid,
[`d-${stackBreak}flex`]: stackUntil,
'mb-100': showPrivacySection,
}"
:action="url"
method="get"
novalidate
:target="newTab ? '_blank' : '_self'"
@submit.prevent.stop="handleSubmit">
<es-form-input
:id="inputId"
v-model="zipCode"
autocomplete="postal-code"
class="mb-100"
:class="{
[`mb-${stackBreak}0 mr-${stackBreak}50`]: stackUntil,
}"
inputmode="numeric"
label-sr-only
maxlength="5"
:name="fieldName"
pattern="\d*"
:placeholder="placeholder"
required
:state="validateState('zipCode')">
<template #prefixIcon>
<icon-location class="text-gray-800" />
</template>
<template #label>
{{ placeholder }}
</template>
<template #errorMessage>
<span :class="{ 'text-white': dark }">
<slot name="errorMessage"> Please enter a 5-digit zip code. </slot>
</span>
</template>
</es-form-input>
<input
v-if="selectedProduct"
type="hidden"
name="product"
:value="selectedProduct">
<es-button
class="text-nowrap w-100"
:class="{
[`ml-${stackBreak}50 w-${stackBreak}auto`]: stackUntil,
'px-100': constrained,
}"
type="submit"
:variant="dark ? 'dark-bg' : 'primary'">
<slot name="buttonText">
Submit
</slot>
</es-button>
</b-form>
<div
v-if="showPrivacySection"
class="d-flex text-left"
:class="{ 'font-size-75': constrained }">
<icon-lock-on
class="privacy-lock-icon flex-shrink-0 mr-50 position-relative"
:class="{ 'mt-25': !constrained }"
height="1.125rem"
width="1.125rem" />
<div>
<span>
<slot name="privacyExplanation"> Your information is safe with us. </slot>
</span>
<b-link
v-if="privacyPolicyLink"
:href="privacyPolicyLink"
class="text-nowrap"
:class="dark ? 'text-white' : ''"
:target="privacyPolicyNewTab ? '_blank' : '_self'">
<slot name="privacyPolicyLinkText">
Privacy Policy
</slot>
</b-link>
</div>
</div>
</div>
</div>
</template>
<script lang="js">
import { formMixins } from '../lib-mixins';
import {
vuelidateMinLength,
vuelidateMaxLength,
vuelidateNumeric,
vuelidateRequired,
} from '../lib-validators';
import EsButton from './EsButton.vue';
import EsFormInput from './EsFormInput.vue';
import IconLocation from '../lib-icons/icon-location.vue';
import IconLockOn from '../lib-icons/icon-lock-on.vue';
export default {
name: 'EsZipCodeForm',
components: {
EsButton,
EsFormInput,
IconLocation,
IconLockOn,
},
mixins: [formMixins],
props: {
constrained: {
type: Boolean,
default: false,
},
dark: {
type: Boolean,
default: false,
},
fieldName: {
type: String,
default: 'zip_code',
},
inputId: {
type: String,
required: true,
},
newTab: {
type: Boolean,
default: false,
},
placeholder: {
type: String,
default: 'ZIP code',
},
showPrivacySection: {
type: Boolean,
default: true,
},
privacyPolicyLink: {
type: String,
default: '',
},
privacyPolicyNewTab: {
type: Boolean,
default: false,
},
stackUntil: {
type: String,
default: '',
},
url: {
type: String,
required: true,
},
zipCodeValue: {
type: String,
default: '',
},
selectedProduct: {
type: String,
default: '',
},
},
data() {
return {
zipCode: this.zipCodeValue,
};
},
computed: {
stackBreak() {
let { stackUntil } = this;
if (stackUntil === 'xs') {
stackUntil = '';
}
return stackUntil ? `${stackUntil}-` : '';
},
},
watch: {
zipCodeValue(newVal) {
this.zipCode = newVal;
},
},
methods: {
handleSubmit() {
if (this?.$v?.$invalid) {
// if form is invalid, touch to display errors and get out
this.$v.$touch();
} else {
this.$refs.ctaForm.submit();
}
},
},
validations: {
zipCode: {
maxLength: vuelidateMaxLength(5),
minLength: vuelidateMinLength(5),
numeric: vuelidateNumeric,
required: vuelidateRequired,
},
},
};
</script>
<style lang="scss" scoped>
.EsZipCodeForm {
.privacy-lock-icon {
top: -0.02em;
}
&--constrained {
.privacy-lock-icon {
top: 0.02em;
}
}
}
</style>