Skip to content

Commit 87a6483

Browse files
author
Pavlo Kulyk
committed
fix: enhance input handling with min/max validation and refactor input event logic
1 parent f48a6ca commit 87a6483

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

adminforth/spa/src/afcl/Input.vue

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
ref="input"
1313
v-bind="$attrs"
1414
:type="type"
15-
@input="$emit('update:modelValue', type === 'number' ? Number(($event.target as HTMLInputElement)?.value) : ($event.target as HTMLInputElement)?.value)"
15+
:min="min"
16+
:max="max"
17+
@input="onInput"
1618
:value="modelValue"
1719
aria-describedby="helper-text-explanation"
1820
class="afcl-input inline-flex bg-lightInputBackground text-lightInputText dark:text-darkInputText border border-lightInputBorder rounded-0 focus:ring-lightPrimary focus:border-lightPrimary dark:focus:ring-darkPrimary dark:focus:border-darkPrimary
@@ -48,8 +50,26 @@ const props = defineProps<{
4850
suffix?: string,
4951
prefix?: string,
5052
readonly?: boolean,
53+
min?: number | null,
54+
max?: number | null,
5155
}>()
5256
57+
const emit = defineEmits<{
58+
(e: 'update:modelValue', value: number | null): void
59+
}>();
60+
61+
const onInput = (e: Event) => {
62+
const el = e.target as HTMLInputElement;
63+
let val = Number(el.value);
64+
65+
if (props.min !== null && props.min !== undefined && val < props.min) val = props.min;
66+
if (props.max !== null && props.max !== undefined && val > props.max) val = props.max;
67+
68+
el.value = String(val);
69+
emit('update:modelValue', val);
70+
};
71+
72+
5373
const input = ref<HTMLInputElement | null>(null)
5474
5575
defineExpose({

0 commit comments

Comments
 (0)