RangeInput
Enter a numeric or text range inside one border. Either endpoint may remain empty. In number mode, reversed endpoints swap when focus leaves the entire component.
Import
import { HuiRangeInput } from '@nscode/handy-ui'
import type { RangeInputValue, RangeInputProps, RangeInputExpose } from '@nscode/handy-ui'
Internal state and controlled updates
Omitting modelValue, including with an update listener only, stores both endpoints internally from an empty tuple. A passed prop, including undefined, remains controlled; rejected edits restore the parent value. Text and numeric modes use the same tuple and retain it when the type changes. Numeric autoSwap runs on leaving the whole control. Clear follows the update, clear, change order.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiRangeInput, HuiButton, HuiSpace, HuiRadioGroup, HuiRadio } from '@nscode/handy-ui'
import type { RangeInputValue } from '@nscode/handy-ui'
const q1Redraw = ref(0)
const q1Last = ref('No request')
const q1Type = ref<'number' | 'text'>('number')
const q1Model = ref<RangeInputValue>([undefined, undefined])
</script>
<template>
<div class="range-input-q1-demo">
<HuiSpace
><HuiButton class="q1-redraw" @click="q1Redraw++">Parent redraw {{ q1Redraw }}</HuiButton
><HuiRadioGroup v-model="q1Type"
><HuiRadio value="number">Number</HuiRadio
><HuiRadio value="text">Text</HuiRadio></HuiRadioGroup
></HuiSpace
><HuiSpace direction="vertical" fill>
<div>
<p>Omitted prop: internal state</p>
<div class="q1-local"><HuiRangeInput :type="q1Type" clearable /></div>
</div>
<div>
<p>Update listener only: internal state</p>
<div class="q1-listener">
<HuiRangeInput
:type="q1Type"
clearable
@update:model-value="q1Last = JSON.stringify($event)"
/>
</div>
</div>
<div>
<p>Explicit undefined: controlled</p>
<div class="q1-empty">
<HuiRangeInput :type="q1Type" clearable :model-value="undefined" />
</div>
</div>
<div>
<p>Fixed controlled value: parent rejects</p>
<div class="q1-fixed">
<HuiRangeInput
:type="q1Type"
clearable
:model-value="[1, 9]"
@update:model-value="q1Last = JSON.stringify($event)"
/>
</div>
</div>
<div>
<p>Two-way binding</p>
<div class="q1-bound">
<HuiRangeInput :type="q1Type" clearable v-model="q1Model" />
</div>
</div>
<p class="q1-notification">
Bound value: {{ q1Model }}; latest request: {{ q1Last }}
</p></HuiSpace
>
</div>
</template>
Examples
Amount and numeric constraints
The default type="number" hides step buttons and keeps arrow key stepping. min, max, step, and precision apply to both endpoints. Numeric edits commit on native change, usually Enter or blur after editing.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiRangeInput, HuiSpace } from '@nscode/handy-ui'
import type { RangeInputValue } from '@nscode/handy-ui'
const amount = ref<RangeInputValue>([100, 500])
</script>
<template>
<HuiSpace direction="vertical" fill>
<span>Amount: 0–10000, step 0.5, two decimal places</span>
<HuiRangeInput
v-model="amount"
:min="0"
:max="10000"
:step="0.5"
:precision="2"
aria-label="Amount range"
/>
<span>Min {{ amount[0] ?? 'Unbounded' }} / Max {{ amount[1] ?? 'Unbounded' }}</span>
</HuiSpace>
</template>
Text codes and separator text
type="text" preserves text and never sorts endpoints. separator accepts custom text; an empty string removes the text.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiRangeInput, HuiSpace } from '@nscode/handy-ui'
import type { RangeInputValue } from '@nscode/handy-ui'
const codes = ref<RangeInputValue>(['A-001', 'A-099'])
const emptySeparator = ref<RangeInputValue>(['B-001', 'B-099'])
</script>
<template>
<HuiSpace direction="vertical" fill>
<span>type=text, separator=~</span>
<HuiRangeInput
v-model="codes"
type="text"
separator="~"
:placeholder="['First code', 'Last code']"
aria-label="Code range"
/>
<span>separator=empty string</span>
<HuiRangeInput
v-model="emptySeparator"
type="text"
separator=""
aria-label="Code range without separator text"
/>
<span>{{ codes[0] ?? 'Empty' }} ~ {{ codes[1] ?? 'Empty' }}</span>
</HuiSpace>
</template>
All sizes and placeholders
Use a shared placeholder or a two-item tuple. An empty string stays empty. Size inherits from InputGroup, FormItem, Form, then global configuration when omitted.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiRangeInput, HuiSpace } from '@nscode/handy-ui'
import type { ComponentSize, RangeInputValue } from '@nscode/handy-ui'
const sizes: ComponentSize[] = ['large', 'default', 'small']
const values = ref<RangeInputValue[]>([
[undefined, undefined],
[undefined, undefined],
[undefined, undefined]
])
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace v-for="(size, index) in sizes" :key="size" direction="vertical" fill>
<span>{{ size }}</span>
<HuiRangeInput
v-model="values[index]"
:size="size"
:placeholder="
index === 0 ? 'Enter a number' : index === 1 ? ['Lower bound', 'Upper bound'] : ''
"
:aria-label="`${size} range`"
/>
</HuiSpace>
</HuiSpace>
</template>
Disabled and shared clear button
clearable enables the shared button when a value exists and the range is editable. Clearing emits one empty tuple and a clear event, then focuses the first input. The component does not open a confirmation dialog.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiRangeInput, HuiSwitch, HuiSpace } from '@nscode/handy-ui'
import type { RangeInputValue } from '@nscode/handy-ui'
const value = ref<RangeInputValue>([0, 100])
const disabled = ref(false)
const clearable = ref(true)
const clears = ref(0)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace
><span>disabled</span><HuiSwitch v-model="disabled" /><span>clearable</span
><HuiSwitch v-model="clearable"
/></HuiSpace>
<HuiRangeInput
v-model="value"
:disabled="disabled"
:clearable="clearable"
aria-label="Clearable range"
@clear="clears++"
/>
<span
>Value {{ value[0] ?? 'Empty' }} / {{ value[1] ?? 'Empty' }}; cleared {{ clears }} times</span
>
</HuiSpace>
</template>
Auto swap and open ranges
Moving between inputs does not swap. Move focus outside the entire component to apply sorting. Set :auto-swap="false" to keep the original order. An empty endpoint represents an open bound.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiRangeInput, HuiSpace, HuiButton } from '@nscode/handy-ui'
import type { RangeInputValue } from '@nscode/handy-ui'
const auto = ref<RangeInputValue>([90, 10])
const manual = ref<RangeInputValue>([90, 10])
const lowerOnly = ref<RangeInputValue>([50, undefined])
const upperOnly = ref<RangeInputValue>([undefined, 200])
</script>
<template>
<HuiSpace direction="vertical" fill>
<span>autoSwap=true: {{ auto[0] }} / {{ auto[1] }}</span
><HuiRangeInput v-model="auto" aria-label="Auto swap" />
<span>autoSwap=false: {{ manual[0] }} / {{ manual[1] }}</span
><HuiRangeInput v-model="manual" :auto-swap="false" aria-label="Keep original order" />
<span>Lower bound only</span><HuiRangeInput v-model="lowerOnly" aria-label="Lower bound only" />
<span>Upper bound only</span><HuiRangeInput v-model="upperOnly" aria-label="Upper bound only" />
<HuiButton>Move focus outside</HuiButton>
</HuiSpace>
</template>
Events and focus methods
focus and blur fire for each endpoint with its original index, 0 or 1. change fires for an actual edit, swap, or clear; the underlying input change is not emitted twice. Text mode emits on each input edit; number mode commits on native change.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiRangeInput, HuiSpace, HuiButton } from '@nscode/handy-ui'
import type { RangeInputExpose, RangeInputValue, RangeInputIndex } from '@nscode/handy-ui'
const range = ref<RangeInputExpose>()
const value = ref<RangeInputValue>([1, 9])
const event = ref('Waiting for input')
const describe = (name: string, e: FocusEvent, index: RangeInputIndex) => {
event.value = `${name}: input ${index + 1}, native event ${e.type}`
}
const changed = (next: RangeInputValue) => {
event.value = `change: ${next[0] ?? 'Empty'} / ${next[1] ?? 'Empty'}`
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiRangeInput
ref="range"
v-model="value"
clearable
aria-label="Range events"
@change="changed"
@focus="(e, index) => describe('focus', e, index)"
@blur="(e, index) => describe('blur', e, index)"
@clear="event = 'clear: both endpoints cleared'"
/>
<HuiSpace
><HuiButton @click="range?.focus()">focus()</HuiButton
><HuiButton @click="range?.focus(1)">focus(1)</HuiButton
><HuiButton @mousedown.prevent @click="range?.blur()">blur()</HuiButton></HuiSpace
>
<span aria-live="polite">{{ event }}</span>
</HuiSpace>
</template>
Public clear() and the icon switch
Call clear() through the parent ref. The method shares the icon's update path; clearable controls only icon visibility. A range with at least one actual nonempty endpoint requests [undefined, undefined] in update:modelValue → clear → change order, with separate event arrays. Disabled, form-readonly and unmounted controls ignore the method. An actually empty range is a no-op; uncommitted invalid numeric text on an empty range is outside this operation.
This example keeps a fixed parent value, rejects updates and counts requests without removing data. Business actions that remove records or associations must confirm first; the library method has no built-in confirmation. Accepted or normalized parent values determine the display, while rejected requests keep the actual range. Normal clearing focuses the first endpoint, but a callback that moves focus or unmounts the component takes precedence.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiRangeInput, HuiButton, HuiSpace, HuiSwitch } from '@nscode/handy-ui'
import type { RangeInputExpose } from '@nscode/handy-ui'
const range = ref<RangeInputExpose>()
const clearable = ref(false)
const disabled = ref(false)
const requests = ref(0)
const order = ref<string[]>([])
const request = () => {
requests.value++
order.value = ['update']
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<span>The fixed parent rejects updates; only requests are counted. No data is removed.</span>
<HuiSpace>
<span>Clear icon</span><HuiSwitch v-model="clearable" /> <span>Disabled</span
><HuiSwitch v-model="disabled" />
</HuiSpace>
<HuiRangeInput
ref="range"
:model-value="[1, 9]"
:clearable="clearable"
:disabled="disabled"
aria-label="Public clear method range"
@update:model-value="request"
@clear="order.push('clear')"
@change="order.push('change')"
/>
<HuiButton :disabled="disabled" @click="range?.clear()">Request clear()</HuiButton>
<span role="status"
>Requests: {{ requests }};{{ order.join(' → ') }};Actual value: 1 / 9</span
>
</HuiSpace>
</template>
Custom separator slot
<script setup lang="ts">
import { ref } from 'vue'
import { HuiRangeInput, HuiIcon } from '@nscode/handy-ui'
import type { RangeInputValue } from '@nscode/handy-ui'
const value = ref<RangeInputValue>([10, 20])
</script>
<template>
<HuiRangeInput v-model="value" aria-label="Range with separator icon"
><template #separator><HuiIcon name="arrow-right" /></template
></HuiRangeInput>
</template>
Form validation and readonly inheritance
The range registers once with FormItem. Its label targets the first input; each endpoint name combines that label and Start/End, and both share error descriptions. Validation errors turn the outer range border red. A required rule requires at least one endpoint; use the custom validator below to require both. :validate-event="false" disables automatic validation; explicit form.validate() still works. Readonly inherits from Form or FormItem. FormItem readonly=false overrides an outer readonly state.
<script setup lang="ts">
import { reactive, ref } from 'vue'
import {
HuiRangeInput,
HuiForm,
HuiFormItem,
HuiSwitch,
HuiSpace,
HuiButton
} from '@nscode/handy-ui'
import type { FormRule, RangeInputValue } from '@nscode/handy-ui'
const form = ref<InstanceType<typeof HuiForm>>()
const model = reactive({
budget: [100, 500] as RangeInputValue,
editable: [1, 9] as RangeInputValue
})
const readonly = ref(false)
const validateEvent = ref(true)
const result = ref('Not validated')
const rules: FormRule[] = [
{
trigger: ['change', 'blur'],
validator: (_rule, value) =>
Array.isArray(value) && value[0] !== undefined && value[1] !== undefined,
message: 'Enter both budget bounds'
}
]
const check = async () => {
result.value = (await form.value?.validate())?.valid ? 'Valid' : 'Invalid'
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace
><span>Form readonly</span><HuiSwitch v-model="readonly" /><span>validateEvent</span
><HuiSwitch v-model="validateEvent"
/></HuiSpace>
<HuiForm ref="form" :model="model" :readonly="readonly" label-width="100px">
<HuiFormItem label="Budget" prop="budget" :rules="rules"
><HuiRangeInput
id="budget-range"
v-model="model.budget"
clearable
:validate-event="validateEvent"
/></HuiFormItem>
<HuiFormItem label="Exception" prop="editable" :readonly="false"
><HuiRangeInput v-model="model.editable" clearable
/></HuiFormItem>
</HuiForm>
<HuiButton @click="check">Validate manually</HuiButton><span>{{ result }}</span>
</HuiSpace>
</template>
SearchForm integration
Place one FormItem for the tuple inside the SearchForm default slot. Search and reset results are shown on the page.
<script setup lang="ts">
import { reactive, ref } from 'vue'
import { HuiRangeInput, HuiSearchForm, HuiFormItem, HuiSpace } from '@nscode/handy-ui'
import type { RangeInputValue } from '@nscode/handy-ui'
const query = reactive({
price: [undefined, undefined] as RangeInputValue,
code: ['', ''] as RangeInputValue
})
const result = ref('No search yet')
const search = (source: string) => {
result.value = `${source}: price ${query.price[0] ?? 'Any'}–${query.price[1] ?? 'Any'}; code ${query.code[0] || 'Any'}–${query.code[1] || 'Any'}`
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSearchForm :model="query" @search="search('Search')" @reset="search('Reset')">
<HuiFormItem label="Price" prop="price"
><HuiRangeInput v-model="query.price" :min="0" clearable
/></HuiFormItem>
<HuiFormItem label="Code" prop="code"
><HuiRangeInput v-model="query.code" type="text" clearable
/></HuiFormItem>
</HuiSearchForm>
<span>{{ result }}</span>
</HuiSpace>
</template>
Props
| Prop | Type / options | Default | Description |
|---|---|---|---|
| modelValue / v-model | RangeInputValue | undefined | Tuple with optional endpoints |
| type | 'number' | 'text' | 'number' | Numeric or text range |
| separator | string | Localized “to” | Separator text; empty string supported; slot takes precedence |
| placeholder | string | [string, string] | Localized Min/Max for numbers, Start/End for text | Shared or separate endpoint placeholders |
| min | number | Unbounded | Minimum numeric value for both endpoints |
| max | number | Unbounded | Maximum numeric value for both endpoints |
| step | number | 1 | Numeric arrow key step |
| precision | number | Unrestricted | Decimal places in number mode |
| autoSwap | boolean | true | Swap reversed numbers when focus leaves the whole component; no effect on text |
| clearable | boolean | false | Enable the shared clear button |
| disabled | boolean | false | Disable editing; also inherits from groups and forms |
| size | 'large' | 'default' | 'small' | Inherited | Control size |
| validateEvent | boolean | true | Automatic change and whole-component blur validation |
Events
| Event | Arguments | Description |
|---|---|---|
| update:modelValue | (value: RangeInputValue) | Request a new tuple |
| change | (value: RangeInputValue) | Actual edit, swap, or clear; text mode emits on each input edit, number mode on native change |
| focus | (event: FocusEvent, index: 0 | 1) | Endpoint gains focus |
| blur | (event: FocusEvent, index: 0 | 1) | Endpoint loses focus, including moves within the range |
| clear | () | User clears the entire range |
Slots
| Slot | Parameters | Description |
|---|---|---|
| separator | None | Replace separator text; wrapper is hidden from screen readers, so use noninteractive content |
Methods
| Method | Type | Description |
|---|---|---|
| focus | (index?: 0 | 1) => void | Focus endpoint 0 by default; disabled controls ignore it |
| blur | () => void | Blur the current focus inside the component, including the clear button |
| clear | () => void | Request an empty tuple for an actually nonempty range; independent of clearable, ignored when disabled, form-readonly or unmounted |
Types
type RangeInputValue = [string | number | undefined, string | number | undefined]
type RangeInputType = 'number' | 'text'
type RangeInputIndex = 0 | 1
interface RangeInputExpose {
focus: (index?: RangeInputIndex) => void
blur: () => void
clear: () => void
}