RangeInput 範圍輸入
在一個邊框內輸入數值或文字範圍。兩端可以獨立留空;數值模式離開整個元件時,預設交換倒置的上下限。
引入
import { HuiRangeInput } from '@nscode/handy-ui'
import type { RangeInputValue, RangeInputProps, RangeInputExpose } from '@nscode/handy-ui'
自持狀態與受控回填
未傳 modelValue 或僅監聽更新時,從雙端空值自行儲存輸入;傳入屬性(含 undefined)即受控,父元件拒絕時恢復父值。文字與數值模式共用同一狀態,切換類型保留原 tuple;數值模式離開整個元件時依 autoSwap 交換,清除按統一的 update→clear→change 順序通知。
<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('尚無請求')
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++">父重新呈現 {{ q1Redraw }}</HuiButton
><HuiRadioGroup v-model="q1Type"
><HuiRadio value="number">數值</HuiRadio
><HuiRadio value="text">文字</HuiRadio></HuiRadioGroup
></HuiSpace
><HuiSpace direction="vertical" fill>
<div>
<p>未傳屬性,自行保存</p>
<div class="q1-local"><HuiRangeInput :type="q1Type" clearable /></div>
</div>
<div>
<p>僅監聽更新,仍自行保存</p>
<div class="q1-listener">
<HuiRangeInput
:type="q1Type"
clearable
@update:model-value="q1Last = JSON.stringify($event)"
/>
</div>
</div>
<div>
<p>顯式undefined,父值優先</p>
<div class="q1-empty">
<HuiRangeInput :type="q1Type" clearable :model-value="undefined" />
</div>
</div>
<div>
<p>固定受控,父拒絕更新</p>
<div class="q1-fixed">
<HuiRangeInput
:type="q1Type"
clearable
:model-value="[1, 9]"
@update:model-value="q1Last = JSON.stringify($event)"
/>
</div>
</div>
<div>
<p>雙向綁定</p>
<div class="q1-bound">
<HuiRangeInput :type="q1Type" clearable v-model="q1Model" />
</div>
</div>
<p class="q1-notification">綁定值:{{ q1Model }};最近請求:{{ q1Last }}</p></HuiSpace
>
</div>
</template>
示例
金額範圍與數值約束
type="number" 為預設模式,隱藏步進按鈕,仍支援方向鍵調整。min、max、step、precision 同時作用於兩端。數值在原生 change 時提交,通常發生在Enter 或修改後失焦時。
<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>金額:0~10000,步長 0.5,兩位小數</span>
<HuiRangeInput
v-model="amount"
:min="0"
:max="10000"
:step="0.5"
:precision="2"
aria-label="金額範圍"
/>
<span>下限 {{ amount[0] ?? '不限' }} / 上限 {{ amount[1] ?? '不限' }}</span>
</HuiSpace>
</template>
文字編號與分隔文字
type="text" 保留文字,不執行大小排序;separator 支援任意文字,空字串表示不顯示分隔文字。
<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="['開始編號', '結束編號']"
aria-label="編號範圍"
/>
<span>separator=空字串</span>
<HuiRangeInput
v-model="emptySeparator"
type="text"
separator=""
aria-label="無分隔文字的編號範圍"
/>
<span>{{ codes[0] ?? '未填寫' }} ~ {{ codes[1] ?? '未填寫' }}</span>
</HuiSpace>
</template>
三種尺寸和提示文字
placeholder 可以共用一段文字,或分別設定兩端;空字串保留為空。尺寸未傳時按 InputGroup、FormItem、Form、全域配置的順序繼承。
<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 ? '輸入數值' : index === 1 ? ['下限', '上限'] : ''"
:aria-label="`${size}範圍`"
/>
</HuiSpace>
</HuiSpace>
</template>
禁用與共用清除按鈕
清除按鈕由 clearable 開啟,只在有值且可編輯時出現。一次清除兩端,發出 clear 並回到首框;元件內部不彈確認框。
<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="可清除範圍"
@clear="clears++"
/>
<span>目前 {{ value[0] ?? '空' }} / {{ value[1] ?? '空' }};已清除 {{ clears }} 次</span>
</HuiSpace>
</template>
自動交換與單端範圍
在兩個輸入框之間切換焦點不會交換;點擊元件外部或移走整個元件的焦點後才交換。auto-swap="false" 保留輸入順序;僅填一端表示開放範圍。
<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="自動交換" />
<span>autoSwap=false:{{ manual[0] }} / {{ manual[1] }}</span
><HuiRangeInput v-model="manual" :auto-swap="false" aria-label="保留順序" /> <span>僅下限</span
><HuiRangeInput v-model="lowerOnly" aria-label="僅下限" /> <span>僅上限</span
><HuiRangeInput v-model="upperOnly" aria-label="僅上限" />
<HuiButton>將焦點移到元件外</HuiButton>
</HuiSpace>
</template>
焦點、變更事件與方法
focus、blur 每端單獨觸發,第二個參數為原始下標 0 或 1。change 在實際值變化、自動交換或清除時觸發,不重複轉發底層 change;文字模式隨每次輸入逐字觸發,數值模式在原生 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('等待操作')
const describe = (name: string, e: FocusEvent, index: RangeInputIndex) => {
event.value = `${name}:第 ${index + 1} 框,原生事件 ${e.type}`
}
const changed = (next: RangeInputValue) => {
event.value = `change:${next[0] ?? '空'} / ${next[1] ?? '空'}`
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiRangeInput
ref="range"
v-model="value"
clearable
aria-label="事件演示範圍"
@change="changed"
@focus="(e, index) => describe('focus', e, index)"
@blur="(e, index) => describe('blur', e, index)"
@clear="event = 'clear:兩端已清除'"
/>
<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>
公開clear()與圖示開關
通過真實父ref呼叫clear(),與清除圖示重用同一更新入口。clearable只控制圖示,關閉圖示不影響方法。實際存在非空端時請求[undefined, undefined],按update:modelValue → clear → change通知;事件陣列各自獨立。禁用、表單唯讀、卸載後或實際兩端已空時無效,本切片不清除空範圍中的未提交無效數值草稿。
以下固定父值拒絕更新,只統計請求與順序,不清除任何資料。實際業務若通過該方法刪除已有記錄或關聯資料,應先在業務入口確認;底層方法本身不彈確認框。父接受或正規化後顯示實際回填;父拒絕時保原範圍。正常清除後回首框,業務回呼已主動移焦或卸載時不搶回。
<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>固定父值拒絕清除,只統計請求,不移除資料。</span>
<HuiSpace>
<span>圖示</span><HuiSwitch v-model="clearable" /> <span>禁用</span
><HuiSwitch v-model="disabled" />
</HuiSpace>
<HuiRangeInput
ref="range"
:model-value="[1, 9]"
:clearable="clearable"
:disabled="disabled"
aria-label="公開清除方法範圍"
@update:model-value="request"
@clear="order.push('clear')"
@change="order.push('change')"
/>
<HuiButton :disabled="disabled" @click="range?.clear()">請求clear()</HuiButton>
<span role="status">請求次數:{{ requests }};{{ order.join(' → ') }};實際值:1 / 9</span>
</HuiSpace>
</template>
自訂分隔插槽
<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="圖示分隔範圍"
><template #separator><HuiIcon name="arrow-right" /></template
></HuiRangeInput>
</template>
表單驗證、唯讀繼承與顯式覆蓋
元件整體關聯一個 FormItem,標籤指向首框;螢幕閱讀器名稱分別包含表單標籤和開始/結束,兩端共享錯誤說明,驗證失敗時整組外框變紅。required 表示至少填寫一端;要求兩端都填寫時使用下例的 validator。validate-event="false" 關閉自動驗證,手動 form.validate() 仍有效。唯讀來自 Form 或 FormItem,FormItem 的 readonly=false 可以覆蓋外層唯讀。
<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('等待驗證')
const rules: FormRule[] = [
{
trigger: ['change', 'blur'],
validator: (_rule, value) =>
Array.isArray(value) && value[0] !== undefined && value[1] !== undefined,
message: '請填寫完整預算範圍'
}
]
const check = async () => {
result.value = (await form.value?.validate())?.valid ? '驗證通過' : '驗證未通過'
}
</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="預算" prop="budget" :rules="rules"
><HuiRangeInput
id="budget-range"
v-model="model.budget"
clearable
:validate-event="validateEvent"
/></HuiFormItem>
<HuiFormItem label="可編輯例外" prop="editable" :readonly="false"
><HuiRangeInput v-model="model.editable" clearable
/></HuiFormItem>
</HuiForm>
<HuiButton @click="check">手動驗證</HuiButton><span>{{ result }}</span>
</HuiSpace>
</template>
查詢表單組合
在 SearchForm 預設插槽內使用一個 FormItem 存放整個 tuple。查詢和重置結果直接顯示在頁面上。
<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('尚未查詢')
const search = (source: string) => {
result.value = `${source}:價格 ${query.price[0] ?? '不限'}~${query.price[1] ?? '不限'};編號 ${query.code[0] || '不限'}~${query.code[1] || '不限'}`
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSearchForm :model="query" @search="search('查詢')" @reset="search('重置')">
<HuiFormItem label="價格" prop="price"
><HuiRangeInput v-model="query.price" :min="0" clearable
/></HuiFormItem>
<HuiFormItem label="編號" prop="code"
><HuiRangeInput v-model="query.code" type="text" clearable
/></HuiFormItem>
</HuiSearchForm>
<span>{{ result }}</span>
</HuiSpace>
</template>
Props
| 屬性 | 類型 / 可選值 | 預設值 | 說明 |
|---|---|---|---|
| modelValue / v-model | RangeInputValue | undefined | 兩端值;任一端可為 undefined |
| type | 'number' | 'text' | 'number' | 數值或文字範圍 |
| separator | string | 目前語言的“至” | 分隔文字;空字串有效,插槽優先 |
| placeholder | string | [string, string] | 數值“最小值 / 最大值”,文字“開始 / 結束” | 兩端共用或分別指定 |
| min | number | 不限制 | 數值模式兩端最小值 |
| max | number | 不限制 | 數值模式兩端最大值 |
| step | number | 1 | 數值模式方向鍵步長 |
| precision | number | 不限制 | 數值模式小數位數 |
| autoSwap | boolean | true | 離開整個元件後交換倒置數值;文字不交換 |
| clearable | boolean | false | 顯示整組清除按鈕 |
| disabled | boolean | false | 禁用;也繼承外層組和表單 |
| size | 'large' | 'default' | 'small' | 繼承配置 | 三種控制項尺寸 |
| validateEvent | boolean | true | 自動觸發表單 change / 整組 blur 驗證 |
Events
| 事件 | 參數 | 說明 |
|---|---|---|
| update:modelValue | (value: RangeInputValue) | 請求更新雙端值 |
| change | (value: RangeInputValue) | 實際值變化、自動交換或清除;文字模式逐字觸發,數值模式在原生 change 時提交 |
| focus | (event: FocusEvent, index: 0 | 1) | 某一端獲得焦點 |
| blur | (event: FocusEvent, index: 0 | 1) | 某一端失焦;組內切換也觸發此公開事件 |
| clear | () | 圖示或clear()請求清除整個範圍 |
Slots
| 插槽 | 參數 | 說明 |
|---|---|---|
| separator | 無 | 替換分隔文字,外層已從螢幕閱讀器樹隱藏;建議只放非互動內容 |
方法
| 方法 | 類型 | 說明 |
|---|---|---|
| focus | (index?: 0 | 1) => void | 聚焦指定端,預設 0;禁用時無效 |
| blur | () => void | 移開元件內目前焦點,包括清除按鈕 |
| clear | () => void | 請求清除實際非空雙端;不受clearable展示開關限制,禁用/唯讀/卸載後無效 |
類型
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
}