store/resources/js/components/ui/phone-number-input/PhoneNumberInput.vue

39 lines
958 B
Vue

<script setup lang="ts">
import type { HTMLAttributes } from 'vue';
import { computed } from 'vue';
import { Input } from '@/components/ui/input';
import { formatPhoneNumber, parsePhoneNumber } from '@/lib/phone-number';
const props = defineProps<{
id?: string;
modelValue?: string | number;
class?: HTMLAttributes['class'];
placeholder?: string;
disabled?: boolean;
}>();
const emit = defineEmits<{
(event: 'update:modelValue', value: string): void;
}>();
const displayValue = computed({
get: () => formatPhoneNumber(props.modelValue ?? ''),
set: (value: string) => {
emit('update:modelValue', parsePhoneNumber(value));
},
});
</script>
<template>
<Input
:id="id"
v-model="displayValue"
type="tel"
inputmode="tel"
autocomplete="off"
:placeholder="placeholder ?? '08xx-xxxx-xxxx'"
:disabled="disabled"
:class="props.class"
/>
</template>