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>
| Prop | Default | Type |
|---|---|---|
as | 'div' | anyThe element or component this component should render as. |
maxLength | 50 | number |
counterClass | '' | 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' | string | number | symbol |
variant | 'outline' | string | number | symbol |
size | 'md' | string | number | symbol |
autocomplete | string | |
autofocusDelay | number | |
defaultValue | string | number | bigint | false | true | |
modelModifiers | ModelModifiers<AcceptableValue> | |
icon | anyDisplay an icon based on the | |
avatar | AvatarPropsDisplay an avatar on the left side.
| |
leadingIcon | anyDisplay an icon on the left side. | |
trailingIcon | anyDisplay an icon on the right side. | |
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 | string | number | bigint | false | true | |
required | boolean | |
autofocus | boolean | |
disabled | boolean | |
highlight | booleanHighlight the ring color like a focus state. | |
leading | booleanWhen | |
trailing | booleanWhen | |
loading | booleanWhen | |
ui | { [x: string]: ClassNameValue; } |
| Event | Type |
|---|---|
update:modelValue | [value: AcceptableValue] |
blur | [event: FocusEvent] |
change | [event: Event] |
| Slot | Type |
|---|---|
leading | { ui: { [x: string]: (props?: Record<string, any>) => string; }; } |
default | { ui: { [x: string]: (props?: Record<string, any>) => string; }; } |