Files
malio-layer-ui/app/components/malio/time/Time.vue
T
tristan dc33cf4135 feat(inputs): UX polish across input family + localFilter + focus scrollbar
Polish across the form input components, plus two new features and a few
standalone fixes.

Fixes
-----
* Reserve hint/error/success paragraph space (min-h-[1rem]) in 15
  components so a single error message no longer shifts neighboring grid
  cells: InputText, Email, Password, Phone, Amount, Number, Upload,
  Autocomplete, RichText, TextArea, Select, SelectCheckbox, Time,
  TimePicker, CalendarField, Checkbox.
* InputPhone: the '+' add button now follows the icon-state cascade
  (muted / primary on focus / black when filled / danger / success) like
  the other field icons instead of being permanently primary.
* Select and SelectCheckbox: chevron color follows the field state
  (muted by default, primary when open, black when an option is
  selected, danger / success on error / success) instead of always being
  text-current.
* InputTextArea: single-root component (was multi-root). The message
  wrapper used to occupy its own grid cell, breaking row-span layouts.
  Now flex flex-col, with the textarea area filling the available height
  via flex-1 and the message inside the same root.
* Disabled labels use text-m-muted (border-gray) instead of text-black/60
  (dark) across InputText, Email, Password, Amount, Phone, Upload,
  Autocomplete, TextArea, RichText. Also removes an unreachable
  peer-[&:not(:placeholder-shown):not(:focus)]:text-black/60 rule that
  twMerge was silently overriding with text-black.
* InputAutocomplete: eliminates four sources of visual jitter when
  focusing / opening a field that already has a selected value.
  - Drop peer-focus:-translate-y-[1.55rem] extra label translate.
  - Drop the .grow-height:focus padding rule (no more height growth or
    downward text shift on focus).
  - Drop focus:pl-[11px] (no more 1px horizontal jump).
  - Replace !border-b-0 with !border-b-transparent so the bottom border
    still reserves its 1px while remaining invisible against the
    dropdown.
* Select / SelectCheckbox: same anti-jitter treatment.
  - Drop .grow-height:focus padding rule (~12px height growth gone).
  - Replace !border-b-0 / !border-t-0 with !border-b-transparent /
    !border-t-transparent across danger / success / primary branches.
* Button: default width 240px -> 200px to match the form button sizing
  used across the app. Test updated to match.

Features
--------
* InputTextArea: scrollbar turns primary blue on focus
  (scrollbar-color: rgb(var(--m-primary)) transparent), matching the
  Select listbox styling.
* InputAutocomplete: new localFilter prop (default false). When enabled,
  filters the options prop client-side based on the input value
  (case-insensitive label.includes(query)), so static lists no longer
  need a @search listener. Async/API usage keeps the existing behavior.
  Playground "Simple statique" and "Avec icône à gauche" examples use
  local-filter.

Playground
----------
* client.vue: tighter grid gap (gap-y-5) plus an example error on a
  SelectCheckbox to visually exercise the message-space fix.

Tests
-----
All component test files include regression coverage for the above.
720/720 tests pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-29 15:43:53 +02:00

264 lines
6.9 KiB
Vue

<template>
<div>
<div :class="mergedGroupClass">
<label
v-if="label"
:for="hoursInputId"
:class="mergedLabelClass"
>
{{ label }}
</label>
<div class="flex items-center gap-2">
<input
:id="hoursInputId"
:name="hoursName"
autocomplete="off"
:class="mergedInputClass('hours')"
:required="required"
:disabled="disabled"
:readonly="readonly"
:aria-invalid="!!error"
:aria-describedby="describedBy"
:value="hoursValue"
v-bind="attrs"
type="text"
inputmode="numeric"
placeholder="00"
maxlength="2"
@input="onHoursInput"
@focus="activeField = 'hours'"
@blur="onHoursBlur"
>
<span class="text-[18px] text-black">:</span>
<input
ref="minutesInputRef"
:id="minutesInputId"
:name="minutesName"
autocomplete="off"
:class="mergedInputClass('minutes')"
:required="required"
:disabled="disabled"
:readonly="readonly"
:aria-invalid="!!error"
:aria-describedby="describedBy"
:value="minutesValue"
v-bind="attrs"
type="text"
inputmode="numeric"
placeholder="00"
maxlength="2"
@input="onMinutesInput"
@focus="activeField = 'minutes'"
@blur="onMinutesBlur"
>
</div>
</div>
<p
:id="`${inputId}-describedby`"
:class="[
hasError
? 'text-m-danger'
: hasSuccess
? 'text-m-success'
: 'text-m-muted',
'mt-1 ml-[2px] text-xs min-h-[1rem]',
]"
>
{{ error || success || hint }}
</p>
</div>
</template>
<script setup lang="ts">
import {computed, nextTick, ref, useAttrs, useId, watch} from 'vue'
import {twMerge} from 'tailwind-merge'
defineOptions({name: 'MalioTime', inheritAttrs: false})
const props = withDefaults(
defineProps<{
id?: string
label?: string
name?: string
modelValue?: string | null | undefined
inputClass?: string
labelClass?: string
groupClass?: string
required?: boolean
disabled?: boolean
readonly?: boolean
hint?: string
error?: string
success?: string
}>(),
{
id: '',
name: '',
modelValue: undefined,
label: '',
inputClass: '',
labelClass: '',
groupClass: '',
required: false,
readonly: false,
disabled: false,
hint: '',
error: '',
success: '',
},
)
const attrs = useAttrs()
const generatedId = useId()
const hoursValue = ref('')
const minutesValue = ref('')
const activeField = ref<'hours' | 'minutes' | null>(null)
const minutesInputRef = ref<HTMLInputElement | null>(null)
const inputId = computed(() => props.id?.toString() || `malio-time-${generatedId}`)
const hoursInputId = computed(() => `${inputId.value}-hours`)
const minutesInputId = computed(() => `${inputId.value}-minutes`)
const hoursName = computed(() => (props.name ? `${props.name}-hours` : ''))
const minutesName = computed(() => (props.name ? `${props.name}-minutes` : ''))
const hasError = computed(() => !!props.error)
const hasSuccess = computed(() => !!props.success && !hasError.value)
const emit = defineEmits<{
(event: 'update:modelValue', value: string): void
}>()
const padSegment = (value: string) => (value === '' ? '' : value.padStart(2, '0'))
const sanitizeDigits = (value: string) =>
value.replace(/\D/g, '').slice(0, 2)
const normalizeHours = (value: string) => {
const digits = sanitizeDigits(value)
if (digits === '') return ''
if (Number.parseInt(digits, 10) > 23) return '23'
return digits
}
const normalizeMinutes = (value: string) => {
const digits = sanitizeDigits(value)
if (digits === '') return ''
if (Number.parseInt(digits, 10) > 59) return '59'
return digits
}
const parseTimeValue = (value: string | null | undefined) => {
if (!value) return {hours: '', minutes: ''}
const [rawHours = '', rawMinutes = ''] = value.split(':')
return {
hours: normalizeHours(rawHours),
minutes: normalizeMinutes(rawMinutes),
}
}
const syncFromModelValue = (value: string | null | undefined) => {
if (activeField.value) return
const parsedValue = parseTimeValue(value)
hoursValue.value = parsedValue.hours
minutesValue.value = parsedValue.minutes
}
watch(() => props.modelValue, syncFromModelValue, {immediate: true})
const describedBy = computed(() =>
(props.hint || hasError.value || hasSuccess.value) ? `${inputId.value}-describedby` : undefined,
)
const mergedGroupClass = computed(() =>
twMerge(
'relative mt-4 flex w-full items-center',
props.groupClass,
),
)
const mergedLabelClass = computed(() =>
twMerge(
'mt-px mr-4 cursor-pointer text-black text-[18px]',
hasError.value ? 'text-m-danger' : '',
hasSuccess.value ? 'text-m-success' : '',
props.disabled ? 'cursor-not-allowed text-black/60' : '',
props.labelClass
),
)
const mergedInputClass = (field: 'hours' | 'minutes') =>
twMerge(
'h-[30px] w-10 border bg-white text-center text-[18px] outline-none rounded-md placeholder:text-m-muted',
props.disabled ? 'cursor-not-allowed text-black/60 border-m-muted' : 'cursor-text',
hasError.value
? 'border-m-danger focus:border-m-danger'
: hasSuccess.value
? 'border-m-success focus:border-m-success'
: activeField.value === field
? 'border-m-primary text-m-primary'
: 'border-black text-black',
props.inputClass,
)
const emitCurrentValue = (pad = false) => {
if (!hoursValue.value && !minutesValue.value) {
emit('update:modelValue', '')
return
}
const h = pad ? padSegment(hoursValue.value || '0') : (hoursValue.value || '00')
const m = pad ? padSegment(minutesValue.value || '0') : (minutesValue.value || '00')
emit('update:modelValue', `${h}:${m}`)
}
const onHoursInput = (event: Event) => {
const target = event.target as HTMLInputElement
const normalizedValue = normalizeHours(target.value)
hoursValue.value = normalizedValue
target.value = normalizedValue
emitCurrentValue()
if (normalizedValue.length === 2) {
nextTick(() => minutesInputRef.value?.focus())
}
}
const onMinutesInput = (event: Event) => {
const target = event.target as HTMLInputElement
const normalizedValue = normalizeMinutes(target.value)
minutesValue.value = normalizedValue
target.value = normalizedValue
emitCurrentValue()
}
const formatFieldOnBlur = (field: 'hours' | 'minutes') => {
if (field === 'hours' && hoursValue.value) {
hoursValue.value = padSegment(hoursValue.value)
}
if (field === 'minutes' && minutesValue.value) {
minutesValue.value = padSegment(minutesValue.value)
}
emitCurrentValue(true)
}
const onHoursBlur = () => {
formatFieldOnBlur('hours')
activeField.value = null
}
const onMinutesBlur = () => {
formatFieldOnBlur('minutes')
activeField.value = null
}
</script>