FloatLabel 浮動標籤
獨立使用的浮動標籤。控制項有值、獲得焦點或打開面板時,標籤浮到上邊框。
引入
ts
import { HuiFloatLabel, HuiInput } from '@nscode/handy-ui'
import type { FloatLabelProps } from '@nscode/handy-ui'
示例
基礎用法
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiFloatLabel, HuiInput } from '@nscode/handy-ui'
const name = ref('')
</script>
<template>
<HuiFloatLabel label="部門名稱" style="width: 260px; margin-top: 8px">
<HuiInput v-model="name" clearable />
</HuiFloatLabel>
</template>
各種控制項
vue
<script setup lang="ts">
import { reactive } from 'vue'
import {
HuiFloatLabel,
HuiInput,
HuiTextarea,
HuiSelect,
HuiCascader,
HuiTreeSelect,
HuiTimePicker,
HuiDatePicker,
HuiAutoComplete,
HuiInputTag,
HuiMentions,
HuiIconPicker
} from '@nscode/handy-ui'
const model = reactive({
name: '',
remark: '',
department: undefined as string | undefined,
region: [] as string[],
tree: undefined as string | undefined,
time: '',
date: '',
keyword: '',
tags: [] as string[],
mentions: '',
icon: ''
})
const options = [
{ label: '研發部', value: 'dev' },
{ label: '運營部', value: 'ops' }
]
const regions = [{ label: '華東', value: 'east', children: [{ label: '上海', value: 'sh' }] }]
const tree = [{ label: '總部', id: 'root', children: [{ label: '研發部', id: 'dev' }] }]
</script>
<template>
<div class="float-grid">
<HuiFloatLabel label="Input 名稱"><HuiInput v-model="model.name" clearable /></HuiFloatLabel>
<HuiFloatLabel label="Textarea 備註"><HuiTextarea v-model="model.remark" /></HuiFloatLabel>
<HuiFloatLabel label="Select 部門"
><HuiSelect v-model="model.department" :options="options" clearable
/></HuiFloatLabel>
<HuiFloatLabel label="Cascader 地區"
><HuiCascader v-model="model.region" :options="regions"
/></HuiFloatLabel>
<HuiFloatLabel label="TreeSelect 組織"
><HuiTreeSelect v-model="model.tree" :data="tree"
/></HuiFloatLabel>
<HuiFloatLabel label="TimePicker 時間"><HuiTimePicker v-model="model.time" /></HuiFloatLabel>
<HuiFloatLabel label="DatePicker 日期"><HuiDatePicker v-model="model.date" /></HuiFloatLabel>
<HuiFloatLabel label="AutoComplete 關鍵詞"
><HuiAutoComplete v-model="model.keyword" :options="options"
/></HuiFloatLabel>
<HuiFloatLabel label="InputTag 標籤"><HuiInputTag v-model="model.tags" /></HuiFloatLabel>
<HuiFloatLabel label="Mentions 成員"
><HuiMentions v-model="model.mentions" :options="options"
/></HuiFloatLabel>
<HuiFloatLabel label="IconPicker 圖示"><HuiIconPicker v-model="model.icon" /></HuiFloatLabel>
</div>
</template>
<style scoped>
.float-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 24px;
padding-top: 8px;
}
@media (max-width: 600px) {
.float-grid {
grid-template-columns: minmax(0, 1fr);
}
}
</style>
一直浮起的控制項
數字框、日期範圍和帶前置內容的輸入框,標籤始終浮起,避免遮住按鈕、前綴和兩段提示文字。
vue
<script setup lang="ts">
import { ref } from 'vue'
import {
HuiFloatLabel,
HuiInputNumber,
HuiDatePicker,
HuiInput,
HuiSpace
} from '@nscode/handy-ui'
const count = ref<number | undefined>()
const range = ref<[string, string]>(['', ''])
const keyword = ref('')
</script>
<template>
<HuiSpace wrap align="start" size="large" style="padding-top: 8px">
<HuiFloatLabel label="InputNumber 數量" style="width: 200px"
><HuiInputNumber v-model="count"
/></HuiFloatLabel>
<HuiFloatLabel label="DatePicker 日期範圍" style="width: 300px"
><HuiDatePicker v-model="range" type="daterange"
/></HuiFloatLabel>
<HuiFloatLabel label="Input 前綴圖示" style="width: 240px"
><HuiInput v-model="keyword" prefix-icon="search"
/></HuiFloatLabel>
</HuiSpace>
</template>
尺寸
vue
<script setup lang="ts">
import { reactive } from 'vue'
import { HuiFloatLabel, HuiInput, HuiSpace } from '@nscode/handy-ui'
const sizes = ['large', 'default', 'small'] as const
const model = reactive({ large: '', default: '', small: '' })
</script>
<template>
<HuiSpace direction="vertical" fill size="large" style="padding-top: 8px">
<HuiFloatLabel v-for="size in sizes" :key="size" :label="size" style="width: 260px">
<HuiInput v-model="model[size]" :size="size" />
</HuiFloatLabel>
</HuiSpace>
</template>
工具列
vue
<script setup lang="ts">
import { ref } from 'vue'
import {
HuiFloatLabel,
HuiInput,
HuiSelect,
HuiButton,
HuiMessage,
HuiSpace
} from '@nscode/handy-ui'
const name = ref('')
const status = ref<string>()
const options = [
{ label: '啟用', value: 'enabled' },
{ label: '停用', value: 'disabled' }
]
const search = () =>
HuiMessage.info(`查詢:${name.value || '全部名稱'},${status.value || '全部狀態'}`)
</script>
<template>
<HuiSpace wrap align="center" style="padding-top: 8px">
<HuiFloatLabel label="名稱" style="width: 220px"
><HuiInput v-model="name" clearable
/></HuiFloatLabel>
<HuiFloatLabel label="狀態" style="width: 180px"
><HuiSelect v-model="status" :options="options" clearable
/></HuiFloatLabel>
<HuiButton type="primary" icon="search" @click="search">查詢</HuiButton>
</HuiSpace>
</template>
禁用
vue
<script setup lang="ts">
import { HuiFloatLabel, HuiInput, HuiSelect, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap size="large" style="padding-top: 8px">
<HuiFloatLabel label="空值禁用" style="width: 240px"><HuiInput disabled /></HuiFloatLabel>
<HuiFloatLabel label="已有值禁用" style="width: 240px"
><HuiInput model-value="研发部" disabled
/></HuiFloatLabel>
<HuiFloatLabel label="下拉禁用" style="width: 240px"
><HuiSelect disabled :options="[]"
/></HuiFloatLabel>
</HuiSpace>
</template>
顯式驗證狀態
invalid 為 true 時,標籤和受支援控制項的邊框變紅,第一個登記控制項帶 aria-invalid="true";該屬性不執行驗證。
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiFloatLabel, HuiInput, HuiSwitch, HuiSpace } from '@nscode/handy-ui'
const invalid = ref(true)
const name = ref('')
</script>
<template>
<HuiSpace direction="vertical" fill size="large">
<HuiSwitch v-model="invalid" aria-label="驗證失敗" />
<HuiFloatLabel label="部門名稱" :invalid="invalid" style="width: 260px; margin-top: 8px">
<HuiInput v-model="name" />
</HuiFloatLabel>
</HuiSpace>
</template>
跟隨表單驗證
表單項不再填寫 label。輸入後刪空、離開輸入框或點擊驗證,錯誤提示仍由表單項顯示。
vue
<script setup lang="ts">
import { reactive, ref } from 'vue'
import {
HuiFloatLabel,
HuiInput,
HuiForm,
HuiFormItem,
HuiButton,
HuiMessage,
HuiSpace
} from '@nscode/handy-ui'
const model = reactive({ name: '' })
const form = ref<InstanceType<typeof HuiForm>>()
const rules = { name: [{ required: true, message: '請輸入部門名稱', trigger: 'blur' as const }] }
const validate = async () => {
const result = await form.value?.validate()
if (result?.valid) HuiMessage.success('驗證通過')
}
</script>
<template>
<HuiForm ref="form" :model="model" :rules="rules" label-width="0" style="padding-top: 8px">
<HuiFormItem prop="name">
<HuiFloatLabel label="部門名稱" style="width: 260px"
><HuiInput v-model="model.name"
/></HuiFloatLabel>
</HuiFormItem>
<HuiSpace><HuiButton type="primary" @click="validate">驗證</HuiButton></HuiSpace>
</HuiForm>
</template>
自訂標籤
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiFloatLabel, HuiInput, HuiIcon } from '@nscode/handy-ui'
const name = ref('')
</script>
<template>
<HuiFloatLabel style="width: 260px; margin-top: 8px">
<template #label><HuiIcon name="user" size="inherit" /> 聯絡人 *</template>
<HuiInput v-model="name" />
</HuiFloatLabel>
</template>
手動有值狀態與原生輸入框
filled 只對 HandyUI 控制項顯式覆蓋自動判斷。原生輸入框不自動登記,標籤在上方顯示,螢幕閱讀器名稱由使用者提供。
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiFloatLabel, HuiInput, HuiSwitch, HuiSpace } from '@nscode/handy-ui'
const filled = ref(false)
const nativeValue = ref('')
</script>
<template>
<HuiSpace direction="vertical" fill size="large">
<HuiSwitch v-model="filled" aria-label="手動浮起" />
<HuiFloatLabel label="手動浮起" :filled="filled" style="width: 260px; margin-top: 8px"
><HuiInput
/></HuiFloatLabel>
<HuiFloatLabel label="原生輸入框" style="width: 260px">
<input v-model="nativeValue" aria-label="原生輸入框" />
</HuiFloatLabel>
</HuiSpace>
</template>
提示文字
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiFloatLabel, HuiInput } from '@nscode/handy-ui'
const name = ref('')
</script>
<template>
<HuiFloatLabel label="部門名稱" style="width: 260px; margin-top: 8px">
<HuiInput v-model="name" placeholder="例如:研發部" clearable />
</HuiFloatLabel>
</template>
Props
| 屬性 | 類型 | 預設值 | 說明 |
|---|---|---|---|
| label | string | — | 標籤文字,label 插槽優先 |
| filled | boolean | undefined | 手動指定有值,只對 HandyUI 控制項生效;不寫時自動判斷,判斷不了時標籤浮起 |
| invalid | boolean | undefined | 手動指定標籤和邊框驗證狀態;不寫時跟隨所在表單項 |
Events
無。
Slots
| 插槽 | 說明 |
|---|---|
| default | 單個輸入控制項 |
| label | 自訂標籤內容,覆蓋 label 屬性 |
方法
無。
類型
ts
interface FloatLabelProps {
label?: string
filled?: boolean
invalid?: boolean
}