<template>
    <label class="w-full border border-solid border-gray-500 focus-within:border-primary rounded-lg relative flex">
        <input
            :id="id"
            v-model="inner"
            :name="name"
            :min="min"
            :max="max"
            :type="type"
            placeholder=""
            class="bg-white group-[.info]:bg-blue-200 rounded-lg focus:outline-none text-gray-600 text-left w-full py-1 @sm:py-2 @sm:group-[.info]:py-1 px-2 @sm:px-3 @sm:group-[.info]:px-2 text-sm @sm:text-base @sm:group-[.info]:text-sm"
            @keypress="$emit('keypress', $event)"
        />
        <div v-if="hint" class="absolute right-0 mr-2 flex items-center h-full">
            <hint :value="hint"></hint>
        </div>
        <field-label :name="label" class="group-[.info]:bg-blue-200" :required="required"></field-label>
    </label>
</template>

<script setup>
import {computed} from 'vue';
import FieldLabel from '../FieldLabel.vue';
import Hint from '../Hint.vue';

const emit = defineEmits(['update:modelValue', 'keypress']);
const props = defineProps({
    modelValue: {
        required: true,
        validator: (value) => value === null || typeof value === 'string',
    },
    required: {
        required: false,
        type: Boolean,
        default: () => false,
    },
    name: {
        required: true,
        type: String,
    },
    id: {
        required: true,
        type: String,
    },
    min: {
        type: String,
        default: () => '',
    },
    max: {
        type: String,
        default: () => '',
    },
    label: {
        required: true,
        type: String,
    },
    type: {
        required: false,
        type: String,
        default: () => 'text',
    },
    hint: {
        required: false,
        validator: (value) => value === null || typeof value === 'string',
        default: () => null,
    },
});

const inner = computed({
    get: () => props.modelValue,
    set: (v) => emit('update:modelValue', v),
});
</script>
