Input 輸入框
單行文字輸入元件,適合使用者名稱、搜尋詞或金額等短文字。支援清空、顯示密碼、密碼強度、字數統計、前後綴圖示與插槽,以及前置/後置附加內容。在 HuiForm 內會沿用表單的禁用與尺寸設定,並參與驗證;多行文字請使用 HuiTextarea。
引入
import { HuiInput } from '@nscode/handy-ui'
自行保存與受控綁定
未傳入 modelValue 時,由元件自行保存輸入;父元件無關的重新呈現不會清除內容,僅監聽 update:modelValue 仍屬此模式。傳入該屬性即為受控模式,包括顯式 undefined、null,兩者都顯示為空;父元件拒絕回填時,畫面維持實際綁定值。
不另設 defaultValue。執行期間增刪綁定屬性會發出開發警告,之後採切換後的模式。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiInput, HuiSpace } from '@nscode/handy-ui'
const redraw = ref(0)
const lastInput = ref('尚未輸入')
const record = (value: string | number) => {
lastInput.value = String(value)
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="redraw++">父元件重新呈現:{{ redraw }}</HuiButton>
<HuiInput
:data-parent-render="redraw"
clearable
:maxlength="20"
show-word-limit
placeholder="自行保存,僅監聽更新"
@update:model-value="record"
/>
<span>最近更新:{{ lastInput }}</span>
<HuiInput model-value="固定值" placeholder="受控:父元件不回填" />
<HuiInput :model-value="undefined" placeholder="顯式 undefined:受控空值" />
<HuiInput :model-value="null" placeholder="顯式 null:受控空值" />
</HuiSpace>
</template>
基礎用法
用 v-model 綁定一個字串,placeholder 是沒有內容時顯示的提示文字。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInput } from '@nscode/handy-ui'
const value = ref('')
</script>
<template>
<HuiInput v-model="value" placeholder="請輸入內容" />
<p style="margin-top: 12px">輸入的內容:{{ value || '(空)' }}</p>
</template>
只允許輸入某些字元
allow-input 的六種預設與正規表示式會逐字元過濾,貼上時僅保留符合規則的字元。函式則檢查輸入後的完整候選值;回傳 false 時恢復原值與選取範圍。空字串一律允許,且不呼叫函式,因此使用者仍可刪除全部內容。
<script setup lang="ts">
import { reactive, ref } from 'vue'
import { HuiInput, HuiSpace } from '@nscode/handy-ui'
import type { InputAllowPreset } from '@nscode/handy-ui'
const presets: InputAllowPreset[] = ['digit', 'int', 'num', 'alpha', 'alnum', 'hex']
const values = reactive<Record<InputAllowPreset, string>>({
digit: '',
int: '',
num: '',
alpha: '',
alnum: '',
hex: ''
})
const patternValue = ref('')
const decimalValue = ref('')
const allowDecimal = (value: string) => /^\d*(\.\d{0,2})?$/.test(value)
</script>
<template>
<HuiSpace wrap align="start">
<div v-for="preset in presets" :key="preset" style="width: 180px">
<p>{{ preset }}</p>
<HuiInput v-model="values[preset]" :allow-input="preset" :placeholder="preset" />
<p>綁定值:'{{ values[preset] }}'</p>
</div>
<div style="width: 180px">
<p>正規表示式:數字和小數點</p>
<HuiInput v-model="patternValue" :allow-input="/[\d.]/" placeholder="逐字元過濾" />
<p>綁定值:'{{ patternValue }}'</p>
</div>
<div style="width: 180px">
<p>函式:最多兩位小數</p>
<HuiInput v-model="decimalValue" :allow-input="allowDecimal" placeholder="整個值驗證" />
<p>綁定值:'{{ decimalValue }}'</p>
</div>
</HuiSpace>
</template>
digit 只允許 0-9;int 另外允許開頭一個負號;num 再允許一個小數點,輸入中的 -、.、-. 也保留;alpha 只允許英文字母;alnum 允許英文字母和數字;hex 允許 0-9、A-F、a-f。
digit 預設使用 inputmode="numeric",num 使用 inputmode="decimal";int 不強制鍵盤類型,便於輸入負號。只要正整數並希望彈出數字鍵盤時用 digit。顯式傳入的 inputmode 優先。過濾覆蓋打字、貼上、拖放和輸入法上屏,外部傳入的綁定值不會被過濾。HuiInput 的函式接收整個值,HuiInputOtp 的函式接收單個字元。
已有小數點時,中間再輸入或貼上的小數點會被過濾,原有小數點和負號保留;選中它們後可以替換。未啟用掩碼時,allow-input 和 maxlength 同用會先過濾插入內容和已有前後文,再按過濾後前後文的 UTF-16 長度計算剩餘容量,不拆開 emoji 等完整碼位。有有效編輯區間時,只截斷本次插入段,保留未編輯的有效前後文;刪除不會連帶截斷原值。設不設 maxlength,編輯時都會過濾外部值中的無效字元;外部回填本身不主動過濾。沒有有效快照的自動填充等整值更新才截斷整個值。函式仍先檢查整個候選值,通過後再限制插入長度。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInput, HuiSpace } from '@nscode/handy-ui'
const amount = ref('12.5')
const code = ref('')
const full = ref('123')
const replacement = ref('1234')
</script>
<template>
<HuiSpace direction="vertical" fill>
<p>在 1 後輸入小數點,原有的 12.5 保持不變</p>
<HuiInput v-model="amount" allow-input="num" />
<span>金額:'{{ amount }}'</span>
<p>貼上 a1b2c3,先過濾得到 123,再限制為三位</p>
<HuiInput v-model="code" allow-input="digit" :maxlength="3" show-word-limit />
<span>程式碼:'{{ code }}'</span>
<p>三位已滿:在開頭輸入 9 仍為 123,可選中原字元後替換</p>
<HuiInput v-model="full" allow-input="digit" :maxlength="3" show-word-limit />
<span>保留未編輯部分:'{{ full }}'</span>
<p>四位已滿:在 1 後貼上 99 仍為 1234;選中 23 再貼上 a9b8c7 得到 1984</p>
<HuiInput v-model="replacement" allow-input="digit" :maxlength="4" show-word-limit />
<span>替換結果:'{{ replacement }}'</span>
</HuiSpace>
</template>
輸入掩碼
mask 以 9 代表數字、a 代表英文字母、* 代表英文字母或數字;反斜線可跳脫下一個字元,? 後為選填區段。僅在 type="text" 或 type="tel" 時生效。中間分隔符會在後方有內容時補入;區段填滿後會補齊閉合字面值,例如 (99) 顯示為 (12)。
預設 mask-value="raw",模型不包含前綴與分隔符;masked 則保留完整顯示格式。必填區段由未完成轉為完成時通知 finish,繼續填寫選填區不會重複通知,外部回填也不觸發。
單一碼點的 insertText 或輸入法確認字元,會直接當作可填字元處理,以免誤刪與字面值相同的輸入。一次輸入多個字元、拖放、整段輸入法確認、貼上或取代時,會先移除目前槽位所對應的前綴與分隔符。例如整段輸入 +8613800000000,顯示為 +86 138 0000 0000,raw 模型為 13800000000;前綴數字不列入號碼或 finish 參數。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInput, HuiSpace, HuiMessage } from '@nscode/handy-ui'
const phone = ref('')
const card = ref('')
const prefixed = ref('')
const formatted = ref('')
const closed = ref('')
const finished = (value: string) => HuiMessage.success(`已填滿:${value}`)
</script>
<template>
<HuiSpace wrap align="start">
<div style="width: 220px">
<p>手機號(raw)</p>
<HuiInput
v-model="phone"
mask="999 9999 9999"
placeholder="138 0000 0000"
@finish="finished"
/>
<p>綁定值:'{{ phone }}'</p>
</div>
<div style="width: 220px">
<p>銀行卡(16 位必填,後三位可選)</p>
<HuiInput
v-model="card"
mask="9999 9999 9999 9999? 999"
placeholder="16 或 19 位"
@finish="finished"
/>
<p>綁定值:'{{ card }}'</p>
</div>
<div style="width: 220px">
<p>帶字面量前綴:貼上、拖入或鍵盤整串插入 +8613800000000</p>
<HuiInput
v-model="prefixed"
mask="+86 999 9999 9999"
allow-input="digit"
type="tel"
placeholder="+86 138 0000 0000"
@finish="finished"
/>
<p>綁定值:'{{ prefixed }}'</p>
</div>
<div style="width: 220px">
<p>手機號(masked)</p>
<HuiInput
v-model="formatted"
mask="999 9999 9999"
mask-value="masked"
placeholder="包含分隔符"
@finish="finished"
/>
<p>綁定值:'{{ formatted }}'</p>
</div>
<div style="width: 220px">
<p>尾部字面量(masked)</p>
<HuiInput v-model="closed" mask="(99)" mask-value="masked" @finish="finished" />
<p>填滿兩位後綁定值帶括號:'{{ closed }}'</p>
</div>
</HuiSpace>
</template>
自訂掩碼佔位符號
mask-tokens 可擴充或覆寫佔位符號。下例以 X 限定統一社會信用代碼可接受的字元;即使正規表示式帶有 g、y 旗標,連續輸入也不會遺漏字元。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInput, HuiSpace } from '@nscode/handy-ui'
const value = ref('')
const tokens = { X: /[0-9A-HJ-NPQRTUWXY]/ }
const escaped = ref('')
const escapedMask = '\\9aa-99'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiInput
v-model="value"
mask="XXXXXXXXXXXXXXXXXX"
:mask-tokens="tokens"
placeholder="18 位統一社會信用代碼"
/>
<span>綁定值:'{{ value }}'</span>
<HuiInput v-model="escaped" :mask="escapedMask" placeholder="字面量 9 + 兩位字母 + 兩位數字" />
<span>轉義前綴 9 不包含在 raw 綁定中:'{{ escaped }}'</span>
</HuiSpace>
</template>
掩碼表單驗證
未填完時失焦會保留內容,是否可提交由表單驗證決定。isMaskComplete 支援 raw、masked 與自訂佔位符號,能從字面值區段起點辨識不完整前綴或分隔符。例如 '+861'、'+86 1' 對 '+86 999' 都不算完成。
貼上或自動填入 +8613800000000、+86-138-0000-0000、13800000000,都會得到同一號碼。純數字字面值可能與 raw 值重合,例如 '99' 對 '\\999' 會解讀為字面值 9 加一個有效數字;這類情況請傳入完整顯示值,例如 '999',以排除歧義。
<script setup lang="ts">
import { reactive, ref } from 'vue'
import {
HuiForm,
HuiFormItem,
HuiInput,
HuiButton,
HuiMessage,
isMaskComplete
} from '@nscode/handy-ui'
import type { FormRule } from '@nscode/handy-ui'
const formRef = ref<InstanceType<typeof HuiForm>>()
const model = reactive({ phone: '' })
const mask = '999 9999 9999'
const rules: Record<string, FormRule[]> = {
phone: [
{
validator: (_rule, value) => isMaskComplete(String(value ?? ''), mask),
message: '請填滿 11 位手機號',
trigger: 'blur'
}
]
}
const submit = async () => {
if ((await formRef.value!.validate()).valid) HuiMessage.success(`手機號:${model.phone}`)
}
</script>
<template>
<HuiForm ref="formRef" :model="model" :rules="rules" label-width="80px" @submit.prevent>
<HuiFormItem label="手機號" prop="phone">
<HuiInput v-model="model.phone" :mask="mask" placeholder="138 0000 0000" />
</HuiFormItem>
<HuiFormItem><HuiButton type="primary" @click="submit">驗證並提交</HuiButton></HuiFormItem>
</HuiForm>
</template>
尺寸
size 可選 large、default、small,高度分別是 40、32、24px,字級 16、14、12px,圓角 8、6、4px,和同尺寸的 HuiButton 一致。prefix-icon、suffix-icon 的圖示和清空、顯示密碼按鈕的圖示比文字大 2px(18、16、14px),前後綴插槽裡自己放的圖示不受影響。放在 HuiForm 裡且沒傳 size 時,跟隨表單項或表單的尺寸。
下面每一行是一種尺寸:帶前綴圖示的可清空輸入框、帶顯示密碼按鈕的密碼框,最後放一個同尺寸的按鈕對比高度。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiInput, HuiSpace } from '@nscode/handy-ui'
const keyword = ref('輸入框')
const password = ref('123456')
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">large</span>
<HuiInput
v-model="keyword"
size="large"
prefix-icon="search"
clearable
style="width: 200px"
/>
<HuiInput
v-model="password"
size="large"
type="password"
show-password
style="width: 200px"
/>
<HuiButton size="large">按鈕</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">default</span>
<HuiInput v-model="keyword" prefix-icon="search" clearable style="width: 200px" />
<HuiInput v-model="password" type="password" show-password style="width: 200px" />
<HuiButton>按鈕</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">small</span>
<HuiInput
v-model="keyword"
size="small"
prefix-icon="search"
clearable
style="width: 200px"
/>
<HuiInput
v-model="password"
size="small"
type="password"
show-password
style="width: 200px"
/>
<HuiButton size="small">按鈕</HuiButton>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 56px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
禁用和唯讀
disabled 禁用後不能編輯,也不能聚焦;readonly 唯讀時可以聚焦、選中和複製文字,但不能修改。兩種狀態下都不顯示清空按鈕和密碼切換按鈕。在禁用的 HuiForm 裡也會被禁用。
下面第一行是禁用、第二行是唯讀,從左到右是有內容、沒有內容(顯示提示文字)、可清空的密碼框:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInput, HuiSpace } from '@nscode/handy-ui'
const value = ref('不能修改的內容')
const empty = ref('')
const password = ref('123456')
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">disabled</span>
<HuiInput v-model="value" prefix-icon="user" disabled style="width: 200px" />
<HuiInput v-model="empty" placeholder="禁用時的提示文字" disabled style="width: 200px" />
<HuiInput
v-model="password"
type="password"
show-password
clearable
disabled
style="width: 200px"
/>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">readonly</span>
<HuiInput v-model="value" prefix-icon="user" readonly style="width: 200px" />
<HuiInput v-model="empty" placeholder="唯讀時的提示文字" readonly style="width: 200px" />
<HuiInput
v-model="password"
type="password"
show-password
clearable
readonly
style="width: 200px"
/>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 64px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
可清空
clearable=true 且有內容時,右側顯示清空按鈕。點選後請求空字串 '',依原有順序通知 update:modelValue → input → clear → change,再讓輸入框取得焦點。這是有「清空」名稱的原生 <button>,可用 Tab 聚焦、Enter 觸發。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInput, HuiMessage } from '@nscode/handy-ui'
const value = ref('按右側按鈕清空')
const onClear = () => {
HuiMessage.info('已清空')
}
</script>
<template>
<HuiInput v-model="value" clearable @clear="onClear" />
</template>
密碼框
type="password" 時輸入的內容顯示成圓點。再加上 show-password,右側會出現眼睛按鈕,點擊在明文和密文之間切換;不加時沒有切換按鈕,只能看到圓點。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInput, HuiSpace } from '@nscode/handy-ui'
const password = ref('123456')
const confirm = ref('')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiInput v-model="password" type="password" placeholder="不帶切換按鈕" />
<HuiInput v-model="confirm" type="password" show-password placeholder="帶顯示密碼按鈕" />
</HuiSpace>
</template>
密碼強度
在 type="password" 時設定 show-strength,輸入框下方會顯示三段強度條:弱為一段危險色、中為兩段警告色、強為三段成功色。空值時不顯示;顯示時會預留一行,表單錯誤訊息順延至下方。
預設 getPasswordStrength 規則:少於 8 碼為弱;8 碼以上依小寫、大寫、數字、符號各計 1 分,12 碼以上再加 1 分。1 分為弱、2 分為中、3 分以上為強。
強度變更時,螢幕閱讀器會朗讀「密碼強度:中」之類的提示;輸入框也以 aria-describedby 關聯強度,在聚焦時一併提供說明。下例第一項可自行輸入,後三項分別展示弱、中、強,可按眼睛按鈕查看原文。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInput, HuiSpace } from '@nscode/handy-ui'
const password = ref('')
const weak = ref('12345678')
const medium = ref('abcd1234')
const strong = ref('Abcd1234!')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiInput
v-model="password"
type="password"
show-password
show-strength
placeholder="輸入密碼看強度"
/>
<HuiInput v-model="weak" type="password" show-password show-strength />
<HuiInput v-model="medium" type="password" show-password show-strength />
<HuiInput v-model="strong" type="password" show-password show-strength />
</HuiSpace>
</template>
自訂強度規則
strength-method 傳一個函式自訂規則:參數是輸入的密碼(不會是空字串),返回 'weak'、'medium'、'strong' 之一。返回其他值時不顯示強度,開發環境下會在主控台警告。
只想在預設規則上加一點限制時,可以從 @nscode/handy-ui 導入 getPasswordStrength 接著用。下面第一個輸入框是完全自訂的規則,第二個在預設規則的基礎上把常見的弱密碼一律算成弱:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInput, HuiSpace, getPasswordStrength } from '@nscode/handy-ui'
import type { InputPasswordStrength } from '@nscode/handy-ui'
const custom = ref('')
const extended = ref('Password123')
// 少於 10 位是弱;10 位及以上,同時有字母和數字是中,再加上符號是強
const customRule = (value: string): InputPasswordStrength => {
if (value.length < 10 || !/[a-zA-Z]/.test(value) || !/\d/.test(value)) return 'weak'
return /[^a-zA-Z\d]/.test(value) ? 'strong' : 'medium'
}
// 預設規則,另外帶 password、123456、qwerty 字樣的一律算弱
const extendedRule = (value: string): InputPasswordStrength =>
/password|123456|qwerty/i.test(value) ? 'weak' : getPasswordStrength(value)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiInput
v-model="custom"
type="password"
show-password
show-strength
:strength-method="customRule"
placeholder="至少 10 位,字母、數字、符號都有才是強"
/>
<HuiInput
v-model="extended"
type="password"
show-password
show-strength
:strength-method="extendedRule"
placeholder="帶 password、123456 的算弱"
/>
</HuiSpace>
</template>
強度文字
strength-texts 依序提供弱、中、強三個等級的文字,缺少的項目使用語言包。strength-label 是朗讀強度前的說明,預設取語言包的「密碼強度」,不顯示於畫面;顯式空字串表示只朗讀強度本身。局部調整可使用這兩個屬性,切換整個應用程式的語言請見《國際化》。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInput, HuiSpace } from '@nscode/handy-ui'
const password = ref('abcd1234')
const texts = ['強度不足', '普通', '強']
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiInput
v-model="password"
type="password"
show-password
show-strength
:strength-texts="texts"
strength-label="密碼強度"
placeholder="密碼"
/>
</HuiSpace>
</template>
在表單裡驗證強度
強度提示只是提示,不會攔住提交。要求密碼至少達到“中”時,在驗證規則裡用 getPasswordStrength(或者你自己的規則函式)判斷。強度顯示在輸入框下面,驗證不通過時錯誤提示排在強度下面:
<script setup lang="ts">
import { reactive, ref } from 'vue'
import {
HuiButton,
HuiForm,
HuiFormItem,
HuiInput,
HuiMessage,
getPasswordStrength
} from '@nscode/handy-ui'
import type { FormRule } from '@nscode/handy-ui'
const formRef = ref<InstanceType<typeof HuiForm>>()
const model = reactive({ password: 'abc' })
const rules: Record<string, FormRule[]> = {
password: [
{ required: true, message: '請輸入密碼', trigger: 'blur' },
{
validator: (_rule, value) => getPasswordStrength(String(value ?? '')) !== 'weak',
message: '密碼太弱,至少 8 位,並包含字母、數字、符號中的兩種以上',
trigger: 'blur'
}
]
}
const submit = async () => {
const { valid } = await formRef.value!.validate()
if (valid) HuiMessage.success('提交成功')
}
</script>
<template>
<HuiForm ref="formRef" :model="model" :rules="rules" label-width="80px" @submit.prevent>
<HuiFormItem label="新密碼" prop="password">
<HuiInput v-model="model.password" type="password" show-password show-strength />
</HuiFormItem>
<HuiFormItem>
<HuiButton type="primary" @click="submit">提交</HuiButton>
</HuiFormItem>
</HuiForm>
</template>
前後綴圖示
prefix-icon、suffix-icon 傳圖示名,圖示分別顯示在輸入框左側和右側,只用來裝飾,不能點擊。兩個可以同時用。能用的圖示名見 Icon 元件的文件。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInput, HuiSpace } from '@nscode/handy-ui'
const keyword = ref('')
const title = ref('')
const phone = ref('')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiInput v-model="keyword" prefix-icon="search" placeholder="前綴圖示(prefix-icon)" />
<HuiInput v-model="title" suffix-icon="edit" placeholder="後綴圖示(suffix-icon)" />
<HuiInput v-model="phone" prefix-icon="phone" suffix-icon="lock" placeholder="前後都有" />
</HuiSpace>
</template>
前綴、後綴插槽
需要放文字或自訂內容時用 prefix、suffix 插槽,插槽內容優先於 prefix-icon、suffix-icon。顯示清空按鈕或密碼切換按鈕時,後綴(插槽和圖示)會讓出位置,不顯示:下面第三個輸入框有內容時顯示清空按鈕,清空後才顯示後綴“.com”。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInput, HuiIcon, HuiSpace } from '@nscode/handy-ui'
const price = ref('')
const user = ref('')
const site = ref('example')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiInput v-model="price" placeholder="金額">
<template #prefix>¥</template>
<template #suffix>元</template>
</HuiInput>
<HuiInput v-model="user" placeholder="使用者名稱">
<template #prefix><HuiIcon name="user" /></template>
</HuiInput>
<HuiInput v-model="site" clearable placeholder="域名">
<template #prefix>https://</template>
<template #suffix>.com</template>
</HuiInput>
</HuiSpace>
</template>
前置、後置內容
prepend、append 插槽的內容放在輸入區外側:和輸入框共用一圈邊框,中間用一條豎線隔開,帶淺灰底色,只有兩端是圓角。常用來放協議、域名後綴、單位這類固定文字。
和前綴、後綴的區別:prefix、suffix 在輸入區裡面,沒有底色和豎線。兩組可以同時用,也能和清空按鈕、字數統計一起用;有內容時清空按鈕只讓後綴讓位,後置內容一直顯示。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiIcon, HuiInput, HuiSpace } from '@nscode/handy-ui'
const site = ref('example')
const receiver = ref('')
const subdomain = ref('docs')
const link = ref('')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiInput v-model="site" clearable placeholder="域名">
<template #prepend>https://</template>
<template #append>.com</template>
</HuiInput>
<HuiInput v-model="receiver" prefix-icon="mail" placeholder="和前綴圖示一起用">
<template #prepend>收件人</template>
</HuiInput>
<HuiInput v-model="subdomain" :maxlength="20" show-word-limit clearable>
<template #append>.example.com</template>
</HuiInput>
<HuiInput v-model="link" placeholder="前置裡放圖示">
<template #prepend><HuiIcon name="link" /></template>
</HuiInput>
</HuiSpace>
</template>
前置、後置放按鈕和選擇器
插槽裡直接放 HuiButton、HuiSelect 時,前置、後置區不再有底色和內距,按鈕、選擇器自己的邊框頂替輸入框那一段的邊框,接縫處只有一條線,外側的圓角和輸入框一致、裡側是直角。
- 選擇器預設按內容寬,寫
style="width: ..."固定寬度,切換選項時輸入框才不會忽寬忽窄。 - 按鈕、選擇器的高度會撐滿輸入框,字級、內距仍按它們自己的
size,輸入框是large、small時給它們傳同樣的size。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiInput, HuiMessage, HuiSelect, HuiSpace } from '@nscode/handy-ui'
const keyword = ref('')
const host = ref('')
const protocol = ref('https://')
const suffix = ref('.com')
const amount = ref('100')
const protocols = [
{ label: 'http://', value: 'http://' },
{ label: 'https://', value: 'https://' }
]
const suffixes = [
{ label: '.com', value: '.com' },
{ label: '.cn', value: '.cn' },
{ label: '.org', value: '.org' }
]
const search = () => {
HuiMessage.info(keyword.value ? `搜尋:${keyword.value}` : '請輸入關鍵字')
}
const visit = () => {
HuiMessage.info(`訪問:${protocol.value}${host.value || 'example'}${suffix.value}`)
}
const step = (delta: number) => {
amount.value = String((Number(amount.value) || 0) + delta)
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiInput
v-model="keyword"
clearable
placeholder="輸入後按Enter或點搜尋"
@keydown.enter="search"
>
<template #append>
<HuiButton type="primary" icon="search" @click="search">搜尋</HuiButton>
</template>
</HuiInput>
<HuiInput v-model="host" placeholder="example">
<template #prepend>
<HuiSelect v-model="protocol" :options="protocols" style="width: 100px" />
</template>
<template #append>
<HuiSelect v-model="suffix" :options="suffixes" style="width: 90px" />
</template>
</HuiInput>
<HuiInput v-model="amount" placeholder="數量">
<template #prepend>
<HuiButton icon="minus" aria-label="減 10" @click="step(-10)" />
</template>
<template #append>
<HuiButton icon="plus" aria-label="加 10" @click="step(10)" />
</template>
</HuiInput>
<HuiInput v-model="host" placeholder="example">
<template #prepend>https://</template>
<template #append>
<HuiButton @click="visit">訪問</HuiButton>
</template>
</HuiInput>
</HuiSpace>
</template>
前置、後置內容的尺寸和禁用
前置、後置內容跟著輸入框的 size 變:文字的字級、左右內距和輸入框一樣,三種尺寸都對齊。輸入框禁用時,前置、後置的文字變成禁用色;裡面放的按鈕、選擇器是獨立的元件,要自己加 disabled。
<script setup lang="ts">
import { reactive } from 'vue'
import { HuiButton, HuiInput, HuiSelect, HuiSpace } from '@nscode/handy-ui'
const sizes = ['large', 'default', 'small'] as const
const hosts = reactive({ large: 'example', default: 'example', small: 'example' })
const protocols = reactive({ large: 'https://', default: 'https://', small: 'https://' })
const options = [
{ label: 'http://', value: 'http://' },
{ label: 'https://', value: 'https://' }
]
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace v-for="size in sizes" :key="size" wrap>
<span class="row-label">{{ size }}</span>
<HuiInput v-model="hosts[size]" :size="size" style="width: 260px">
<template #prepend>https://</template>
<template #append>.com</template>
</HuiInput>
<HuiInput v-model="hosts[size]" :size="size" clearable style="width: 340px">
<template #prepend>
<HuiSelect
v-model="protocols[size]"
:options="options"
:size="size"
style="width: 120px"
/>
</template>
<template #append>
<HuiButton type="primary" :size="size">訪問</HuiButton>
</template>
</HuiInput>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">disabled</span>
<HuiInput model-value="example" disabled style="width: 260px">
<template #prepend>https://</template>
<template #append>.com</template>
</HuiInput>
<HuiInput model-value="example" disabled style="width: 340px">
<template #prepend>
<HuiSelect model-value="https://" :options="options" disabled style="width: 120px" />
</template>
<template #append>
<HuiButton type="primary" disabled>訪問</HuiButton>
</template>
</HuiInput>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 64px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
前置、後置內容的驗證狀態
在出錯的 HuiFormItem 裡,整圈邊框(包括前置、後置區)變成危險色;裡面放的選擇器也一起變紅,按鈕保持自己的顏色。用開關切換表單項的 error 看看:
<script setup lang="ts">
import { reactive, ref } from 'vue'
import {
HuiButton,
HuiForm,
HuiFormItem,
HuiInput,
HuiSelect,
HuiSpace,
HuiSwitch
} from '@nscode/handy-ui'
const showError = ref(true)
const model = reactive({ host: 'exa mple', protocol: 'https://', keyword: '', price: '-1' })
const protocols = [
{ label: 'http://', value: 'http://' },
{ label: 'https://', value: 'https://' }
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSwitch v-model="showError" active-text="顯示錯誤" />
<HuiForm :model="model" label-width="80px" @submit.prevent>
<HuiFormItem label="網址" :error="showError ? '網址裡不能有空白' : ''">
<HuiInput v-model="model.host" clearable>
<template #prepend>
<HuiSelect v-model="model.protocol" :options="protocols" style="width: 100px" />
</template>
<template #append>.com</template>
</HuiInput>
</HuiFormItem>
<HuiFormItem label="關鍵字" :error="showError ? '請輸入關鍵字' : ''" size="large">
<HuiInput v-model="model.keyword" prefix-icon="search">
<template #append>
<HuiButton type="primary" size="large">搜尋</HuiButton>
</template>
</HuiInput>
</HuiFormItem>
<HuiFormItem label="價格" :error="showError ? '價格不能小於 0' : ''" size="small">
<HuiInput v-model="model.price">
<template #prepend>¥</template>
<template #append>元</template>
</HuiInput>
</HuiFormItem>
</HuiForm>
</HuiSpace>
</template>
字數統計
同時設定 maxlength 和 show-word-limit,右側顯示“已輸入/上限”。只設 maxlength 時只限制長度,不顯示統計;只設 show-word-limit 不起作用。字數統計和清空按鈕可以同時顯示,三種尺寸都能用。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInput, HuiSpace } from '@nscode/handy-ui'
const limitOnly = ref('')
const withCount = ref('')
const small = ref('小尺寸')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiInput v-model="limitOnly" :maxlength="10" placeholder="最多 10 個字,不顯示統計" />
<HuiInput
v-model="withCount"
:maxlength="20"
show-word-limit
clearable
placeholder="最多 20 個字"
/>
<HuiInput v-model="small" size="small" :maxlength="20" show-word-limit />
</HuiSpace>
</template>
輸入類型
type 可選 text、password、number、email、tel、url,對應原生 input 的類型,會影響手機上彈出的鍵盤。type="number" 時 v-model 拿到的仍然是字串(如 "12"),需要數字時寫 v-model.number;要限制範圍、步長時用 HuiInputNumber。
<script setup lang="ts">
import { reactive, ref } from 'vue'
import { HuiInput, HuiSpace } from '@nscode/handy-ui'
const form = reactive({ text: '', password: '', email: '', tel: '', url: '' })
const age = ref<number | string>('')
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">text</span>
<HuiInput v-model="form.text" placeholder="普通文字(預設)" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">password</span>
<HuiInput v-model="form.password" type="password" placeholder="密碼" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">number</span>
<HuiInput v-model.number="age" type="number" placeholder="年齡" style="width: 240px" />
<span>值:{{ age === '' ? '(空)' : age }},類型:{{ typeof age }}</span>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">email</span>
<HuiInput v-model="form.email" type="email" placeholder="電子郵件" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">tel</span>
<HuiInput v-model="form.tel" type="tel" placeholder="手機號" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">url</span>
<HuiInput v-model="form.url" type="url" placeholder="網址" style="width: 240px" />
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 64px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
事件
輸入時觸發 input(和 update:modelValue 同時),內容有變化後失焦或按Enter觸發 change,獲得、失去焦點觸發 focus、blur,點清空按鈕觸發 clear。在下面的輸入框裡打字、按Enter、點外面、點清空按鈕,看看事件的順序:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInput } from '@nscode/handy-ui'
const value = ref('')
const logs = ref<string[]>([])
const log = (text: string) => {
logs.value = [text, ...logs.value].slice(0, 6)
}
const onInput = (val: string | number) => log(`input:${val}`)
const onChange = (val: string | number) => log(`change:${val}`)
</script>
<template>
<HuiInput
v-model="value"
clearable
placeholder="輸入幾個字試試"
@input="onInput"
@change="onChange"
@focus="log('focus')"
@blur="log('blur')"
@clear="log('clear')"
/>
<p style="margin-top: 12px">最近的事件(新的在前):{{ logs.join(',') || '還沒有' }}</p>
</template>
驗證狀態
輸入框沒有單獨的狀態屬性,出錯的樣子由所在的 HuiFormItem 決定:驗證不通過,或者給表單項傳了 error 時,輸入框的邊框變成危險色,下面顯示錯誤訊息,內部的 <input> 帶上 aria-invalid。下面用開關切換表單項的 error,三種尺寸都一樣:
<script setup lang="ts">
import { reactive, ref } from 'vue'
import { HuiForm, HuiFormItem, HuiInput, HuiSwitch } from '@nscode/handy-ui'
const showError = ref(true)
const model = reactive({ username: 'admin', email: 'abc', phone: '123' })
</script>
<template>
<HuiSwitch v-model="showError" active-text="顯示錯誤" />
<HuiForm :model="model" label-width="80px" style="margin-top: 16px" @submit.prevent>
<HuiFormItem label="使用者名稱" :error="showError ? '使用者名稱已被使用' : ''" size="large">
<HuiInput v-model="model.username" prefix-icon="user" clearable />
</HuiFormItem>
<HuiFormItem label="電子郵件" :error="showError ? '電子郵件格式不正確' : ''">
<HuiInput v-model="model.email" prefix-icon="mail" />
</HuiFormItem>
<HuiFormItem label="手機號" :error="showError ? '請輸入 11 位手機號碼' : ''" size="small">
<HuiInput v-model="model.phone" :maxlength="11" show-word-limit />
</HuiFormItem>
</HuiForm>
</template>
原生屬性和事件
class、style 留在最外層容器。data-hui-field、data-invalid、data-disabled、data-open、data-filled 為保留的邊框狀態標記,落在帶邊框的根容器,客戶端與 SSR 一致。其他屬性與監聽器則落在內部 <input>,包含 name、id、autofocus、tabindex、aria-label、其他 data-*,以及未宣告為元件事件的 @keydown、@keyup、@paste。
autocomplete 沿用原生自動填入屬性,例如設定 'off' 可關閉瀏覽器的歷史提示。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInput, HuiMessage } from '@nscode/handy-ui'
const keyword = ref('')
const search = () => {
HuiMessage.info(`搜尋:${keyword.value}`)
}
</script>
<template>
<HuiInput
v-model="keyword"
name="keyword"
autocomplete="off"
aria-label="搜尋關鍵字"
placeholder="輸入後按Enter搜尋"
class="search-box"
@keydown.enter="search"
/>
</template>
呼叫方法
通過 ref 呼叫 focus()、blur()、select()、clear(),或者用 inputRef 直接拿到內部的 <input> 元素。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInput, HuiButton, HuiSpace } from '@nscode/handy-ui'
const inputRef = ref<InstanceType<typeof HuiInput>>()
const value = ref('一段文字')
const focused = ref(false)
const moveToEnd = () => {
const el = inputRef.value?.inputRef
if (!el) return
el.focus()
el.setSelectionRange(el.value.length, el.value.length)
}
</script>
<template>
<HuiInput ref="inputRef" v-model="value" @focus="focused = true" @blur="focused = false" />
<div style="margin-top: 12px">
<HuiSpace wrap>
<HuiButton @click="inputRef?.focus()">聚焦</HuiButton>
<HuiButton @click="inputRef?.blur()">失焦</HuiButton>
<HuiButton @click="inputRef?.select()">全選</HuiButton>
<HuiButton @click="inputRef?.clear()">清空</HuiButton>
<HuiButton @click="moveToEnd">游標移到末尾(inputRef)</HuiButton>
</HuiSpace>
</div>
<p style="margin-top: 12px">輸入框{{ focused ? '已聚焦' : '沒有聚焦' }}</p>
</template>
在表單裡使用
放在 HuiFormItem 裡時,值變化觸發 change 規則,失焦觸發 blur 規則;表單項的 label 會通過 for 關聯到輸入框,驗證出錯時輸入框帶上 aria-invalid 和指向錯誤提示的 aria-describedby。不需要驗證時設 :validate-event="false"。
<script setup lang="ts">
import { reactive } from 'vue'
import { HuiForm, HuiFormItem, HuiInput } from '@nscode/handy-ui'
import type { FormRule } from '@nscode/handy-ui'
const model = reactive({ username: '' })
const rules: Record<string, FormRule[]> = {
username: [
{ required: true, message: '請輸入使用者名稱', trigger: 'blur' },
{ min: 3, max: 12, message: '長度 3 到 12 個字元', trigger: 'change' }
]
}
</script>
<template>
<HuiForm :model="model" :rules="rules" @submit.prevent>
<HuiFormItem label="使用者名稱" prop="username">
<HuiInput v-model="model.username" />
</HuiFormItem>
</HuiForm>
</template>
祖先變數覆蓋
--hui-input-padding-x 控制輸入區左右內距,尺寸預設仍為 large 16px、default 12px、small 8px。變數可以設在輸入框自身或任意祖先;下例外層統一為 20px,內層局部改為 8px,各元件仍保持自己的高度、字級和圓角。
前置、後置內容使用同一個內距;存在附加內容的一側由附加內容提供邊距,輸入框外層該側保持 0,文字與分隔線之間的補償同步更新。附加按鈕或選擇器仍沿用原來的貼邊版面配置。僅覆蓋變數即可,無需選擇內部節點;父元件的 <style scoped> 可直接在祖先類上設定變數,不需要 :deep()。
<script setup lang="ts">
import { reactive } from 'vue'
import { HuiInput } from '@nscode/handy-ui'
const values = reactive({
small: '小尺寸',
normal: '附加內容',
large: '大尺寸',
nested: '局部覆寫'
})
</script>
<template>
<div
class="input-variables-demo"
style="--hui-input-padding-x:20px;display:flex;flex-direction:column;gap:var(--hui-spacing-4);max-width:440px"
>
<HuiInput v-model="values.small" size="small"><template #prefix>前綴</template></HuiInput>
<HuiInput v-model="values.normal"
><template #prepend>https://</template><template #append>.com</template></HuiInput
>
<HuiInput v-model="values.large" size="large"><template #suffix>後綴</template></HuiInput>
<div class="input-variables-nested" style="--hui-input-padding-x:8px">
<HuiInput v-model="values.nested" />
</div>
<span class="input-variables-value">目前輸入:{{ values.normal }}</span>
</div>
</template>
Props
| 屬性 | 類型 | 預設值 | 說明 |
|---|---|---|---|
| modelValue | string | number | null | undefined | 傳入即受控;未傳自行保存,初始為空字串;顯式 null/undefined 顯示為空 |
| type | 'text' | 'password' | 'number' | 'email' | 'tel' | 'url' | 'text' | 原生 input 的類型 |
| size | 'large' | 'default' | 'small' | - | 尺寸。不傳時跟隨表單,不在表單裡時是 'default' |
| placeholder | string | - | 提示文字 |
| disabled | boolean | false | 是否禁用 |
| readonly | boolean | false | 是否唯讀 |
| clearable | boolean | false | 有內容時是否顯示清空按鈕 |
| showPassword | boolean | false | type 為 password 時是否顯示“顯示 / 隱藏密碼”按鈕 |
| maxlength | number | - | 最大輸入長度 |
| minlength | number | - | 最小輸入長度。原生屬性,只在原生表單驗證時起作用,不影響 HuiForm |
| showWordLimit | boolean | false | 是否顯示字數統計,需要同時設定 maxlength |
| prefixIcon | string | - | 前綴圖示名 |
| suffixIcon | string | - | 後綴圖示名 |
| autocomplete | string | - | 原生 autocomplete 屬性,例如 'off'、'username' |
| form | string | - | 原生 form 屬性:輸入框所屬 <form> 的 id |
| allowInput | InputAllowInput | - | 預設或正規表示式逐字元過濾;函式判斷整個輸入值,返回 false 時撤銷輸入。空字串始終允許 |
| mask | string | - | 輸入格式,9 數字、a 字母、* 字母或數字,反斜線轉義,? 後可選。僅 text、tel 生效 |
| maskTokens | Record<string, RegExp> | - | 擴展或覆蓋佔位符號 |
| maskValue | 'raw' | 'masked' | 'raw' | 綁定值不帶格式,或包含前綴和分隔符 |
| validateEvent | boolean | true | 值變化和失焦時是否觸發所在表單項的驗證 |
| showStrength | boolean | false | type 為 password 時,在輸入框下方顯示密碼強度(強度條和文字)。沒有輸入內容時不顯示 |
| strengthMethod | (value: string) => InputPasswordStrength | - | 自訂強度規則,參數是輸入的密碼(不為空),返回 'weak'、'medium'、'strong'。不傳時用 getPasswordStrength |
| strengthTexts | string[] | 隨語言(中文:['弱', '中', '强']) | 弱、中、強三檔顯示的文字,依次傳三項,沒傳的項取語言包 |
| strengthLabel | string | 隨語言(中文:密碼強度) | 螢幕閱讀器軟體在強度文字前讀出的說明,介面上不顯示。傳空字串時不讀 |
Events
| 事件 | 回呼參數 | 說明 |
|---|---|---|
| update:modelValue | (value: string | number) | 輸入時觸發,參數是輸入框裡的字串。輸入法組合期間不觸發,確認輸入後觸發一次 |
| input | (value: string | number) | 和 update:modelValue 同時觸發 |
| change | (value: string | number) | 原生 change:內容有變化後失焦或按Enter時觸發;點清空按鈕時也觸發,參數是 '' |
| focus | (event: FocusEvent) | 獲得焦點時觸發 |
| blur | (event: FocusEvent) | 失去焦點時觸發 |
| clear | - | 點清空按鈕或呼叫 clear() 後觸發 |
| finish | (value: string) | 掩碼必填部分從未填滿變為填滿時觸發,參數遵循 maskValue;外部回填不觸發 |
Slots
| 插槽 | 作用域參數 | 說明 |
|---|---|---|
| prefix | - | 輸入框左側的內容,優先於 prefixIcon |
| suffix | - | 輸入框右側的內容,優先於 suffixIcon。顯示清空、密碼切換按鈕時不顯示 |
| prepend | - | 輸入區外側左邊的內容,和輸入框共用邊框。放 HuiButton、HuiSelect 時邊框自動合併 |
| append | - | 輸入區外側右邊的內容,和輸入框共用邊框。放 HuiButton、HuiSelect 時邊框自動合併 |
方法
| 名稱 | 類型 | 說明 |
|---|---|---|
| focus | () => void | 讓輸入框獲得焦點 |
| blur | () => void | 讓輸入框失去焦點 |
| select | () => void | 選中輸入框裡的全部文字 |
| clear | () => void | 可編輯時清空,依次觸發 update:modelValue、input、clear、change;保留外部焦點 |
| inputRef | HTMLInputElement | 內部的 input 元素 |
工具函式
| 名稱 | 類型 | 說明 |
|---|---|---|
| isMaskComplete | (value: string, mask: string, tokens?: Record<string, RegExp>) => boolean | 掩碼的必填槽位是否填滿,支援帶格式或不帶格式的值 |
類型
公開類型使用元件前綴。兼容映射:PasswordStrength → InputPasswordStrength。舊名仍可從 @nscode/handy-ui 導入,已標記 @deprecated,將在 1.0 刪除。
import type {
InputProps,
InputEmits,
InputSize,
InputType,
InputPasswordStrength,
InputAllowPreset,
InputAllowInput
} from '@nscode/handy-ui'
import { getPasswordStrength, isMaskComplete } from '@nscode/handy-ui'
type InputSize = 'large' | 'default' | 'small'
type InputType = 'text' | 'password' | 'number' | 'email' | 'tel' | 'url'
type InputAllowPreset = 'digit' | 'int' | 'num' | 'alpha' | 'alnum' | 'hex'
type InputAllowInput = InputAllowPreset | RegExp | ((value: string) => boolean)
/** 密碼強度:弱、中、強 */
type InputPasswordStrength = 'weak' | 'medium' | 'strong'
/** show-strength 的預設規則,也可以在自訂規則、表單驗證裡用 */
function getPasswordStrength(value: string): InputPasswordStrength
function isMaskComplete(value: string, mask: string, tokens?: Record<string, RegExp>): boolean
interface InputProps {
modelValue?: string | number | null
allowInput?: InputAllowInput
mask?: string
maskTokens?: Record<string, RegExp>
maskValue?: 'raw' | 'masked'
type?: InputType
size?: InputSize
placeholder?: string
disabled?: boolean
readonly?: boolean
clearable?: boolean
showPassword?: boolean
maxlength?: number
minlength?: number
showWordLimit?: boolean
prefixIcon?: string
suffixIcon?: string
autocomplete?: string
form?: string
validateEvent?: boolean
showStrength?: boolean
strengthMethod?: (value: string) => InputPasswordStrength
strengthTexts?: string[]
strengthLabel?: string
}
interface InputEmits {
'update:modelValue': [value: string | number]
input: [value: string | number]
change: [value: string | number]
focus: [event: FocusEvent]
blur: [event: FocusEvent]
clear: []
finish: [value: string]
}
焦點方法與公開類型
元件 ref 使用 InputExpose。focus() 只聚焦輸入框,不改值;禁用或卸載後不執行,唯讀時仍可聚焦。blur() 只讓本元件所屬文件內、目前位於元件中的元素失焦,不影響外部焦點,表單驗證行為保持。
clear() 重用圖示清空的值與事件約定;禁用、唯讀時不執行,方法呼叫會保留外部焦點。select() 選取全部文字。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInput, HuiButton, HuiSpace } from '@nscode/handy-ui'
import type { InputExpose } from '@nscode/handy-ui'
const control = ref<InputExpose>()
const value = ref('文字')
const event = ref('等待操作')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton @mousedown.prevent @click="control?.focus()">focus()</HuiButton>
<HuiButton @mousedown.prevent @click="control?.blur()">blur()</HuiButton>
<HuiButton @mousedown.prevent @click="control?.select()">select()</HuiButton>
<HuiButton @mousedown.prevent @click="control?.clear()">clear()</HuiButton>
</HuiSpace>
<HuiInput
ref="control"
v-model="value"
clearable
@focus="event = 'focus'"
@blur="event = 'blur'"
/>
<span>{{ event }}</span>
</HuiSpace>
</template>
| 方法 | 簽名 |
|---|---|
| focus | () => void |
| blur | () => void |