Skip to content

Commit

Permalink
refactor: Add intl-tel-input package for phone number input
Browse files Browse the repository at this point in the history
  • Loading branch information
danielswiatek committed Sep 7, 2024
1 parent 70149cd commit e23b8ac
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 3 deletions.
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@vueuse/core": "^10.6.1",
"crypto-js": "^4.2.0",
"http2-proxy": "^5.0.53",
"intl-tel-input": "^24.4.0",
"remixicon": "^3.5.0",
"socket.io-client": "^4.7.2",
"vue": "^3.4.18",
Expand Down
92 changes: 92 additions & 0 deletions frontend/src/components/BasicInputs/BasicInputPhoneNumber.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<script setup lang="ts">
import { useVModel } from '@vueuse/core'
import intlTelInput from 'intl-tel-input/intlTelInputWithUtils'
import { onMounted, onUnmounted, ref } from 'vue'
import useValidationModel from '../../composables/useValidationModel'
import BasicFormGroup from './components/BasicFormGroup.vue'
import { type BasicInputDefaultProps } from './defaultProps'
const props = withDefaults(
defineProps<
BasicInputDefaultProps<string> & {
inputClass?: string
disableValidation?: boolean
}
>(),
{
type: 'number',
autocapitalize: 'off',
inputClass: '',
disableValidation: false,
}
)
const emit = defineEmits<{
(event: 'update:modelValue', eventArgs: string | undefined): void
(event: 'focus'): void
(event: 'blur'): void
}>()
const { model, errorMessage } = props.disableValidation
? {
model: useVModel(props, 'modelValue', emit),
errorMessage: undefined,
}
: useValidationModel(props, emit)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let intl: any = null
const phone = ref()
onMounted(async () => {
// await import('intl-tel-input/build/js/utils.js')
intl = intlTelInput(phone.value, {
separateDialCode: true,
strictMode: true,
initialCountry: 'de',
autoPlaceholder: 'off',
})
})
onUnmounted(() => {
intl?.destroy()
})
</script>

<template>
<BasicFormGroup
:id="id"
:name="name"
:label="label"
:required="required"
:error-message="errorMessage"
>
<div class="intl-tel-wrapper">
<input
:id="id || name || label"
ref="phone"
:value="model"
type="tel"
:placeholder="placeholder || label || name"
:class="inputClass"
:name="id || name || label"
:disabled="disabled"
/>
</div>
<template #label>
<slot name="label"></slot>
</template>
</BasicFormGroup>
</template>

<style lang="scss">
@import 'intl-tel-input/build/css/intlTelInput.css';
.intl-tel-wrapper {
.iti {
@apply block w-full;
.form-control {
@apply block w-full;
}
}
}
</style>
3 changes: 2 additions & 1 deletion frontend/src/components/UIComponents/KontaktItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TrashIcon } from '@heroicons/vue/24/outline'
import { ref, watch } from 'vue'
import BasicCheckbox from '../BasicInputs/BasicCheckbox.vue'
import BasicInputPhoneNumber from '../BasicInputs/BasicInputPhoneNumber.vue'
import BasicInput from '@/components/BasicInputs/BasicInput.vue'
import type { TKontaktSchema } from '@codeanker/api'
Expand Down Expand Up @@ -45,7 +46,7 @@ watch(
placeholder="Nachname eingeben"
required
/>
<BasicInput
<BasicInputPhoneNumber
v-model="kontakt.telefon"
label="Mobiltelefonnummer"
placeholder="Mobiltelefonnummer eingeben"
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/forms/person/FormContactGeneral.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { computed } from 'vue'
import BasicInput from '@/components/BasicInputs/BasicInput.vue'
import BasicInputPhoneNumber from '@/components/BasicInputs/BasicInputPhoneNumber.vue'
export interface IContact {
email: string
Expand Down Expand Up @@ -37,9 +38,8 @@ const model = computed({
placeholder="Email-Adresse"
required
/>
<BasicInput
<BasicInputPhoneNumber
v-model="model.telefon"
type="tel"
label="Telefonnummer"
placeholder="Telefonnummer"
required
Expand Down
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e23b8ac

Please sign in to comment.