AK Input Number
ak-ui/input-numberFreeComponent
Accessible numeric input with increment, decrement, minimum, and maximum actions.
Install it
npx shadcn@latest add https://ak-ui.yunyoujun.cn/r/input-number.jsonSource
1<script setup lang="ts">2import { computed, watch } from 'vue'34import '@yunyoujun/ak-ui'56const props = withDefaults(defineProps<{7 disabled?: boolean8 label?: string9 max?: number10 maxLabel?: string11 min?: number12 minLabel?: string13 step?: number14}>(), {15 disabled: false,16 label: '数值',17 max: 99,18 maxLabel: '最多',19 min: 0,20 minLabel: '最少',21 step: 1,22})2324const model = defineModel<number>({ default: 0 })25const normalizedValue = computed(() => clamp(model.value))26const normalizedStep = computed(() => Math.abs(props.step) || 1)2728function clamp(value: number) {29 return Math.min(props.max, Math.max(props.min, value))30}3132function updateValue(value: number) {33 if (!props.disabled)34 model.value = clamp(value)35}3637watch([model, () => props.min, () => props.max], ([value]) => {38 const nextValue = clamp(value)3940 if (nextValue !== value)41 model.value = nextValue42}, { immediate: true })4344function onInput(event: Event) {45 const input = event.target as HTMLInputElement46 const value = Number(input.value)4748 if (Number.isNaN(value))49 return5051 const nextValue = clamp(value)52 updateValue(nextValue)5354 if (nextValue !== value)55 input.value = String(nextValue)56}57</script>5859<template>60 <div61 class="ak-input-number"62 data-slot="ak-input-number"63 :data-disabled="disabled || undefined"64 >65 <button66 type="button"67 class="ak-input-number__increase"68 aria-label="增加"69 :disabled="disabled || normalizedValue >= max"70 @click="updateValue(normalizedValue + normalizedStep)"71 >72 <svg class="ak-icon" viewBox="0 0 24 24" aria-hidden="true">73 <path d="M11 5h2v14h-2zM5 11h14v2H5z" />74 </svg>75 </button>76 <button77 type="button"78 class="ak-input-number__decrease"79 aria-label="减少"80 :disabled="disabled || normalizedValue <= min"81 @click="updateValue(normalizedValue - normalizedStep)"82 >83 <svg class="ak-icon" viewBox="0 0 24 24" aria-hidden="true">84 <path d="M5 11h14v2H5z" />85 </svg>86 </button>87 <button88 type="button"89 class="ak-input-number__max"90 :disabled="disabled || normalizedValue >= max"91 @click="updateValue(max)"92 >93 {{ maxLabel }}94 </button>95 <button96 type="button"97 class="ak-input-number__min"98 :disabled="disabled || normalizedValue <= min"99 @click="updateValue(min)"100 >101 {{ minLabel }}102 </button>103 <input104 class="ak-input-number__inner"105// … truncated
What it pulls in
npm packages
Files it writes
More from ak-ui
All 7 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| AK Buttonbutton | ak-ui | Components | Free | 1 dep · 3 files | |
| AK Cardcard | ak-ui | Components | Free | 1 dep · 8 files | |
| AK Noticenotice | ak-ui | Components | Free | 1 dep · 3 files | |
| AK Progressprogress | ak-ui | Components | Free | 1 dep · 4 files | |
| AK Statusstatus | ak-ui | Components | Free | 1 dep · 4 files | |
| AK Tabstabs | ak-ui | Components | Free | 1 dep · 4 files |
