---
title: "Password Input"
description: "A password input with optional visibility toggle."
url: https://weme-ui.github.io/slim/components/password-input.md
---
# Password Input

密码输入控件，基于 `Input`，内置可见性切换。`type` 由可见性决定。没有 `loading`、`suffix`、清除、字数、`autocomplete`。可以单独用，也可以放进 `FormField`。有 `FormField` 时会合并 `id` / `aria-*` / `data-*`。

## Installation

```bash
pnpm dlx @weme-ui/weme-ui add password-input
```

## Usage

```vue
<script lang="ts" setup>
import * as v from 'valibot'
import Button from '../../button/button.vue'
import FormField from '../../form-field/form-field.vue'
import Form from '../../form/form.vue'
import PasswordInput from '../password-input.vue'

const schema = v.object({
  password: v.pipe(v.string('密码不能为空'), v.minLength(8, '至少 8 位')),
})

const defaultValues = {
  password: '',
}

function onFormSubmit(values: { password: string }) {
  // eslint-disable-next-line no-alert
  alert(JSON.stringify(values))
}
</script>

<template>
  <Form
    class="max-w-sm w-full"
    :default-values="defaultValues"
    :schema="schema"
    @form-submit="onFormSubmit"
  >
    <FormField v-slot="{ field }" name="password" label="Password" required>
      <PasswordInput
        :model-value="field.state.value"
        placeholder="Enter password"
        toggle-visibility
        @blur="field.api.handleBlur"
        @update:model-value="field.api.handleChange"
      />
    </FormField>
    <Button type="submit"> Submit </Button>
  </Form>
</template>
```

## Props

| 属性               | 说明                                                                         | 类型                                    | 默认值              |
| ------------------ | ---------------------------------------------------------------------------- | --------------------------------------- | ------------------- |
| `as`               | 根节点渲染为的元素或组件（Reka UI `Primitive`）                              | `any`                                   | `'div'`             |
| `modelValue`       | 受控值                                                                       | `string \| number \| null \| undefined` | —                   |
| `defaultValue`     | 非受控初始值                                                                 | `string \| number \| null \| undefined` | —                   |
| `visible`          | 密码是否明文显示；可用 `v-model:visible`                                     | `boolean`                               | `false`             |
| `toggleVisibility` | 是否显示可见性切换控件（`suffix` 位）                                        | `boolean`                               | —                   |
| `visibleIcon`      | 明文态图标（Iconify）                                                        | `IconifyIconProps['icon']`              | `'ri:eye-line'`     |
| `invisibleIcon`    | 掩码态图标（Iconify）                                                        | `IconifyIconProps['icon']`              | `'ri:eye-off-line'` |
| `variant`          | 外观变体                                                                     | `'soft' \| 'outline'`                   | `'soft'`            |
| `unstyled`         | 移除样式变体类，仅保留结构                                                   | `boolean`                               | —                   |
| `size`             | 尺寸                                                                         | `'sm' \| 'md' \| 'lg'`                  | `'md'`              |
| `placeholder`      | 占位文案                                                                     | `string`                                | —                   |
| `autoFocus`        | 挂载后聚焦原生 `<input>`                                                     | `boolean`                               | —                   |
| `prefix`           | 前缀文案（无 `prefixIcon` 时展示）                                           | `string`                                | —                   |
| `prefixIcon`       | 前缀图标（Iconify）                                                          | `IconifyIconProps['icon']`              | —                   |
| `id`               | 控件 `id`；未传时回退到 FormField Context                                    | `string`                                | —                   |
| `name`             | 控件 `name`；未传时回退到 FormField Context                                  | `string`                                | —                   |
| `required`         | 是否必填；会与 FormField Context 的 `required` 合并                          | `boolean`                               | —                   |
| `disabled`         | 是否禁用；会与 FormField Context 的 `disabled` 合并                          | `boolean`                               | —                   |
| `invalid`          | 是否无效；会与 FormField Context 的 `invalid` 合并                           | `boolean`                               | —                   |
| `class`            | 根节点额外 class                                                             | `any`                                   | —                   |
| `ui`               | 覆盖 slots 样式（`root` / `prefix` / `prefixIcon` / `input` / `visibility`） | `Partial<PasswordInputStyleSlots>`      | —                   |

## Events

| 事件                | 说明                                                                           | 参数                                           |
| ------------------- | ------------------------------------------------------------------------------ | ---------------------------------------------- |
| `update:modelValue` | 值变更时触发（含 `v-model`）                                                   | `value: string \| number \| null \| undefined` |
| `update:visible`    | 可见性变更时触发（含 `v-model:visible`）                                       | `value: boolean`                               |
| `change`            | 原生 `change` 时触发；参数为当前输入框字符串（非原生 `Event`；透传自 `Input`） | `value: string \| number \| null \| undefined` |
| `enter`             | 在输入框按 `Enter` 时触发；参数为当前输入框值（透传自 `Input`）                | `value: string \| number \| null \| undefined` |
| `focus`             | 获得焦点                                                                       | `event: FocusEvent`                            |
| `blur`              | 失去焦点                                                                       | `event: FocusEvent`                            |

## Slots

| 插槽         | 说明                                                                                                    |
| ------------ | ------------------------------------------------------------------------------------------------------- |
| `default`    | 透传至 `Input` 的默认插槽                                                                               |
| `visibility` | 仅 `toggleVisibility` 为真时渲染；作用域参数 `{ visible: boolean }`；未传时为带 `aria-label` 的切换按钮 |

## Accessibility

基于 `Input`（原生 `<input>` + FormField Context 的 `id` / `name` / `disabled` / `required` / `aria-*` / `data-*`）。开启 `toggleVisibility` 时，可见性切换为 `type="button"`，默认 `aria-label="Toggle password visibility"`；明文时输入 `type="text"`，掩码时 `type="password"`。

### Keyboard Interactions

| Key               | Description                                   |
| ----------------- | --------------------------------------------- |
| `Enter`           | 在输入框内触发 `enter` 事件（行为同 `Input`） |
| `Space` / `Enter` | 在可见性按钮上激活切换                        |

## Examples

### Variants

```vue
<script lang="ts" setup>
import PasswordInput from '../password-input.vue'
</script>

<template>
  <div class="flex flex-col gap-3 max-w-sm w-full">
    <PasswordInput toggle-visibility variant="soft" placeholder="Soft" />

    <PasswordInput toggle-visibility variant="outline" placeholder="Outline" />
  </div>
</template>
```

### Sizes

```vue
<script lang="ts" setup>
import PasswordInput from '../password-input.vue'
</script>

<template>
  <div class="flex flex-col gap-3 max-w-sm w-full">
    <PasswordInput toggle-visibility size="sm" placeholder="Small" />

    <PasswordInput toggle-visibility size="md" placeholder="Medium" />

    <PasswordInput toggle-visibility size="lg" placeholder="Large" />
  </div>
</template>
```

### With Prefix

```vue
<script lang="ts" setup>
import PasswordInput from '../password-input.vue'
</script>

<template>
  <div class="max-w-sm w-full">
    <PasswordInput toggle-visibility prefix-icon="ri:lock-line" placeholder="Enter password" />
  </div>
</template>
```

### Toggle Visibility

```vue
<script lang="ts" setup>
import PasswordInput from '../password-input.vue'
</script>

<template>
  <div class="flex flex-col gap-3 max-w-sm w-full">
    <PasswordInput toggle-visibility placeholder="Toggle enabled" />

    <PasswordInput placeholder="Toggle disabled" />
  </div>
</template>
```

### Visible

```vue
<script lang="ts" setup>
import PasswordInput from '../password-input.vue'
</script>

<template>
  <div class="max-w-sm w-full">
    <PasswordInput toggle-visibility visible placeholder="Visible by default" />
  </div>
</template>
```

### Disabled

```vue
<script lang="ts" setup>
import PasswordInput from '../password-input.vue'
</script>

<template>
  <div class="max-w-sm w-full">
    <PasswordInput toggle-visibility disabled placeholder="Disabled" />
  </div>
</template>
```

### Invalid

```vue
<script lang="ts" setup>
import PasswordInput from '../password-input.vue'
</script>

<template>
  <div class="flex flex-col gap-3 max-w-sm w-full">
    <PasswordInput placeholder="Default" />
    <PasswordInput invalid placeholder="Invalid" />
  </div>
</template>
```

### Unstyled

```vue
<script lang="ts" setup>
import PasswordInput from '../password-input.vue'
</script>

<template>
  <div class="max-w-sm w-full">
    <PasswordInput
      toggle-visibility
      unstyled
      placeholder="Fully custom…"
      class="text-sm outline-none border-b b-default flex gap-1 w-full items-center focus-within:b-accent"
    />
  </div>
</template>
```