dc33cf4135
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>
344 lines
11 KiB
TypeScript
344 lines
11 KiB
TypeScript
import {describe, expect, it} from 'vitest'
|
|
import {mount} from '@vue/test-utils'
|
|
import type {DefineComponent} from 'vue'
|
|
import { Icon as IconifyIcon } from '@iconify/vue'
|
|
import InputPhone from './InputPhone.vue'
|
|
|
|
type InputPhoneProps = {
|
|
id?: string
|
|
label?: string
|
|
name?: string
|
|
autocomplete?: string
|
|
modelValue?: string | null
|
|
inputClass?: string
|
|
labelClass?: string
|
|
groupClass?: string
|
|
required?: boolean
|
|
disabled?: boolean
|
|
readonly?: boolean
|
|
hint?: string
|
|
error?: string
|
|
success?: string
|
|
iconName?: string
|
|
iconPosition?: 'left' | 'right'
|
|
iconSize?: string | number
|
|
iconColor?: string
|
|
mask?: string
|
|
addable?: boolean
|
|
addIconName?: string
|
|
addButtonLabel?: string
|
|
}
|
|
|
|
const InputPhoneForTest = InputPhone as DefineComponent<InputPhoneProps>
|
|
|
|
const mountComponent = (props: InputPhoneProps = {}) =>
|
|
mount(InputPhoneForTest, {
|
|
props,
|
|
global: {
|
|
stubs: {
|
|
IconifyIcon: {
|
|
template: '<span data-test="icon" v-bind="$attrs" />',
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
describe('MalioInputPhone', () => {
|
|
it('renders the initial input value', () => {
|
|
const wrapper = mountComponent({modelValue: '+33 6 12 34 56 78'})
|
|
|
|
expect(wrapper.get('input').element.value).toBe('+33 6 12 34 56 78')
|
|
})
|
|
|
|
it('renders the label text', () => {
|
|
const wrapper = mountComponent({label: 'Téléphone'})
|
|
|
|
expect(wrapper.get('label').text()).toBe('Téléphone')
|
|
})
|
|
|
|
it('has type tel', () => {
|
|
const wrapper = mountComponent()
|
|
|
|
expect(wrapper.get('input').attributes('type')).toBe('tel')
|
|
})
|
|
|
|
it('has inputmode tel', () => {
|
|
const wrapper = mountComponent()
|
|
|
|
expect(wrapper.get('input').attributes('inputmode')).toBe('tel')
|
|
})
|
|
|
|
it('renders the default phone icon', () => {
|
|
const wrapper = mountComponent()
|
|
|
|
const iconComponent = wrapper.findComponent(IconifyIcon)
|
|
expect(iconComponent.props('icon')).toBe('mdi:phone-outline')
|
|
})
|
|
|
|
it('allows overriding the icon', () => {
|
|
const wrapper = mountComponent({iconName: 'mdi:cellphone'})
|
|
|
|
const iconComponent = wrapper.findComponent(IconifyIcon)
|
|
expect(iconComponent.props('icon')).toBe('mdi:cellphone')
|
|
})
|
|
|
|
it('does not render icon when iconName is empty', () => {
|
|
const wrapper = mountComponent({iconName: ''})
|
|
|
|
expect(wrapper.find('[data-test="icon"]').exists()).toBe(false)
|
|
})
|
|
|
|
it('places icon on the left by default', () => {
|
|
const wrapper = mountComponent()
|
|
|
|
expect(wrapper.get('[data-test="icon"]').classes()).toContain('left-[10px]')
|
|
})
|
|
|
|
it('places icon on the right when iconPosition is right', () => {
|
|
const wrapper = mountComponent({iconPosition: 'right'})
|
|
|
|
expect(wrapper.get('[data-test="icon"]').classes()).toContain('right-[10px]')
|
|
})
|
|
|
|
it('emits update:modelValue on input change', async () => {
|
|
const wrapper = mountComponent({modelValue: ''})
|
|
|
|
await wrapper.get('input').setValue('+33612345678')
|
|
|
|
expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['+33612345678'])
|
|
})
|
|
|
|
it('sets disabled styles when true', () => {
|
|
const wrapper = mountComponent({disabled: true})
|
|
|
|
expect(wrapper.get('input').attributes('disabled')).toBeDefined()
|
|
expect(wrapper.get('input').classes()).toContain('cursor-not-allowed')
|
|
})
|
|
|
|
it('sets readonly when true', () => {
|
|
const wrapper = mountComponent({readonly: true})
|
|
|
|
expect(wrapper.get('input').attributes('readonly')).toBeDefined()
|
|
})
|
|
|
|
it('shows error message and styles', () => {
|
|
const wrapper = mountComponent({error: 'Numéro invalide'})
|
|
|
|
expect(wrapper.get('p.text-m-danger').text()).toBe('Numéro invalide')
|
|
expect(wrapper.get('input').classes()).toContain('border-m-danger')
|
|
expect(wrapper.get('input').attributes('aria-invalid')).toBe('true')
|
|
})
|
|
|
|
it('shows error style on icon', () => {
|
|
const wrapper = mountComponent({error: 'Error'})
|
|
|
|
expect(wrapper.get('[data-test="icon"]').classes()).toContain('text-m-danger')
|
|
})
|
|
|
|
it('shows success message and styles', () => {
|
|
const wrapper = mountComponent({success: 'Numéro valide'})
|
|
|
|
expect(wrapper.get('p.text-m-success').text()).toBe('Numéro valide')
|
|
expect(wrapper.get('input').classes()).toContain('border-m-success')
|
|
})
|
|
|
|
it('shows success style on icon', () => {
|
|
const wrapper = mountComponent({success: 'Success'})
|
|
|
|
expect(wrapper.get('[data-test="icon"]').classes()).toContain('text-m-success')
|
|
})
|
|
|
|
it('shows default icon color when empty and unfocused', () => {
|
|
const wrapper = mountComponent()
|
|
|
|
expect(wrapper.get('[data-test="icon"]').classes()).toContain('text-m-muted')
|
|
})
|
|
|
|
it('shows primary icon color on focus', async () => {
|
|
const wrapper = mountComponent()
|
|
|
|
await wrapper.get('input').trigger('focus')
|
|
|
|
expect(wrapper.get('[data-test="icon"]').classes()).toContain('text-m-primary')
|
|
})
|
|
|
|
it('shows black icon color when filled and unfocused', () => {
|
|
const wrapper = mountComponent({modelValue: '+33612345678'})
|
|
|
|
expect(wrapper.get('[data-test="icon"]').classes()).toContain('text-black')
|
|
})
|
|
|
|
it('keeps default icon color when disabled, even if filled', () => {
|
|
const wrapper = mountComponent({modelValue: '+33612345678', disabled: true})
|
|
|
|
expect(wrapper.get('[data-test="icon"]').classes()).toContain('text-m-muted')
|
|
})
|
|
|
|
it('error overrides focus color on icon', async () => {
|
|
const wrapper = mountComponent({error: 'Numéro invalide'})
|
|
|
|
await wrapper.get('input').trigger('focus')
|
|
|
|
expect(wrapper.get('[data-test="icon"]').classes()).toContain('text-m-danger')
|
|
})
|
|
|
|
it('shows hint message', () => {
|
|
const wrapper = mountComponent({hint: 'Format international recommandé'})
|
|
|
|
expect(wrapper.get('p.text-m-muted').text()).toBe('Format international recommandé')
|
|
})
|
|
|
|
it('links label to input via for/id', () => {
|
|
const wrapper = mountComponent({id: 'phone-field', label: 'Téléphone'})
|
|
|
|
expect(wrapper.get('input').attributes('id')).toBe('phone-field')
|
|
expect(wrapper.get('label').attributes('for')).toBe('phone-field')
|
|
})
|
|
|
|
it('generates an id when missing and reuses it on label', () => {
|
|
const wrapper = mountComponent({label: 'Téléphone'})
|
|
|
|
const inputId = wrapper.get('input').attributes('id')
|
|
|
|
expect(inputId?.startsWith('malio-input-phone-')).toBe(true)
|
|
expect(wrapper.get('label').attributes('for')).toBe(inputId)
|
|
})
|
|
|
|
it('aria-invalid is false when no error', () => {
|
|
const wrapper = mountComponent()
|
|
|
|
expect(wrapper.get('input').attributes('aria-invalid')).toBe('false')
|
|
})
|
|
|
|
it('uses autocomplete off by default', () => {
|
|
const wrapper = mountComponent()
|
|
|
|
expect(wrapper.get('input').attributes('autocomplete')).toBe('off')
|
|
})
|
|
|
|
it('allows overriding autocomplete', () => {
|
|
const wrapper = mountComponent({autocomplete: 'tel'})
|
|
|
|
expect(wrapper.get('input').attributes('autocomplete')).toBe('tel')
|
|
})
|
|
|
|
it('does not render add button by default', () => {
|
|
const wrapper = mountComponent()
|
|
|
|
expect(wrapper.find('[data-test="add-button"]').exists()).toBe(false)
|
|
})
|
|
|
|
it('renders add button when addable is true', () => {
|
|
const wrapper = mountComponent({addable: true})
|
|
|
|
expect(wrapper.find('[data-test="add-button"]').exists()).toBe(true)
|
|
})
|
|
|
|
it('emits add event when add button is clicked', async () => {
|
|
const wrapper = mountComponent({addable: true})
|
|
|
|
await wrapper.get('[data-test="add-button"]').trigger('click')
|
|
|
|
expect(wrapper.emitted('add')).toHaveLength(1)
|
|
})
|
|
|
|
it('does not emit add when disabled', async () => {
|
|
const wrapper = mountComponent({addable: true, disabled: true})
|
|
|
|
await wrapper.get('[data-test="add-button"]').trigger('click')
|
|
|
|
expect(wrapper.emitted('add')).toBeUndefined()
|
|
})
|
|
|
|
it('does not emit add when readonly', async () => {
|
|
const wrapper = mountComponent({addable: true, readonly: true})
|
|
|
|
await wrapper.get('[data-test="add-button"]').trigger('click')
|
|
|
|
expect(wrapper.emitted('add')).toBeUndefined()
|
|
})
|
|
|
|
it('disables add button when disabled', () => {
|
|
const wrapper = mountComponent({addable: true, disabled: true})
|
|
|
|
expect(wrapper.get('[data-test="add-button"]').attributes('disabled')).toBeDefined()
|
|
})
|
|
|
|
it('disables add button when readonly', () => {
|
|
const wrapper = mountComponent({addable: true, readonly: true})
|
|
|
|
expect(wrapper.get('[data-test="add-button"]').attributes('disabled')).toBeDefined()
|
|
})
|
|
|
|
it('renders the default add icon (mdi:plus)', () => {
|
|
const wrapper = mountComponent({addable: true})
|
|
|
|
const icons = wrapper.findAllComponents(IconifyIcon)
|
|
const addIcon = icons[icons.length - 1]
|
|
expect(addIcon.props('icon')).toBe('mdi:plus')
|
|
})
|
|
|
|
it('allows overriding the add icon', () => {
|
|
const wrapper = mountComponent({addable: true, addIconName: 'mdi:phone-plus'})
|
|
|
|
const icons = wrapper.findAllComponents(IconifyIcon)
|
|
const addIcon = icons[icons.length - 1]
|
|
expect(addIcon.props('icon')).toBe('mdi:phone-plus')
|
|
})
|
|
|
|
it('exposes aria-label on add button', () => {
|
|
const wrapper = mountComponent({addable: true, addButtonLabel: 'Ajouter un autre numéro'})
|
|
|
|
expect(wrapper.get('[data-test="add-button"]').attributes('aria-label')).toBe('Ajouter un autre numéro')
|
|
})
|
|
|
|
it('adds right padding to input when addable', () => {
|
|
const wrapper = mountComponent({addable: true})
|
|
|
|
expect(wrapper.get('input').classes()).toContain('!pr-10')
|
|
})
|
|
|
|
it('shows default add button color when empty and unfocused', () => {
|
|
const wrapper = mountComponent({addable: true})
|
|
|
|
expect(wrapper.get('[data-test="add-button"]').classes()).toContain('text-m-muted')
|
|
expect(wrapper.get('[data-test="add-button"]').classes()).not.toContain('text-m-primary')
|
|
})
|
|
|
|
it('shows primary add button color on focus', async () => {
|
|
const wrapper = mountComponent({addable: true})
|
|
|
|
await wrapper.get('input').trigger('focus')
|
|
|
|
expect(wrapper.get('[data-test="add-button"]').classes()).toContain('text-m-primary')
|
|
})
|
|
|
|
it('shows black add button color when filled and unfocused', () => {
|
|
const wrapper = mountComponent({addable: true, modelValue: '+33612345678'})
|
|
|
|
expect(wrapper.get('[data-test="add-button"]').classes()).toContain('text-black')
|
|
})
|
|
|
|
it('error overrides focus color on add button', async () => {
|
|
const wrapper = mountComponent({addable: true, error: 'Numéro invalide'})
|
|
|
|
await wrapper.get('input').trigger('focus')
|
|
|
|
expect(wrapper.get('[data-test="add-button"]').classes()).toContain('text-m-danger')
|
|
})
|
|
|
|
it('success applies to add button', () => {
|
|
const wrapper = mountComponent({addable: true, success: 'Numéro valide'})
|
|
|
|
expect(wrapper.get('[data-test="add-button"]').classes()).toContain('text-m-success')
|
|
})
|
|
|
|
it('applies mask via maska directive', async () => {
|
|
const wrapper = mountComponent({mask: '+## # ## ## ## ##'})
|
|
|
|
await wrapper.get('input').setValue('33612345678')
|
|
|
|
expect(wrapper.emitted('update:modelValue')).toBeDefined()
|
|
})
|
|
})
|