{"version":3,"file":"useFormInput-BwBzLfG4.mjs","sources":["../src/utils/normalizeInput.ts","../src/composables/useFormInput.ts"],"sourcesContent":["import type {Numberish} from '../types/CommonTypes'\n\nexport const normalizeInput = (\n  v: Numberish | null,\n  modelModifiers: Record<'number' | 'lazy' | 'trim', true | undefined>\n) => {\n  if (v === null) return\n  let update = v\n  if (modelModifiers.number && typeof update === 'string' && update !== '') {\n    const parsed = Number.parseFloat(update)\n    update = Number.isNaN(parsed) ? update : parsed\n  }\n  return update\n}\n","import type {Numberish} from '../types/CommonTypes'\nimport {\n  computed,\n  inject,\n  nextTick,\n  onActivated,\n  onMounted,\n  ref,\n  type Ref,\n  type ShallowRef,\n} from 'vue'\nimport {useAriaInvalid} from './useAriaInvalid'\nimport {useId} from './useId'\nimport {useDebounceFn, useFocus, useToNumber} from '@vueuse/core'\nimport type {CommonInputProps} from '../types/FormCommonInputProps'\nimport {formGroupPluginKey} from '../utils/keys'\nimport {useStateClass} from './useStateClass'\n\nexport const useFormInput = (\n  props: Readonly<CommonInputProps>,\n  input:\n    | Readonly<ShallowRef<HTMLInputElement | null>>\n    | Readonly<ShallowRef<HTMLTextAreaElement | null>>,\n  modelValue: Ref<Numberish | null>,\n  modelModifiers: Record<'number' | 'lazy' | 'trim', true | undefined>\n) => {\n  const forceUpdateKey = ref(0)\n\n  const computedId = useId(() => props.id, 'input')\n  const debounceNumber = useToNumber(() => props.debounce ?? 0)\n  const debounceMaxWaitNumber = useToNumber(() => props.debounceMaxWait ?? NaN)\n\n  // This automatically adds the appropriate \"for\" attribute to a BFormGroup label\n  const formGroupData = inject(formGroupPluginKey, null)?.(computedId)\n  const computedState = computed(() =>\n    props.state !== undefined ? props.state : (formGroupData?.state.value ?? null)\n  )\n  const computedAriaInvalid = useAriaInvalid(() => props.ariaInvalid, computedState)\n  const stateClass = useStateClass(computedState)\n\n  const internalUpdateModelValue = useDebounceFn(\n    (value: Numberish) => {\n      modelValue.value = value\n    },\n    () => (modelModifiers.lazy === true ? 0 : debounceNumber.value),\n    {maxWait: () => (modelModifiers.lazy === true ? NaN : debounceMaxWaitNumber.value)}\n  )\n\n  const updateModelValue = (value: Numberish, force = false) => {\n    if (modelModifiers.lazy === true && force === false) return\n    internalUpdateModelValue(value)\n  }\n\n  const {focused} = useFocus(input, {\n    initialValue: props.autofocus,\n  })\n\n  const _formatValue = (value: string, evt: Readonly<Event>, force = false) => {\n    if (props.formatter !== undefined && (!props.lazyFormatter || force)) {\n      return props.formatter(value, evt)\n    }\n    return value\n  }\n  onMounted(() => {\n    if (input.value) {\n      input.value.value = modelValue.value?.toString() ?? ''\n    }\n  })\n\n  onActivated(() => {\n    nextTick(() => {\n      if (props.autofocus) {\n        focused.value = true\n      }\n    })\n  })\n\n  const onInput = (evt: Readonly<Event>) => {\n    const {value} = evt.target as HTMLInputElement\n    const formattedValue = _formatValue(value, evt)\n    if (evt.defaultPrevented) {\n      evt.preventDefault()\n      return\n    }\n\n    const nextModel = formattedValue\n\n    updateModelValue(nextModel)\n  }\n\n  const onChange = (evt: Readonly<Event>) => {\n    const {value} = evt.target as HTMLInputElement\n    const formattedValue = _formatValue(value, evt)\n    if (evt.defaultPrevented) {\n      evt.preventDefault()\n      return\n    }\n\n    const nextModel = formattedValue\n    if (modelValue.value !== nextModel) {\n      updateModelValue(formattedValue, true)\n    }\n  }\n\n  const onBlur = (evt: Readonly<FocusEvent>) => {\n    if (!modelModifiers.lazy && !props.lazyFormatter && !modelModifiers.trim) return\n\n    const {value} = evt.target as HTMLInputElement\n    const formattedValue = _formatValue(value, evt, true)\n\n    const nextModel = modelModifiers.trim ? formattedValue.trim() : formattedValue\n    const needsForceUpdate = nextModel.length !== formattedValue.length\n    if (modelValue.value !== nextModel) {\n      updateModelValue(formattedValue, true)\n    }\n    if (modelModifiers.trim && needsForceUpdate) {\n      // The value is trimmed but there would still exist some white space\n      // So, force update the value. You need to bind this to :key on the input element\n      forceUpdateKey.value = forceUpdateKey.value + 1\n    }\n  }\n\n  const focus = () => {\n    if (!props.disabled) {\n      focused.value = true\n    }\n  }\n\n  const blur = () => {\n    if (!props.disabled) {\n      focused.value = false\n    }\n  }\n\n  return {\n    input,\n    computedId,\n    computedAriaInvalid,\n    onInput,\n    onChange,\n    onBlur,\n    focus,\n    blur,\n    forceUpdateKey,\n    stateClass,\n  }\n}\n"],"names":["_a"],"mappings":";;;;;;;AAEa,MAAA,iBAAiB,CAC5B,GACA,mBACG;AACH,MAAI,MAAM,KAAM;AAChB,MAAI,SAAS;AACb,MAAI,eAAe,UAAU,OAAO,WAAW,YAAY,WAAW,IAAI;AAClE,UAAA,SAAS,OAAO,WAAW,MAAM;AACvC,aAAS,OAAO,MAAM,MAAM,IAAI,SAAS;AAAA,EAAA;AAEpC,SAAA;AACT;ACKO,MAAM,eAAe,CAC1B,OACA,OAGA,YACA,mBACG;;AACG,QAAA,iBAAiB,IAAI,CAAC;AAE5B,QAAM,aAAa,MAAM,MAAM,MAAM,IAAI,OAAO;AAChD,QAAM,iBAAiB,YAAY,MAAM,MAAM,YAAY,CAAC;AAC5D,QAAM,wBAAwB,YAAY,MAAM,MAAM,mBAAmB,GAAG;AAG5E,QAAM,iBAAgB,YAAO,oBAAoB,IAAI,MAA/B,mBAAmC;AACzD,QAAM,gBAAgB;AAAA,IAAS,MAC7B,MAAM,UAAU,SAAY,MAAM,SAAS,+CAAe,MAAM,UAAS;AAAA,EAC3E;AACA,QAAM,sBAAsB,eAAe,MAAM,MAAM,aAAa,aAAa;AAC3E,QAAA,aAAa,cAAc,aAAa;AAE9C,QAAM,2BAA2B;AAAA,IAC/B,CAAC,UAAqB;AACpB,iBAAW,QAAQ;AAAA,IACrB;AAAA,IACA,MAAO,eAAe,SAAS,OAAO,IAAI,eAAe;AAAA,IACzD,EAAC,SAAS,MAAO,eAAe,SAAS,OAAO,MAAM,sBAAsB,MAAM;AAAA,EACpF;AAEA,QAAM,mBAAmB,CAAC,OAAkB,QAAQ,UAAU;AAC5D,QAAI,eAAe,SAAS,QAAQ,UAAU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