optimize phone step

pull/381/head
Pete Matsyburka 1 year ago committed by Oleksandr Turchyn
parent e9172392b5
commit 1ad8d9196a

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -70,7 +70,7 @@
{{ selectedCountry.flag }} +{{ selectedCountry.dial }} {{ selectedCountry.flag }} +{{ selectedCountry.dial }}
</div> </div>
<select <select
class="absolute inset-0 opacity-0 w-full h-full cursor-pointer" class="absolute top-0 bottom-0 right-0 left-0 opacity-0 w-full h-full cursor-pointer"
:disabled="!!defaultValue" :disabled="!!defaultValue"
@change="onCountrySelect(countries.find((country) => country.flag === $event.target.value))" @change="onCountrySelect(countries.find((country) => country.flag === $event.target.value))"
> >
@ -83,6 +83,11 @@
</option> </option>
</select> </select>
</div> </div>
<input
:name="`values[${field.uuid}]`"
:value="fullInternationalPhoneValue"
hidden
>
<input <input
:id="field.uuid" :id="field.uuid"
ref="phone" ref="phone"
@ -94,7 +99,6 @@
inputmode="tel" inputmode="tel"
:required="field.required" :required="field.required"
placeholder="234 567-8900" placeholder="234 567-8900"
:name="`values[${field.uuid}]`"
@input="onPhoneInput" @input="onPhoneInput"
@focus="$emit('focus')" @focus="$emit('focus')"
> >
@ -105,7 +109,7 @@
<script> <script>
import MarkdownContent from './markdown_content' import MarkdownContent from './markdown_content'
import Geo from './geo' import phoneData from './phone_data'
function throttle (func, delay) { function throttle (func, delay) {
let lastCallTime = 0 let lastCallTime = 0
@ -167,33 +171,50 @@ export default {
isCodeSent: false, isCodeSent: false,
isResendLoading: false, isResendLoading: false,
phoneValue: this.modelValue || this.defaultValue || '', phoneValue: this.modelValue || this.defaultValue || '',
fullInternationalPhoneValue: '', selectedCountry: {}
selectedCountry: Geo.countryFlags['🇺🇸'],
countries: Geo.countries
} }
}, },
watch: { computed: {
fullInternationalPhoneValue (value) { countries () {
const digitCount = value.replace(/[^\d]/g, '').length return phoneData.map(([name, dial, flag, tz]) => {
return { name, dial, flag, tz }
})
},
countriesDialIndex () {
return this.countries.reduce((acc, item) => {
acc[item.dial] ||= item
return acc
}, {})
},
dialCodesRegexp () {
const dialCodes = this.countries.map((country) => country.dial).sort((a, b) => b.length - a.length)
if (!value.match(/^\+[0-9\s\\-]+$/) || digitCount < 8 || digitCount > 15) { return new RegExp(`^\\+(${dialCodes.join('|')})`)
this.$refs.phone.setCustomValidity(this.t('use_international_format')) },
detectedPhoneValueDialCode () {
return (this.phoneValue || '').replace(/[^\d+]/g, '').match(this.dialCodesRegexp)?.[1]
},
fullInternationalPhoneValue () {
if (this.detectedPhoneValueDialCode) {
return this.phoneValue
} else if (this.phoneValue) {
return ['+', this.selectedCountry.dial, this.phoneValue].filter(Boolean).join('')
} else { } else {
this.$refs.phone.setCustomValidity('') return ''
} }
} }
}, },
mounted () { mounted () {
const detectedDialCode = this.detectDialeCode(this.phoneValue) const browserTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone
const brawserTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone
if (detectedDialCode) { if (this.detectedPhoneValueDialCode) {
this.selectedCountry = Geo.countries.find((country) => country.dial === detectedDialCode) this.selectedCountry = this.countriesDialIndex[this.detectedPhoneValueDialCode]
} else if (brawserTimeZone) { } else if (browserTimeZone) {
this.selectedCountry = Geo.countries.find((country) => country.tz.includes(brawserTimeZone)) || Geo.countryFlags['🇺🇸'] const tz = browserTimeZone.split('/')[1]
}
this.updateFullInternationalPhoneValue(this.phoneValue) this.selectedCountry = this.countries.find((country) => country.tz.includes(tz)) || this.countries[0]
}
}, },
methods: { methods: {
emitSubmit: throttle(function (e) { emitSubmit: throttle(function (e) {
@ -206,32 +227,14 @@ export default {
this.selectedCountry = country this.selectedCountry = country
this.updateFullInternationalPhoneValue(this.phoneValue)
this.$refs.phone.focus() this.$refs.phone.focus()
}, },
onPhoneInput (e) { onPhoneInput (e) {
this.phoneValue = e.target.value this.phoneValue = e.target.value
this.updateFullInternationalPhoneValue(this.phoneValue) if (this.detectedPhoneValueDialCode) {
}, this.selectedCountry = this.countriesDialIndex[this.detectedPhoneValueDialCode]
detectDialeCode (value) {
const dialCodes = Geo.countries.map((country) => country.dial).sort((a, b) => b.length - a.length)
return (value || '').replace(/[^\d+]/g, '').match(new RegExp(`^\\+(${dialCodes.join('|')})`))?.[1]
},
updateFullInternationalPhoneValue (value) {
const detectedDialCode = this.detectDialeCode(value)
if (detectedDialCode) {
this.selectedCountry = Geo.countries.find((country) => country.dial === detectedDialCode)
this.fullInternationalPhoneValue = this.phoneValue
} else if (this.phoneValue) {
this.fullInternationalPhoneValue = ['+', this.selectedCountry.dial, this.phoneValue].filter(Boolean).join('')
} else {
this.fullInternationalPhoneValue = ''
} }
this.$emit('update:model-value', this.fullInternationalPhoneValue)
}, },
onInputCode (e) { onInputCode (e) {
if (e.target.value.length === 6) { if (e.target.value.length === 6) {

@ -11,6 +11,7 @@ const configs = generateWebpackConfig({
}, },
optimization: { optimization: {
runtimeChunk: false, runtimeChunk: false,
concatenateModules: !process.env.BUNDLE_ANALYZE,
splitChunks: { splitChunks: {
chunks (chunk) { chunks (chunk) {
return chunk.name !== 'rollbar' return chunk.name !== 'rollbar'

Loading…
Cancel
Save