Skip to content

Beautiful Open-Source Form Builder

Fix phoneinput width (#905)

Julien Nahum891b4e91ea22
diff --git a/client/components/forms/core/components/VSelect.vue b/client/components/forms/core/components/VSelect.vue
index 77965cd3b..e163b5531 100644
--- a/client/components/forms/core/components/VSelect.vue
+++ b/client/components/forms/core/components/VSelect.vue
@@ -11,7 +11,7 @@
sideOffset: 4
}"
:ui="{
- content: (popoverWidth ? `w-[${popoverWidth}] ` : 'w-(--reka-popper-anchor-width) ') + 'bg-white dark:!bg-notion-dark-light shadow-xl z-30 overflow-auto ' + borderRadiusClass
+ content: 'bg-white dark:!bg-notion-dark-light shadow-xl z-30 overflow-auto ' + borderRadiusClass
}"
>
<template #anchor>
@@ -102,6 +102,8 @@
role="listbox"
ref="scrollRef"
:class="variantSlots.dropdown()"
+ class="w-(--reka-popper-anchor-width)"
+ :style="popoverContentStyle"
>
<div
v-if="isSearchable"
@@ -287,7 +289,8 @@ export default {
fuseOptions: { type: Object, default: () => ({}) },
searchDebounceMs: { type: Number, default: 150 },
minSearchLength: { type: Number, default: 1 },
- popoverWidth: { type: String, default: null }
+ // Explicit popover width control. Accepts number (px) or CSS length string.
+ popoverWidth: { type: [String, Number], default: null }
},
emits: ['update:modelValue', 'update-options', 'focus', 'blur'],
data () {
@@ -349,6 +352,12 @@ export default {
'--tw-ring-color': this.color
}
},
+ popoverContentStyle () {
+ const widthValue = this.popoverWidth !== null && this.popoverWidth !== undefined
+ ? (typeof this.popoverWidth === 'number' ? `${this.popoverWidth}px` : String(this.popoverWidth))
+ : 'var(--reka-popper-anchor-width)'
+ return { width: widthValue }
+ },
debouncedRemote () {
if (this.remote) {
return debounce(this.remote, 300)
@@ -451,6 +460,13 @@ export default {
this.setupVirtualizer()
})
}
+ },
+ popoverWidth () {
+ if (this.isOpen) {
+ this.$nextTick(() => {
+ this.setupVirtualizer()
+ })
+ }
}
},
mounted () {
@@ -617,4 +633,4 @@ export default {
}
}
}
-</script>
\ No newline at end of file
+</script>
diff --git a/client/components/forms/heavy/PhoneInput.vue b/client/components/forms/heavy/PhoneInput.vue
index f25dd30d5..3d2b989cc 100644
--- a/client/components/forms/heavy/PhoneInput.vue
+++ b/client/components/forms/heavy/PhoneInput.vue
@@ -7,6 +7,7 @@
</template>
<div
+ ref="phoneInputContainer"
:id="id ? id : name"
:name="name"
:style="inputStyle"
@@ -15,7 +16,6 @@
<v-select
class="min-w-0"
v-model="selectedCountryCode"
- popover-width="full"
input-class="ltr-only:rounded-r-none rtl:rounded-l-none!"
:data="countries"
:disabled="disabled || countries.length===1"
@@ -27,6 +27,7 @@
:theme="resolvedTheme"
:size="resolvedSize"
:border-radius="resolvedBorderRadius"
+ :popover-width="width"
:ui="{ container: ui.countrySelectWidth() }"
:placeholder="'Select a country'"
:uppercase-labels="true"
@@ -39,16 +40,16 @@
class="-mt-[9px]! rounded"
:country="props.option.code"
/>
- <span class="truncate">{{ props.option.name }}</span>
- <span class="text-gray-500 whitespace-nowrap">{{ props.option.dial_code && props.option.dial_code.startsWith('+') ? props.option.dial_code : '+' + props.option.dial_code }}</span>
+ <span class="truncate" :class="ui.option()">{{ props.option.name }}</span>
+ <span class="text-gray-500 whitespace-nowrap" :class="ui.option()">{{ props.option.dial_code && props.option.dial_code.startsWith('+') ? props.option.dial_code : '+' + props.option.dial_code }}</span>
</div>
</template>
<template #selected="props">
<div
- class="flex items-center gap-2 w-full overflow-hidden ltr-only:pr-8 rtl-only:pl-8"
- :class="ui.selectedMaxHeight()"
+ class="flex items-center gap-2 w-full overflow-hidden ltr-only:pr-1 rtl-only:pl-1"
+ :class="[ui.selectedMaxHeight(), ui.selected()]"
>
- <span class="text-sm whitespace-nowrap shrink-0">{{ props.option.dial_code && props.option.dial_code.startsWith('+') ? props.option.dial_code : '+' + props.option.dial_code }}</span>
+ <span class="whitespace-nowrap shrink-0" :class="ui.selected()">{{ props.option.dial_code && props.option.dial_code.startsWith('+') ? props.option.dial_code : '+' + props.option.dial_code }}</span>
<country-flag
:size="countryFlagSize"
class="rounded-lg! ms-auto shrink-0"
@@ -86,126 +87,134 @@
</input-wrapper>
</template>
-<script>
+<script setup>
import { inputProps, useFormInput } from '../useFormInput.js'
import countryCodes from '~/data/country_codes.json'
import CountryFlag from 'vue-country-flag-next'
import parsePhoneNumber from 'libphonenumber-js'
import { phoneInputTheme } from '~/lib/forms/themes/phone-input.theme.js'
+import { useElementSize } from '@vueuse/core'
-export default {
- phone: 'PhoneInput',
- components: { CountryFlag },
- props: {
- ...inputProps,
- canOnlyCountry: { type: Boolean, default: false },
- unavailableCountries: { type: Array, default: () => [] }
- },
-
- setup (props, context) {
- const formInput = useFormInput(props, context, {
- variants: phoneInputTheme
- })
- const countryFlagSize = computed(() => {
- const size = formInput.resolvedSize.value
- return (size === 'xs' || size === 'sm') ? 'small' : 'normal'
- })
- return {
- ...formInput,
- countryFlagSize
- }
- },
+// Emits
+const emit = defineEmits(['update:modelValue', 'focus', 'blur'])
- data () {
- return {
- selectedCountryCode: null,
- inputVal: null
- }
- },
+// Props
+const props = defineProps({
+ ...inputProps,
+ canOnlyCountry: { type: Boolean, default: false },
+ unavailableCountries: { type: Array, default: () => [] }
+})
- computed: {
- countries () {
- return countryCodes.filter((item) => {
- return !this.unavailableCountries.includes(item.code)
- })
- }
- },
-
- watch: {
- inputVal: {
- handler(val) {
- if (!this.selectedCountryCode) return
-
- if (val && val.startsWith('0')) {
- val = val.substring(1)
- }
- if (this.canOnlyCountry) {
- this.compVal = (val) ? this.selectedCountryCode.code + this.selectedCountryCode.dial_code + val : this.selectedCountryCode.code + this.selectedCountryCode.dial_code
- } else {
- this.compVal = (val) ? this.selectedCountryCode.code + this.selectedCountryCode.dial_code + val : null
- }
- }
- },
- compVal () {
- this.initState()
- },
- selectedCountryCode (newVal, oldVal) {
- if (this.compVal && newVal && oldVal) {
- this.compVal = this.compVal.replace(oldVal.code + oldVal.dial_code, newVal.code + newVal.dial_code)
- }
- }
- },
+// Composables
+const { compVal, resolvedSize, inputStyle, inputWrapperProps, ui } = useFormInput(props, { emit }, {
+ variants: phoneInputTheme
+})
- mounted () {
- if (this.compVal) {
- this.initState()
- }
- if (!this.selectedCountryCode) {
- this.selectedCountryCode = this.getCountryBy()
- }
- if (!this.selectedCountryCode || this.countries.length === 1) {
- this.selectedCountryCode = this.countries[0]
+// Reactive data
+const selectedCountryCode = ref(null)
+const inputVal = ref(null)
+
+// Computed properties
+const countries = computed(() => {
+ return countryCodes.filter((item) => {
+ return !props.unavailableCountries.includes(item.code)
+ })
+})
+
+const phoneInputContainer = ref(null)
+const { width } = useElementSize(phoneInputContainer)
+
+const countryFlagSize = computed(() => {
+ const size = resolvedSize.value
+ return (size === 'xs' || size === 'sm') ? 'small' : 'normal'
+})
+
+// Methods
+const getCountryBy = (code = 'US', type = 'code') => {
+ if (!code) code = 'US' // Default US
+ return countries.value.find((item) => {
+ return item[type] === code
+ }) ?? null
+}
+
+const onInput = (event) => {
+ inputVal.value = event?.target?.value.replace(/[^0-9]/g, '')
+}
+
+const onChangeCountryCode = () => {
+ if (!selectedCountryCode.value && countries.value.length > 0) {
+ selectedCountryCode.value = countries.value[0]
+ }
+ if (props.canOnlyCountry && (inputVal.value === null || inputVal.value === '' || !inputVal.value)) {
+ compVal.value = selectedCountryCode.value.code + selectedCountryCode.value.dial_code
+ }
+}
+
+const initState = () => {
+ if (compVal.value === null) {
+ return
+ }
+ if (!compVal.value?.startsWith('+')) {
+ // If the user already selected a country, don't override it with inference
+ if (!selectedCountryCode.value) {
+ selectedCountryCode.value = getCountryBy(compVal.value.substring(2, 0))
}
- },
-
- methods: {
- getCountryBy (code = 'US', type = 'code') {
- if (!code) code = 'US' // Default US
- return this.countries.find((item) => {
- return item[type] === code
- }) ?? null
- },
- onInput (event) {
- this.inputVal = event?.target?.value.replace(/[^0-9]/g, '')
-
- },
- onChangeCountryCode () {
- if (!this.selectedCountryCode && this.countries.length > 0) {
- this.selectedCountryCode = this.countries[0]
- }
- if (this.canOnlyCountry && (this.inputVal === null || this.inputVal === '' || !this.inputVal)) {
- this.compVal = this.selectedCountryCode.code + this.selectedCountryCode.dial_code
- }
- },
- initState () {
- if (this.compVal === null) {
- return
- }
- if (!this.compVal?.startsWith('+')) {
- // If the user already selected a country, don't override it with inference
- if (!this.selectedCountryCode) {
- this.selectedCountryCode = this.getCountryBy(this.compVal.substring(2, 0))
- }
- }
+ }
- const phoneObj = parsePhoneNumber(this.compVal)
- if (phoneObj !== undefined && phoneObj) {
- if (phoneObj.country !== undefined && phoneObj.country) {
- this.selectedCountryCode = this.getCountryBy(phoneObj.country)
- }
- this.inputVal = phoneObj.nationalNumber
+ // Parse only the dial part (strip ISO code prefix like "US")
+ let toParse = compVal.value
+ if (!toParse.startsWith('+')) {
+ const plusIndex = toParse.indexOf('+')
+ if (plusIndex >= 0) {
+ toParse = toParse.substring(plusIndex)
+ }
+ }
+ const phoneObj = parsePhoneNumber(toParse)
+ if (phoneObj !== undefined && phoneObj) {
+ if (phoneObj.country !== undefined && phoneObj.country) {
+ // Sync selection to parsed country when different or not set
+ if (!selectedCountryCode.value || selectedCountryCode.value.code !== phoneObj.country) {
+ selectedCountryCode.value = getCountryBy(phoneObj.country)
}
}
+ inputVal.value = phoneObj.nationalNumber
}
}
-</script>
+
+// Watchers
+watch(inputVal, (val) => {
+ if (!selectedCountryCode.value) return
+
+ if (val && val.startsWith('0')) {
+ val = val.substring(1)
+ }
+ if (props.canOnlyCountry) {
+ compVal.value = (val) ? selectedCountryCode.value.code + selectedCountryCode.value.dial_code + val : selectedCountryCode.value.code + selectedCountryCode.value.dial_code
+ } else {
+ compVal.value = (val) ? selectedCountryCode.value.code + selectedCountryCode.value.dial_code + val : null
+ }
+})
+
+watch(() => compVal.value, () => {
+ initState()
+})
+
+watch(selectedCountryCode, (newVal, oldVal) => {
+ if (compVal.value && newVal && oldVal) {
+ compVal.value = compVal.value.replace(oldVal.code + oldVal.dial_code, newVal.code + newVal.dial_code)
+ }
+})
+
+// Lifecycle
+onMounted(() => {
+ if (compVal.value) {
+ initState()
+ }
+ if (!selectedCountryCode.value) {
+ selectedCountryCode.value = getCountryBy()
+ }
+ if (!selectedCountryCode.value || countries.value.length === 1) {
+ selectedCountryCode.value = countries.value[0]
+ }
+})
+</script>
\ No newline at end of file
diff --git a/client/lib/forms/themes/phone-input.theme.js b/client/lib/forms/themes/phone-input.theme.js
index c75cbfc9b..72e3baef1 100644
--- a/client/lib/forms/themes/phone-input.theme.js
+++ b/client/lib/forms/themes/phone-input.theme.js
@@ -4,6 +4,8 @@
export const phoneInputTheme = {
slots: {
countrySelectWidth: '',
+ selected: '',
+ option: '',
selectedMaxHeight: '',
flag: '',
input: [
@@ -53,24 +55,32 @@ export const phoneInputTheme = {
size: {
xs: {
input: 'px-2.5 py-1.5 text-xs',
+ selected: 'text-xs',
+ option: 'text-xs',
countrySelectWidth: 'w-[80px]',
selectedMaxHeight: 'max-h-[16px]',
flag: '-mt-[16px]!'
},
sm: {
input: 'px-2 py-1.5 text-sm',
+ selected: 'text-sm',
+ option: 'text-sm',
countrySelectWidth: 'w-[100px]',
selectedMaxHeight: 'max-h-[20px]',
flag: '-mt-[14px]!'
},
md: {
input: 'px-4 py-2 text-base',
+ selected: 'text-base',
+ option: 'text-base',
countrySelectWidth: 'w-[120px]',
selectedMaxHeight: 'max-h-[24px]',
flag: '-mt-[9px]! rounded-xs'
},
lg: {
input: 'px-5 py-3 text-lg',
+ selected: 'text-lg',
+ option: 'text-lg',
countrySelectWidth: 'w-[120px]',
selectedMaxHeight: 'max-h-[28px]',
flag: '-mt-[9px]! rounded-xs'
diff --git a/client/lib/forms/themes/v-select.theme.js b/client/lib/forms/themes/v-select.theme.js
index 94b462533..87a77e80b 100644
--- a/client/lib/forms/themes/v-select.theme.js
+++ b/client/lib/forms/themes/v-select.theme.js
@@ -11,12 +11,12 @@ export const vSelectTheme = {
],
buttonInner: [
'flex items-center',
- 'ltr-only:pr-8 rtl-only:pl-8'
+ 'ltr-only:pr-6 rtl-only:pl-6'
],
placeholder: [
'text-neutral-400 dark:text-neutral-500 w-full ltr:text-left rtl:text-right! truncate ltr:pr-3 rtl:pl-3 rtl:pr-0!'
],
- chevronGradient: 'absolute inset-y-0 ltr-only:right-6 rtl-only:left-6 w-10 pointer-events-none -z-1',
+ chevronGradient: 'absolute inset-y-0 ltr-only:right-6 rtl-only:left-6 w-10 pointer-events-none',
chevronContainer: [
'absolute inset-y-0 ltr-only:right-0 rtl-only:left-0 rtl-only:right-auto!',
'flex items-center ltr-only:pr-2 rtl-only:pl-2 rtl-only:pr-0! pointer-events-none'
diff --git a/client/pages/home.vue b/client/pages/home.vue
index 624cb5945..d2391aab9 100644
--- a/client/pages/home.vue
+++ b/client/pages/home.vue
@@ -22,6 +22,7 @@
multiple
placeholder="Tags"
class="hidden sm:block"
+ :ui="{ content: 'min-w-fit' }"
/>
<!-- Clear button -->