用法
使用 v-model 指令来控制 PinInput 的值。
<script setup lang="ts">
const value = ref([])
</script>
<template>
<UPinInput v-model="value" />
</template>
使用 default-value prop 在您不需要控制其状态时设置初始值。
<template>
<UPinInput :default-value="['1', '2', '3']" />
</template>
类型
使用 type prop 来更改输入类型。默认为 text。
<template>
<UPinInput type="number" />
</template>
当
type 设置为 number 时,它将只接受数字字符。掩码
使用 mask prop 将输入视为密码。
<template>
<UPinInput mask :default-value="['1', '2', '3', '4', '5']" />
</template>
OTP
使用 otp prop 来启用一次性密码功能。启用后,移动设备可以自动检测并从短信消息或剪贴板内容中填充 OTP 代码,并支持自动填充。
<template>
<UPinInput otp />
</template>
长度
使用 length prop 来更改输入的数量。
<template>
<UPinInput :length="6" />
</template>
占位符
使用 placeholder 属性设置占位文本。
<template>
<UPinInput placeholder="○" />
</template>
颜色
使用 color prop 来更改 PinInput 获得焦点时的圆环颜色。
<template>
<UPinInput color="neutral" highlight placeholder="○" />
</template>
highlight 属性在此用于显示焦点状态。当发生验证错误时,它会在内部使用。变体
使用 variant prop 来更改 PinInput 的变体。
<template>
<UPinInput color="neutral" variant="subtle" placeholder="○" />
</template>
尺寸
使用 size prop 来更改 PinInput 的大小。
<template>
<UPinInput size="xl" placeholder="○" />
</template>
禁用
使用 disabled prop 来禁用 PinInput。
<template>
<UPinInput disabled placeholder="○" />
</template>
API
属性
| 属性 | 默认值 | 类型 |
|---|---|---|
as | 'div' | any此组件应渲染为的元素或组件。 |
color | 'primary' | "primary" | "secondary" | "success" | "info" | "warning" | "error" | "neutral" |
variant | 'outline' | "outline" | "soft" | "subtle" | "ghost" | "none" |
尺寸 | 'md' | "md" | "xs" | "sm" | "lg" | "xl" |
length | 5 | string | number输入字段的数量。 |
autofocus | boolean | |
autofocusDelay | 0 | number |
高亮 | boolean | |
defaultValue | PinInputValue<T>当 PinInput 初始渲染时,其默认值为 PinInput 的值。当您不需要控制其选中状态时使用。 | |
disabled | boolean当 | |
id | string元素的 ID | |
mask | boolean当 | |
modelValue | null | PinInputValue<T>pin input 的受控选中状态。可以绑定为 | |
name | string字段的名称。作为名称/值对的一部分随其所属表单提交。 | |
otp | boolean当 | |
placeholder | string用于空 pin-input 的占位符字符。 | |
required | boolean当为 | |
type | 'text' | T输入字段的输入类型。 |
ui | { root?: ClassNameValue; base?: ClassNameValue; } |
事件
| 事件 | 类型 |
|---|---|
update:modelValue | [value: PinInputValue<T>] |
complete | [value: PinInputValue<T>] |
change | [事件: Event] |
blur | [事件: Event] |
可访问属性
通过模板引用访问组件时,您可以使用以下内容:
| 名称 | 类型 |
|---|---|
inputsRef | Ref<ComponentPublicInstance[]> |
主题
app.config.ts
export default defineAppConfig({
ui: {
pinInput: {
slots: {
root: 'relative inline-flex items-center gap-1.5',
base: [
'rounded-md border-0 placeholder:text-dimmed text-center focus:outline-none disabled:cursor-not-allowed disabled:opacity-75',
'transition-colors'
]
},
variants: {
size: {
xs: {
base: 'size-6 text-xs'
},
sm: {
base: 'size-7 text-xs'
},
md: {
base: 'size-8 text-sm'
},
lg: {
base: 'size-9 text-sm'
},
xl: {
base: 'size-10 text-base'
}
},
variant: {
outline: 'text-highlighted bg-default ring ring-inset ring-accented',
soft: 'text-highlighted bg-elevated/50 hover:bg-elevated focus:bg-elevated disabled:bg-elevated/50',
subtle: 'text-highlighted bg-elevated ring ring-inset ring-accented',
ghost: 'text-highlighted bg-transparent hover:bg-elevated focus:bg-elevated disabled:bg-transparent dark:disabled:bg-transparent',
none: 'text-highlighted bg-transparent'
},
color: {
primary: '',
secondary: '',
success: '',
info: '',
warning: '',
error: '',
neutral: ''
},
highlight: {
true: ''
}
},
compoundVariants: [
{
color: 'primary',
variant: [
'outline',
'subtle'
],
class: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-primary'
},
{
color: 'primary',
highlight: true,
class: 'ring ring-inset ring-primary'
},
{
color: 'neutral',
variant: [
'outline',
'subtle'
],
class: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-inverted'
},
{
color: 'neutral',
highlight: true,
class: 'ring ring-inset ring-inverted'
}
],
defaultVariants: {
size: 'md',
color: 'primary',
variant: 'outline'
}
}
}
})
vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'
export default defineConfig({
plugins: [
vue(),
ui({
ui: {
pinInput: {
slots: {
root: 'relative inline-flex items-center gap-1.5',
base: [
'rounded-md border-0 placeholder:text-dimmed text-center focus:outline-none disabled:cursor-not-allowed disabled:opacity-75',
'transition-colors'
]
},
variants: {
size: {
xs: {
base: 'size-6 text-xs'
},
sm: {
base: 'size-7 text-xs'
},
md: {
base: 'size-8 text-sm'
},
lg: {
base: 'size-9 text-sm'
},
xl: {
base: 'size-10 text-base'
}
},
variant: {
outline: 'text-highlighted bg-default ring ring-inset ring-accented',
soft: 'text-highlighted bg-elevated/50 hover:bg-elevated focus:bg-elevated disabled:bg-elevated/50',
subtle: 'text-highlighted bg-elevated ring ring-inset ring-accented',
ghost: 'text-highlighted bg-transparent hover:bg-elevated focus:bg-elevated disabled:bg-transparent dark:disabled:bg-transparent',
none: 'text-highlighted bg-transparent'
},
color: {
primary: '',
secondary: '',
success: '',
info: '',
warning: '',
error: '',
neutral: ''
},
highlight: {
true: ''
}
},
compoundVariants: [
{
color: 'primary',
variant: [
'outline',
'subtle'
],
class: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-primary'
},
{
color: 'primary',
highlight: true,
class: 'ring ring-inset ring-primary'
},
{
color: 'neutral',
variant: [
'outline',
'subtle'
],
class: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-inverted'
},
{
color: 'neutral',
highlight: true,
class: 'ring ring-inset ring-inverted'
}
],
defaultVariants: {
size: 'md',
color: 'primary',
variant: 'outline'
}
}
}
})
]
})