---
title: "WithFloatingLabel"
description: "带浮动标签和清除按钮的输入框组件。"
seo_title: "WithFloatingLabel"
seo_description: "An input with a floating label and clear button, built on the Nuxt UI input."
canonical_url: "https://nuxt.mhaibaraai.cn/docs/components/with-floating-label"
---
# WithFloatingLabel

> 带浮动标签和清除按钮的输入框组件。

## 简介

`MWithFloatingLabel` 是一个带有浮动标签效果的输入框组件。标签在输入框为空时居中显示为占位符，聚焦或有内容时自动上浮，同时内置清除按钮，有内容时右侧自动显示。

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

## 用法

标签在空值时居中显示，聚焦或有内容时自动上浮：

```vue
<script setup lang="ts">
const value = ref("test@example.com")
</script>

<template>
  <MWithFloatingLabel label="邮箱地址" />
</template>
```

### `leadingIcon` 前置图标

通过 `leadingIcon` 为输入框添加前置图标：

```vue
<template>
  <MWithFloatingLabel label="用户名" leading-icon="i-lucide-user" />
</template>
```

### `size` 尺寸

通过 `size` 切换输入框与标签尺寸：

```vue
<template>
  <MWithFloatingLabel label="尺寸演示" size="md" />
</template>
```

### `clearButtonProps` 清除按钮

```vue
<script setup lang="ts">
const value = ref("test@example.com")
</script>

<template>
  <MWithFloatingLabel label="邮箱地址" :ui='{"label":"text-warning"}' :clear-button-props='{"color":"error","icon":"i-lucide-x"}' />
</template>
```

## 示例

### 清除事件

通过 `@clear` 事件监听清除操作：

```vue [ComponentsWithFloatingLabelClearExample.vue]
<script setup lang="ts">
const toast = useToast()
const email = ref('user@example.com')

function handleClear() {
  toast.add({
    title: '已清除',
    description: '输入内容已清空',
    color: 'neutral'
  })
}
</script>

<template>
  <MWithFloatingLabel v-model="email" label="邮箱地址" leading-icon="i-lucide-mail" @clear="handleClear" />
</template>
```

## API

### Props

```ts
/**
 * Props for the MWithFloatingLabel component
 */
interface MWithFloatingLabelProps {
  label?: string | undefined;
  size?: "xs" | "sm" | "md" | "lg" | "xl" | undefined;
  clearButtonProps?: ButtonProps | undefined;
  ui?: (Record<string, C> & { root?: SlotClass; base?: SlotClass; leading?: SlotClass; leadingIcon?: SlotClass; leadingAvatar?: SlotClass; leadingAvatarSize?: SlotClass; trailing?: SlotClass; trailingIcon?: SlotClass; label?: SlotClass; labelText?: 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;
  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 MWithFloatingLabel component
 */
interface MWithFloatingLabelEmits {
  update:modelValue: (payload: [value: T]) => void;
  blur: (payload: [event: FocusEvent]) => void;
  change: (payload: [event: Event]) => void;
  clear: (payload: []) => void;
  update:modelValue: (payload: [value: T | undefined]) => void;
}
```

### Slots

```ts
/**
 * Slots for the MWithFloatingLabel component
 */
interface MWithFloatingLabelSlots {
  leading(): any;
}
```

## Theme

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

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


## Sitemap

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