feat(amount) : affichage groupé temps réel (séparateurs de milliers)
This commit is contained in:
@@ -9,10 +9,9 @@
|
|||||||
:autocomplete="autocomplete"
|
:autocomplete="autocomplete"
|
||||||
:class="mergedInputClass"
|
:class="mergedInputClass"
|
||||||
:required="required"
|
:required="required"
|
||||||
:maxlength="maxLength"
|
|
||||||
:minlength="minLength"
|
:minlength="minLength"
|
||||||
:disabled="disabled"
|
:disabled="disabled"
|
||||||
:value="currentValue"
|
:value="formattedValue"
|
||||||
:readonly="readonly"
|
:readonly="readonly"
|
||||||
:aria-invalid="!!error"
|
:aria-invalid="!!error"
|
||||||
:aria-describedby="describedBy"
|
:aria-describedby="describedBy"
|
||||||
@@ -66,6 +65,7 @@ import {computed, ref, useAttrs, useId} from 'vue'
|
|||||||
import { Icon as IconifyIcon } from '@iconify/vue'
|
import { Icon as IconifyIcon } from '@iconify/vue'
|
||||||
import {twMerge} from 'tailwind-merge'
|
import {twMerge} from 'tailwind-merge'
|
||||||
import MalioRequiredMark from '../shared/RequiredMark.vue'
|
import MalioRequiredMark from '../shared/RequiredMark.vue'
|
||||||
|
import {normalizeAmount, formatGroupedAmount, countSignificant, caretFromSignificant} from './composables/amountFormat'
|
||||||
|
|
||||||
defineOptions({name: 'MalioInputAmount', inheritAttrs: false})
|
defineOptions({name: 'MalioInputAmount', inheritAttrs: false})
|
||||||
|
|
||||||
@@ -126,6 +126,7 @@ const isFocused = ref(false)
|
|||||||
const inputId = computed(() => props.id?.toString() || `malio-input-amount-${generatedId}`)
|
const inputId = computed(() => props.id?.toString() || `malio-input-amount-${generatedId}`)
|
||||||
const isControlled = computed(() => props.modelValue !== undefined)
|
const isControlled = computed(() => props.modelValue !== undefined)
|
||||||
const currentValue = computed(() => (isControlled.value ? (props.modelValue ?? '') : localValue.value))
|
const currentValue = computed(() => (isControlled.value ? (props.modelValue ?? '') : localValue.value))
|
||||||
|
const formattedValue = computed(() => formatGroupedAmount(currentValue.value))
|
||||||
const hasError = computed(() => !!props.error)
|
const hasError = computed(() => !!props.error)
|
||||||
const hasSuccess = computed(() => !!props.success)
|
const hasSuccess = computed(() => !!props.success)
|
||||||
const isFilled = computed(() => currentValue.value.trim().length > 0)
|
const isFilled = computed(() => currentValue.value.trim().length > 0)
|
||||||
@@ -190,35 +191,31 @@ const emit = defineEmits<{
|
|||||||
(event: 'update:modelValue', value: string): void
|
(event: 'update:modelValue', value: string): void
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
const normalizeAmount = (value: string) => {
|
// À la frappe : parse vers le modèle propre (émis), reformate l'affichage groupé, repositionne le curseur.
|
||||||
const sanitizedValue = value
|
|
||||||
.replace(/\s+/g, '')
|
|
||||||
.replace(/,/g, '.')
|
|
||||||
.replace(/[^\d.]/g, '')
|
|
||||||
const [integerPartRaw = '', ...decimalParts] = sanitizedValue.split('.')
|
|
||||||
const integerPart = integerPartRaw.replace(/^0+(?=\d)/, '')
|
|
||||||
const decimalPart = decimalParts.join('').slice(0, 2)
|
|
||||||
|
|
||||||
if (sanitizedValue.includes('.')) {
|
|
||||||
return `${integerPart || '0'}.${decimalPart}`
|
|
||||||
}
|
|
||||||
|
|
||||||
return integerPart
|
|
||||||
}
|
|
||||||
|
|
||||||
// Keep the DOM input value, local state, and v-model emission in sync.
|
|
||||||
const updateValue = (target: HTMLInputElement, value: string) => {
|
|
||||||
target.value = value
|
|
||||||
if (!isControlled.value) {
|
|
||||||
localValue.value = value
|
|
||||||
}
|
|
||||||
emit('update:modelValue', value)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Normalize while typing so the field never keeps invalid amount characters.
|
|
||||||
const onInput = (event: Event) => {
|
const onInput = (event: Event) => {
|
||||||
const target = event.target as HTMLInputElement
|
const target = event.target as HTMLInputElement
|
||||||
updateValue(target, normalizeAmount(target.value))
|
const rawText = target.value
|
||||||
|
const caret = target.selectionStart ?? rawText.length
|
||||||
|
const model = normalizeAmount(rawText)
|
||||||
|
|
||||||
|
// maxLength borne la longueur du MODÈLE (pas l'affichage) : on ignore le keystroke en dépassement.
|
||||||
|
if (props.maxLength != null && model.length > Number(props.maxLength)) {
|
||||||
|
target.value = formattedValue.value
|
||||||
|
const restored = Math.max(0, caret - 1)
|
||||||
|
target.setSelectionRange(restored, restored)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const display = formatGroupedAmount(model)
|
||||||
|
const sig = countSignificant(rawText, caret)
|
||||||
|
target.value = display
|
||||||
|
const newCaret = caretFromSignificant(display, sig)
|
||||||
|
target.setSelectionRange(newCaret, newCaret)
|
||||||
|
|
||||||
|
if (!isControlled.value) {
|
||||||
|
localValue.value = model
|
||||||
|
}
|
||||||
|
emit('update:modelValue', model)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep the blur handler only for focus-driven UI state.
|
// Keep the blur handler only for focus-driven UI state.
|
||||||
|
|||||||
Reference in New Issue
Block a user