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
}