---
title: "WithCharacterLimit"
description: "带字符数限制和计数显示的输入框组件。"
seo_title: "WithCharacterLimit"
seo_description: "An input with character limit and live counter display, built on the Nuxt UI input."
canonical_url: "https://nuxt.mhaibaraai.cn/docs/components/with-character-limit"
---
# WithCharacterLimit

> 带字符数限制和计数显示的输入框组件。

## 简介

`MWithCharacterLimit` 是一个带有字符数限制和实时计数显示的输入框组件。在输入框右侧实时显示当前字符数和最大字符限制，帮助用户控制输入长度。

> \[\!NOTE\]
> See: https://ui.nuxt.com/docs/components/input
> 
> 基于 Nuxt UI 的 Input 组件封装

## 用法

默认限制 50 个字符，右侧实时显示已输入与上限：

```vue
<script setup lang="ts">
const value = ref("最多输入字符...")
</script>

<template>
  <MWithCharacterLimit />
</template>
```

### `maxLength` 字符上限

通过 `maxLength` 设置最大字符数：

```vue
<template>
  <MWithCharacterLimit :max-length="10" />
</template>
```

### `leadingIcon` 前置图标

通过 `leadingIcon` 为输入框添加语义化图标：

```vue
<template>
  <MWithCharacterLimit leading-icon="i-lucide-message-square" />
</template>
```

### `size` 尺寸

通过 `size` 切换输入框与计数器尺寸：

```vue
<template>
  <MWithCharacterLimit size="md" />
</template>
```

### `ui` 样式

通过 `ui.counter` 自定义计数器样式：

```vue
<template>
  <MWithCharacterLimit :max-length="100" :ui='{"counter":"text-success"}' />
</template>
```

## API

### Props

```ts
/**
 * Props for the MWithCharacterLimit component
 */
interface MWithCharacterLimitProps {
  /**
   * 最大允许输入的字符数
   * @default "50"
   */
  maxLength?: number | undefined;
  ui?: (Record<string, C> & { root?: SlotClass; base?: SlotClass; leading?: SlotClass; leadingIcon?: SlotClass; leadingAvatar?: SlotClass; leadingAvatarSize?: SlotClass; trailing?: SlotClass; trailingIcon?: SlotClass; counter?: SlotClass; }) | undefined;
  /**
   * The element or component this component should render as.
   */
  as?: any;
  id?: string | undefined;
  name?: string | undefined;
  type?: InputTypeHTMLAttribute | undefined;
  /**
   * The placeholder text when the input is empty.
   */
  placeholder?: string | undefined;
  color?: "primary" | "secondary" | "info" | "success" | "warning" | "error" | "important" | "neutral" | undefined;
  variant?: "outline" | "soft" | "subtle" | "ghost" | "none" | undefined;
  size?: "xs" | "sm" | "md" | "lg" | "xl" | undefined;
  required?: boolean | undefined;
  autocomplete?: (string & {}) | "on" | "off" | undefined;
  autofocus?: boolean | undefined;
  autofocusDelay?: number | undefined;
  disabled?: boolean | undefined;
  /**
   * Highlight the ring color like a focus state.
   */
  highlight?: boolean | undefined;
  /**
   * Keep the mobile text size on all breakpoints.
   */
  fixed?: boolean | undefined;
  defaultValue?: T | undefined;
  modelModifiers?: ModelModifiers | undefined;
  /**
   * Display an icon based on the `leading` and `trailing` props.
   */
  icon?: any;
  /**
   * Display an avatar on the left side.
   */
  avatar?: AvatarProps | undefined;
  /**
   * When `true`, the icon will be displayed on the left side.
   */
  leading?: boolean | undefined;
  /**
   * Display an icon on the left side.
   */
  leadingIcon?: any;
  /**
   * When `true`, the icon will be displayed on the right side.
   */
  trailing?: boolean | undefined;
  /**
   * Display an icon on the right side.
   */
  trailingIcon?: any;
  /**
   * When `true`, the loading icon will be displayed.
   */
  loading?: boolean | undefined;
  /**
   * The icon when the `loading` prop is `true`.
   */
  loadingIcon?: any;
  enterKeyHint?: "search" | "enter" | "done" | "go" | "next" | "previous" | "send" | undefined;
  form?: string | undefined;
  formaction?: string | undefined;
  formenctype?: string | undefined;
  formmethod?: string | undefined;
  formnovalidate?: Booleanish | undefined;
  formtarget?: string | undefined;
  list?: string | undefined;
  max?: Numberish | undefined;
  maxlength?: Numberish | undefined;
  min?: Numberish | undefined;
  minlength?: Numberish | undefined;
  pattern?: string | undefined;
  readonly?: Booleanish | undefined;
  step?: Numberish | undefined;
  modelValue?: T | undefined;
}
```

### Emits

```ts
/**
 * Emitted events for the MWithCharacterLimit component
 */
interface MWithCharacterLimitEmits {
  update:modelValue: (payload: [value: T]) => void;
  blur: (payload: [event: FocusEvent]) => void;
  change: (payload: [event: Event]) => void;
  update:modelValue: (payload: [value: T | undefined]) => void;
}
```

### Slots

```ts
/**
 * Slots for the MWithCharacterLimit component
 */
interface MWithCharacterLimitSlots {
  leading(): any;
  default(): any;
}
```

## Theme

<component-theme></component-theme>## Changelog

See commit history for [src/runtime/components/input/WithCharacterLimit.vue](https://github.com/mhaibaraai/movk-nuxt/commits/main/src/runtime/components/input/WithCharacterLimit.vue).


## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
