Files
tools/vue/primitives/src/number-field/NumberFieldInput.vue
T
robonen 626fbc70d8 fix(primitives): eslint/tsconfig migration, asChild refactor, type fixes
- Migrate to eslint flat config + composite tsconfig.
- Complete the asChild→as="template" refactor (remove asChild prop + :as-child
  bindings across components, matching Primitive's slot model).
- Fix test type errors and source type-safety (useGraceArea hull/point math,
  FocusScope/util ref typing).

Note: ~53 vue-tsc errors remain (HTML attr/event passthrough typing on
transparent wrapper components + a couple of duplicate-export naming
collisions) — not gated by CI (build/lint/test green); pending a
component-attribute-typing design decision.
2026-06-07 16:29:56 +07:00

88 lines
2.2 KiB
Vue

<script lang="ts">
import type { PrimitiveProps } from '../primitive';
export interface NumberFieldInputProps extends PrimitiveProps {
placeholder?: string;
name?: string;
required?: boolean;
}
</script>
<script setup lang="ts">
import { computed } from 'vue';
import { useNumberFieldContext } from './context';
const props = defineProps<NumberFieldInputProps>();
const ctx = useNumberFieldContext();
const displayValue = computed(() => ctx.value.value === null ? '' : String(ctx.value.value));
function parse(raw: string): number | null {
const trimmed = raw.trim();
if (trimmed === '') return null;
const n = Number(trimmed);
return Number.isFinite(n) ? n : null;
}
function onInput(event: Event): void {
const target = event.target as HTMLInputElement;
ctx.setValue(parse(target.value));
}
function onKeyDown(event: KeyboardEvent): void {
if (ctx.disabled.value || ctx.readonly.value) return;
switch (event.key) {
case 'ArrowUp':
event.preventDefault();
ctx.increment();
break;
case 'ArrowDown':
event.preventDefault();
ctx.decrement();
break;
case 'PageUp':
event.preventDefault();
ctx.increment(ctx.step.value * 10);
break;
case 'PageDown':
event.preventDefault();
ctx.decrement(ctx.step.value * 10);
break;
case 'Home':
if (ctx.min.value !== undefined) {
event.preventDefault();
ctx.setValue(ctx.min.value);
}
break;
case 'End':
if (ctx.max.value !== undefined) {
event.preventDefault();
ctx.setValue(ctx.max.value);
}
break;
}
}
</script>
<template>
<input
:id="ctx.inputId"
role="spinbutton"
inputmode="decimal"
autocomplete="off"
:aria-valuemin="ctx.min.value"
:aria-valuemax="ctx.max.value"
:aria-valuenow="ctx.value.value ?? undefined"
:aria-disabled="ctx.disabled.value || undefined"
:aria-readonly="ctx.readonly.value || undefined"
:disabled="ctx.disabled.value || undefined"
:readonly="ctx.readonly.value || undefined"
:placeholder="props.placeholder"
:name="props.name"
:required="props.required || undefined"
:value="displayValue"
@input="onInput"
@keydown="onKeyDown"
>
</template>