UNPKG

reka-ui

Version:

Vue port for Radix UI Primitives.

1 lines 12.1 kB
{"version":3,"file":"NumberFieldRoot.cjs","sources":["../../src/NumberField/NumberFieldRoot.vue"],"sourcesContent":["<script lang=\"ts\">\nimport type { PrimitiveProps } from '@/Primitive'\nimport { useVModel } from '@vueuse/core'\nimport { clamp, createContext, snapValueToStep, useFormControl, useLocale } from '@/shared'\nimport { type HTMLAttributes, type Ref, computed, ref, toRefs } from 'vue'\nimport type { FormFieldProps } from '@/shared/types'\n\nexport interface NumberFieldRootProps extends PrimitiveProps, FormFieldProps {\n defaultValue?: number\n modelValue?: number | null\n /** The smallest value allowed for the input. */\n min?: number\n /** The largest value allowed for the input. */\n max?: number\n /** The amount that the input value changes with each increment or decrement \"tick\". */\n step?: number\n /** When `false`, prevents the value from snapping to the nearest increment of the step value */\n stepSnapping?: boolean\n /** Formatting options for the value displayed in the number field. This also affects what characters are allowed to be typed by the user. */\n formatOptions?: Intl.NumberFormatOptions\n /** The locale to use for formatting dates */\n locale?: string\n /** When `true`, prevents the user from interacting with the Number Field. */\n disabled?: boolean\n /** When `true`, prevents the value from changing on wheel scroll. */\n disableWheelChange?: boolean\n /** Id of the element */\n id?: string\n}\n\nexport type NumberFieldRootEmits = {\n 'update:modelValue': [val: number]\n}\n\ninterface NumberFieldRootContext {\n modelValue: Ref<number>\n handleIncrease: (multiplier?: number) => void\n handleDecrease: (multiplier?: number) => void\n handleMinMaxValue: (type: 'min' | 'max') => void\n inputEl: Ref<HTMLInputElement | undefined>\n onInputElement: (el: HTMLInputElement) => void\n inputMode: Ref<HTMLAttributes['inputmode']>\n textValue: Ref<string>\n validate: (val: string) => boolean\n applyInputValue: (val: string) => void\n disabled: Ref<boolean>\n disableWheelChange: Ref<boolean>\n max: Ref<number | undefined>\n min: Ref<number | undefined>\n isDecreaseDisabled: Ref<boolean>\n isIncreaseDisabled: Ref<boolean>\n id: Ref<string | undefined>\n}\n\nexport const [injectNumberFieldRootContext, provideNumberFieldRootContext] = createContext<NumberFieldRootContext>('NumberFieldRoot')\n</script>\n\n<script setup lang=\"ts\">\nimport { Primitive, usePrimitiveElement } from '@/Primitive'\nimport { handleDecimalOperation, useNumberFormatter, useNumberParser } from './utils'\nimport { VisuallyHiddenInput } from '@/VisuallyHidden'\n\ndefineOptions({\n inheritAttrs: false,\n})\n\nconst props = withDefaults(defineProps<NumberFieldRootProps>(), {\n as: 'div',\n defaultValue: undefined,\n step: 1,\n stepSnapping: true,\n})\nconst emits = defineEmits<NumberFieldRootEmits>()\nconst { disabled, disableWheelChange, min, max, step, stepSnapping, formatOptions, id, locale: propLocale } = toRefs(props)\n\nconst modelValue = useVModel(props, 'modelValue', emits, {\n defaultValue: props.defaultValue,\n passive: (props.modelValue === undefined) as false,\n}) as Ref<number>\n\nconst { primitiveElement, currentElement } = usePrimitiveElement()\n\nconst locale = useLocale(propLocale)\nconst isFormControl = useFormControl(currentElement)\nconst inputEl = ref<HTMLInputElement>()\n\nconst isDecreaseDisabled = computed(() => (\n clampInputValue(modelValue.value) === min.value\n || (min.value && !isNaN(modelValue.value) ? (handleDecimalOperation('-', modelValue.value, step.value) < min.value) : false)),\n)\nconst isIncreaseDisabled = computed(() => (\n clampInputValue(modelValue.value) === max.value\n || (max.value && !isNaN(modelValue.value) ? (handleDecimalOperation('+', modelValue.value, step.value) > max.value) : false)),\n)\n\nfunction handleChangingValue(type: 'increase' | 'decrease', multiplier = 1) {\n inputEl.value?.focus()\n const currentInputValue = numberParser.parse(inputEl.value?.value ?? '')\n if (props.disabled)\n return\n if (isNaN(currentInputValue)) {\n modelValue.value = min.value ?? 0\n }\n else {\n if (type === 'increase')\n modelValue.value = clampInputValue(currentInputValue + ((step.value ?? 1) * multiplier))\n else\n modelValue.value = clampInputValue(currentInputValue - ((step.value ?? 1) * multiplier))\n }\n}\n\nfunction handleIncrease(multiplier = 1) {\n handleChangingValue('increase', multiplier)\n}\nfunction handleDecrease(multiplier = 1) {\n handleChangingValue('decrease', multiplier)\n}\n\nfunction handleMinMaxValue(type: 'min' | 'max') {\n if (type === 'min' && min.value !== undefined)\n modelValue.value = clampInputValue(min.value)\n else if (type === 'max' && max.value !== undefined)\n modelValue.value = clampInputValue(max.value)\n}\n\n// Formatter\nconst numberFormatter = useNumberFormatter(locale, formatOptions)\nconst numberParser = useNumberParser(locale, formatOptions)\n\nconst inputMode = computed<HTMLAttributes['inputmode']>(() => {\n // The inputMode attribute influences the software keyboard that is shown on touch devices.\n // Browsers and operating systems are quite inconsistent about what keys are available, however.\n // We choose between numeric and decimal based on whether we allow negative and fractional numbers,\n // and based on testing on various devices to determine what keys are available in each inputMode.\n const hasDecimals = numberFormatter.resolvedOptions().maximumFractionDigits! > 0\n\n return hasDecimals ? 'decimal' : 'numeric'\n})\n// Replace negative textValue formatted using currencySign: 'accounting'\n// with a textValue that can be announced using a minus sign.\nconst textValueFormatter = useNumberFormatter(locale, formatOptions)\nconst textValue = computed(() => isNaN(modelValue.value) ? '' : textValueFormatter.format(modelValue.value))\n\nfunction validate(val: string) {\n return numberParser.isValidPartialNumber(val, min.value, max.value)\n}\n\nfunction setInputValue(val: string) {\n if (inputEl.value)\n inputEl.value.value = val\n}\n\nfunction clampInputValue(val: number) {\n // Clamp to min and max, round to the nearest step, and round to specified number of digits\n let clampedValue: number\n if (step.value === undefined || isNaN(step.value) || !stepSnapping.value)\n clampedValue = clamp(val, min.value, max.value)\n else\n clampedValue = snapValueToStep(val, min.value, max.value, step.value)\n\n clampedValue = numberParser.parse(numberFormatter.format(clampedValue))\n return clampedValue\n}\n\nfunction applyInputValue(val: string) {\n const parsedValue = numberParser.parse(val)\n\n modelValue.value = clampInputValue(parsedValue)\n // Set to empty state if input value is empty\n if (!val.length)\n return setInputValue(val)\n\n // if it failed to parse, then reset input to formatted version of current number\n if (isNaN(parsedValue))\n return setInputValue(textValue.value)\n\n return setInputValue(textValue.value)\n}\n\nprovideNumberFieldRootContext({\n modelValue,\n handleDecrease,\n handleIncrease,\n handleMinMaxValue,\n inputMode,\n inputEl,\n onInputElement: el => inputEl.value = el,\n textValue,\n validate,\n applyInputValue,\n disabled,\n disableWheelChange,\n max,\n min,\n isDecreaseDisabled,\n isIncreaseDisabled,\n id,\n})\n</script>\n\n<template>\n <Primitive\n v-bind=\"$attrs\"\n ref=\"primitiveElement\"\n role=\"group\"\n :as=\"as\"\n :as-child=\"asChild\"\n :data-disabled=\"disabled ? '' : undefined\"\n >\n <slot\n :model-value=\"modelValue\"\n :text-value=\"textValue\"\n />\n\n <VisuallyHiddenInput\n v-if=\"isFormControl && name\"\n type=\"text\"\n :value=\"modelValue\"\n :name=\"name\"\n :disabled=\"disabled\"\n :required=\"required\"\n />\n </Primitive>\n</template>\n"],"names":["createContext","toRefs","useVModel","usePrimitiveElement","useLocale","useFormControl","ref","computed","handleDecimalOperation","useNumberFormatter","useNumberParser","clamp","snapValueToStep"],"mappings":";;;;;;;;;;;;;AAsDO,MAAM,CAAC,4BAAA,EAA8B,6BAA6B,CAAA,GAAIA,mCAAsC,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;AAYpI,IAAA,MAAM,KAAQ,GAAA,OAAA;AAMd,IAAA,MAAM,KAAQ,GAAA,MAAA;AACd,IAAA,MAAM,EAAE,QAAA,EAAU,kBAAoB,EAAA,GAAA,EAAK,GAAK,EAAA,IAAA,EAAM,YAAc,EAAA,aAAA,EAAe,EAAI,EAAA,MAAA,EAAQ,UAAW,EAAA,GAAIC,WAAO,KAAK,CAAA;AAE1H,IAAA,MAAM,UAAa,GAAAC,cAAA,CAAU,KAAO,EAAA,YAAA,EAAc,KAAO,EAAA;AAAA,MACvD,cAAc,KAAM,CAAA,YAAA;AAAA,MACpB,OAAA,EAAU,MAAM,UAAe,KAAA;AAAA,KAChC,CAAA;AAED,IAAA,MAAM,EAAE,gBAAA,EAAkB,cAAe,EAAA,GAAIC,iDAAoB,EAAA;AAEjE,IAAM,MAAA,MAAA,GAASC,2BAAU,UAAU,CAAA;AACnC,IAAM,MAAA,aAAA,GAAgBC,qCA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