WithCharacterLimit
简介
MWithCharacterLimit 是一个带有字符数限制和实时计数显示的输入框组件。在输入框右侧实时显示当前字符数和最大字符限制,帮助用户控制输入长度。
基础用法
默认限制 50 个字符:
<script setup lang="ts">
const text = ref('')
</script>
<template>
<MWithCharacterLimit v-model="text" placeholder="最多输入 50 个字符..." />
</template>
自定义限制
通过 maxLength 设置最大字符数:
<script setup lang="ts">
const comment = ref('')
</script>
<template>
<MWithCharacterLimit v-model="comment" :max-length="200" placeholder="最多 200 字..." />
</template>
带图标
为输入框添加语义化图标:
<script setup lang="ts">
const message = ref('')
</script>
<template>
<MWithCharacterLimit
v-model="message"
leading-icon="i-lucide-message-square"
:max-length="100"
placeholder="写下你的想法..."
/>
</template>
不同尺寸
支持多种尺寸:
<script setup lang="ts">
const textSm = ref('')
const textMd = ref('')
const textLg = ref('')
</script>
<template>
<div class="space-y-4 flex flex-col">
<MWithCharacterLimit
v-model="textSm"
size="sm"
:max-length="30"
placeholder="Small"
/>
<MWithCharacterLimit
v-model="textMd"
size="md"
:max-length="50"
placeholder="Medium"
/>
<MWithCharacterLimit
v-model="textLg"
size="lg"
:max-length="80"
placeholder="Large"
/>
</div>
</template>
自定义计数器样式
通过 counterClass 自定义计数器样式:
<script setup lang="ts">
const content = ref('')
</script>
<template>
<MWithCharacterLimit
v-model="content"
:max-length="100"
counter-class="text-primary font-semibold"
placeholder="自定义计数器颜色"
/>
</template>
表单场景
在表单字段中限制输入长度:
<script setup lang="ts">
const form = reactive({
username: '',
bio: ''
})
</script>
<template>
<div class="space-y-4">
<UFormField label="用户名" hint="2-20 个字符">
<MWithCharacterLimit v-model="form.username" :max-length="20" placeholder="请输入用户名" />
</UFormField>
<UFormField label="个人简介" hint="不超过 140 字">
<MWithCharacterLimit v-model="form.bio" :max-length="140" placeholder="介绍一下自己..." />
</UFormField>
</div>
</template>
API
Props
| Prop | Default | Type |
|---|---|---|
as | 'div' | anyThe element or component this component should render as. |
maxLength | 50 | number |
counterClass | '' | null | string | false | 0 | 0n | ClassNameArray |
id | string | |
name | string | |
type | "number" | "color" | "button" | "checkbox" | "date" | "datetime-local" | "email" | "file" | "hidden" | "image" | "month" | "password" | "radio" | "range" | "reset" | "search" | "submit" | "tel" | "text" | "time" | "url" | "week" | string & {}
| |
placeholder | stringThe placeholder text when the input is empty. | |
color | 'primary' | "primary" | "secondary" | "success" | "info" | "warning" | "error" | "neutral" |
variant | 'outline' | "outline" | "soft" | "subtle" | "ghost" | "none" |
size | 'md' | "xs" | "sm" | "md" | "lg" | "xl" |
required | boolean | |
autocomplete | string | |
autofocus | boolean | |
autofocusDelay | number | |
disabled | boolean | |
highlight | boolean Highlight the ring color like a focus state. | |
defaultValue | null | string | number | bigint | false | true | |
modelModifiers | ModelModifiers<AcceptableValue> | |
icon | anyDisplay an icon based on the | |
avatar | AvatarPropsDisplay an avatar on the left side.
| |
leading | boolean When | |
leadingIcon | anyDisplay an icon on the left side. | |
trailing | boolean When | |
trailingIcon | anyDisplay an icon on the right side. | |
loading | boolean When | |
loadingIcon | appConfig.ui.icons.loading | anyThe icon when the |
list | string | |
max | string | number | |
maxlength | string | number | |
min | string | number | |
minlength | string | number | |
pattern | string | |
readonly | false | true | "true" | "false" | |
step | string | number | |
modelValue | null | string | number | bigint | false | true | |
ui | { root?: ClassNameValue; base?: ClassNameValue; leading?: ClassNameValue; leadingIcon?: ClassNameValue; leadingAvatar?: ClassNameValue; leadingAvatarSize?: ClassNameValue; trailing?: ClassNameValue; trailingIcon?: ClassNameValue; } |
Emits
| Event | Type |
|---|---|
update:modelValue | [value: AcceptableValue] |
blur | [event: FocusEvent] |
change | [event: Event] |
update:modelValue | [value: AcceptableValue | undefined] |
Slots
| Slot | Type |
|---|---|
leading | { ui: { root: (props?: Record<string, any> | undefined) => string; base: (props?: Record<string, any> | undefined) => string; leading: (props?: Record<string, any> | undefined) => string; leadingIcon: (props?: Record<string, any> | undefined) => string; leadingAvatar: (props?: Record<string, any> | undefined) => string; leadingAvatarSize: (props?: Record<string, any> | undefined) => string; trailing: (props?: Record<string, any> | undefined) => string; trailingIcon: (props?: Record<string, any> | undefined) => string; }; } |
default | { ui: { root: (props?: Record<string, any> | undefined) => string; base: (props?: Record<string, any> | undefined) => string; leading: (props?: Record<string, any> | undefined) => string; leadingIcon: (props?: Record<string, any> | undefined) => string; leadingAvatar: (props?: Record<string, any> | undefined) => string; leadingAvatarSize: (props?: Record<string, any> | undefined) => string; trailing: (props?: Record<string, any> | undefined) => string; trailingIcon: (props?: Record<string, any> | undefined) => string; }; } |