Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom input fields #3483

Merged
merged 13 commits into from
May 1, 2024
Original file line number Diff line number Diff line change
@@ -1,52 +1,28 @@
<template>
<div
class="input-container"
:class="{ error: errorMessage, disabled: disabled, focused: focused }"
@click="focusInput"
>
<label for="custom-input" class="input-label">{{ label }}</label>
<input
id="custom-input"
class="input-field"
ref="inputField"
:value="modelValue"
@input="updateValue"
:disabled="disabled"
@focus="onFocus"
@blur="onBlur"
/>
<div class="input-container" :class="{ error: errorMessage }" @click.self.stop="focusInput">
blanchco marked this conversation as resolved.
Show resolved Hide resolved
<label @click.self.stop="focusInput">{{ label }}</label>
<input v-bind="attrs" ref="inputField" :value="modelValue" @input="updateValue" />
</div>
<p v-if="errorMessage" class="error-message">
<i class="pi pi-exclamation-circle" /> {{ errorMessage }}
</p>
<aside v-if="errorMessage"><i class="pi pi-exclamation-circle" /> {{ errorMessage }}</aside>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { ref, useAttrs } from 'vue';

defineProps<{
modelValue: string;
label?: string;
errorMessage?: string;
disabled?: boolean;
}>();

const emit = defineEmits(['update:modelValue']);
const inputField = ref<HTMLInputElement | null>(null);
const focused = ref(false);
const attrs = useAttrs();

const focusInput = () => {
inputField.value?.focus();
};

const onFocus = () => {
focused.value = true;
};

const onBlur = () => {
focused.value = false;
};

const updateValue = (event: Event) => {
const value = (event.target as HTMLInputElement).value;
emit('update:modelValue', value);
Expand All @@ -60,18 +36,19 @@ const updateValue = (event: Event) => {
align-items: center;
width: 100%;
padding: var(--gap-xsmall) var(--gap-small);
background-color: #fff;
background-color: var(--surface-section);
border: 1px solid var(--surface-border-alt);
border-radius: var(--border-radius-small);
cursor: text;
margin-bottom: var(--gap-small);
transition: border-color 0.3s ease-in-out;
font-family: var(--font-family);

&.disabled {
&:has(*:disabled) {
opacity: 0.5;
}

&.focused {
&:has(*:focus) {
border-color: var(--primary-color);
}
&.error {
Expand All @@ -82,21 +59,21 @@ const updateValue = (event: Event) => {
}
}

.input-label {
background-color: transparent;
label {
background-color: none;
color: var(--text-color-secondary);
cursor: text;
padding-right: var(--gap-small);
}

.input-field {
input {
text-align: right;
YohannParis marked this conversation as resolved.
Show resolved Hide resolved
flex-grow: 1;
border: none;
background-color: transparent;
background-color: none;
blanchco marked this conversation as resolved.
Show resolved Hide resolved
}

.error-message {
aside {
color: var(--error-message-color);
font-size: var(--font-caption);
word-wrap: break-word;
Expand Down
Loading