# Password Field

> A password input with a toggle that reveals the typed value.

```tsx title="hero.tsx"
import { Field, FieldLabel } from '@langgenius/dify-ui/field'
import {
  PasswordField,
  PasswordFieldInput,
  PasswordFieldToggle,
} from '@langgenius/dify-ui/password-field'

export function PasswordFieldHeroDemo() {
  return (
    <Field name="password" className="w-full max-w-80">
      <FieldLabel>Password</FieldLabel>
      <PasswordField>
        <PasswordFieldInput autoComplete="current-password" placeholder="Case-sensitive" />
        <PasswordFieldToggle aria-label="Show password" />
      </PasswordField>
    </Field>
  )
}
```

## Usage guidelines

- Use `PasswordField` for a password the user types and may reveal to check. It renders the [`InputGroup`](/components/input-group.md) surface, so its border, focus, invalid and disabled visuals match every other grouped input.
- Set `autoComplete` on `PasswordFieldInput`; the types require it. Use `current-password` for an existing password and `new-password` for a password being set, so password managers fill or suggest the right value. `PasswordFieldInput` owns `type`.
- Wrap the field in [`Field`](/components/field.md) with a `FieldLabel` for the label, validation and errors.
- `PasswordFieldToggle` is optional. Give it an `aria-label` that stays the same in both states; `aria-pressed` reports whether the value is revealed. It is a `type="button"` button, so it never submits the form. Pressing it with a pointer keeps focus in the input, and it stays available while the input is read-only.
- Set `disabled` on `PasswordField` to disable both the input and the toggle, and on the surrounding `Field` so `FieldLabel` receives `data-disabled` as well. A disabled `Field` alone reaches only the input.
- Keep `PasswordField` for the user's own account password. Credentials for other services, such as API keys, tokens and database passwords, use [`Input`](/components/input.md) with `type="password"` and `autoComplete="off"`: `current-password` invites the browser to fill the account password into them, and `new-password` makes it suggest a generated one. Some password managers ignore `autoComplete="off"` on masked inputs, so keep these fields out of sign-in and password forms.
- Ask for a new password once. The reveal toggle lets the user check what they typed, so a second "confirm password" input is unnecessary.

## Anatomy

```tsx
import {
  PasswordField,
  PasswordFieldInput,
  PasswordFieldToggle,
} from '@langgenius/dify-ui/password-field'
```

```tsx
<PasswordField>
  <PasswordFieldInput autoComplete="current-password" />
  <PasswordFieldToggle aria-label="Show password" />
</PasswordField>
```

The anatomy mirrors the upstream Base UI Password Field preview. The reveal behavior is a local implementation until Base UI ships the primitive; the parts then wrap it without changing call sites.

## Examples

### In a form

A password change form asks for the current password and one new password. A hidden `username` input tells password managers which account the new password belongs to. Native constraints and `validate` drive the field errors, which appear when the form is submitted and then update as the value changes. The submit button shows `loading` while the request runs and ignores repeated submits. A rejected current password comes back through `Form` `errors` and shows in the `FieldError` without `match`; enter `incorrect` as the current password to see it.

```tsx title="form.tsx"
import { Button } from '@langgenius/dify-ui/button'
import { Field, FieldDescription, FieldError, FieldLabel } from '@langgenius/dify-ui/field'
import { Form } from '@langgenius/dify-ui/form'
import {
  PasswordField,
  PasswordFieldInput,
  PasswordFieldToggle,
} from '@langgenius/dify-ui/password-field'
import * as React from 'react'

type PasswordValues = {
  currentPassword: string
  newPassword: string
}

export function PasswordFieldFormDemo() {
  const [errors, setErrors] = React.useState<Record<string, string>>({})
  const [status, setStatus] = React.useState<'idle' | 'saving' | 'saved'>('idle')
  const timerRef = React.useRef<number | null>(null)

  React.useEffect(() => {
    return () => {
      if (timerRef.current !== null) window.clearTimeout(timerRef.current)
    }
  }, [])

  function submit(values: PasswordValues) {
    if (status === 'saving') return
    setStatus('saving')
    timerRef.current = window.setTimeout(() => {
      timerRef.current = null
      if (values.currentPassword === 'incorrect') {
        setErrors({ currentPassword: 'The current password is incorrect.' })
        setStatus('idle')
        return
      }
      setErrors({})
      setStatus('saved')
    }, 800)
  }

  return (
    <Form<PasswordValues>
      aria-label="Change password"
      className="flex w-full max-w-80 flex-col gap-4"
      errors={errors}
      onChange={() => {
        if (status === 'saved') setStatus('idle')
      }}
      onFormSubmit={submit}
    >
      <input
        type="text"
        name="username"
        autoComplete="username"
        defaultValue="alex@example.com"
        readOnly
        hidden
      />
      <Field
        name="currentPassword"
        validate={(value) => (value ? null : 'Enter your current password.')}
      >
        <FieldLabel>Current password</FieldLabel>
        <PasswordField>
          <PasswordFieldInput autoComplete="current-password" placeholder="Case-sensitive" />
          <PasswordFieldToggle aria-label="Show current password" />
        </PasswordField>
        <FieldError />
      </Field>
      <Field name="newPassword">
        <FieldLabel>New password</FieldLabel>
        <PasswordField>
          <PasswordFieldInput
            autoComplete="new-password"
            required
            minLength={8}
            placeholder="At least 8 characters"
          />
          <PasswordFieldToggle aria-label="Show new password" />
        </PasswordField>
        <FieldDescription>At least 8 characters.</FieldDescription>
        <FieldError match="valueMissing">Enter a new password.</FieldError>
        <FieldError match="tooShort">Use at least 8 characters.</FieldError>
      </Field>
      <div className="flex items-center justify-between gap-3">
        <span role="status" className="min-w-0 truncate system-xs-regular text-text-success">
          {status === 'saved' ? 'Password changed' : ''}
        </span>
        <Button type="submit" variant="primary" loading={status === 'saving'} className="shrink-0">
          Change password
        </Button>
      </div>
    </Form>
  )
}
```

### Read-only and disabled

A read-only temporary password can still be revealed. Setting `disabled` on both `Field` and `PasswordField` disables the label, input and toggle together, and the description says why.

```tsx title="states.tsx"
import { Field, FieldDescription, FieldLabel } from '@langgenius/dify-ui/field'
import {
  PasswordField,
  PasswordFieldInput,
  PasswordFieldToggle,
} from '@langgenius/dify-ui/password-field'

export function PasswordFieldStatesDemo() {
  return (
    <div className="flex w-full max-w-80 flex-col gap-4">
      <Field name="temporaryPassword">
        <FieldLabel>Temporary password</FieldLabel>
        <PasswordField>
          <PasswordFieldInput
            autoComplete="new-password"
            defaultValue="vX7q-2mLp-9tRw"
            placeholder="Letters, digits and symbols"
            readOnly
          />
          <PasswordFieldToggle aria-label="Show temporary password" />
        </PasswordField>
        <FieldDescription>Share it with the new member. It expires in 24 hours.</FieldDescription>
      </Field>
      <Field name="password" disabled>
        <FieldLabel>Password</FieldLabel>
        <PasswordField disabled>
          <PasswordFieldInput autoComplete="current-password" />
          <PasswordFieldToggle aria-label="Show password" />
        </PasswordField>
        <FieldDescription>Your workspace requires single sign-on.</FieldDescription>
      </Field>
    </div>
  )
}
```

### Tokens and API keys

A model provider key is masked with `Input` and `type="password"`. `autoComplete="off"` asks the browser not to fill a saved password into it or suggest a generated one.

```tsx title="token.tsx"
import { Field, FieldDescription, FieldLabel } from '@langgenius/dify-ui/field'
import { Input } from '@langgenius/dify-ui/input'

export function PasswordFieldTokenDemo() {
  return (
    <Field name="openaiApiKey" className="w-full max-w-80">
      <FieldLabel>OpenAI API key</FieldLabel>
      <Input
        type="password"
        autoCapitalize="none"
        autoComplete="off"
        autoCorrect="off"
        spellCheck={false}
        placeholder="sk-…"
      />
      <FieldDescription>Stored encrypted and used only for this workspace.</FieldDescription>
    </Field>
  )
}
```

## Keyboard

| Key              | Action                                                |
| ---------------- | ----------------------------------------------------- |
| `Tab`            | Moves focus from the input to the toggle.             |
| `Enter`, `Space` | Reveals or masks the value when the toggle has focus. |

## API reference

### PasswordField

A compound password input with a built-in visibility toggle on the `InputGroup` surface. Renders a `<div>` element.

**Props**

| Prop        | Type      | Default | Description                                                                 |
| ----------- | --------- | ------- | --------------------------------------------------------------------------- |
| `disabled?` | `boolean` | `false` | Whether the input and the visibility toggle should ignore user interaction. |

### PasswordFieldInput

The password input inside `PasswordField`; requires `autoComplete` (`'current-password'` or `'new-password'`). Renders an `<input>` element.

**Props**

| Prop           | Type                                   | Default | Description                                                                                     |
| -------------- | -------------------------------------- | ------- | ----------------------------------------------------------------------------------------------- |
| `autoComplete` | `'current-password' \| 'new-password'` | —       | Tells password managers whether the field signs in with an existing password or sets a new one. |

Inherits `className`, `style`, `onValueChange`, `defaultValue`, `value` from [Base UI Input](https://base-ui.com/react/components/input).

**Data attributes**

| Attribute       | Description                                                                 |
| --------------- | --------------------------------------------------------------------------- |
| `data-disabled` | Present when the input is disabled.                                         |
| `data-valid`    | Present when the input is in a valid state (when wrapped in Field.Root).    |
| `data-invalid`  | Present when the input is in an invalid state (when wrapped in Field.Root). |
| `data-touched`  | Present when the input has been touched (when wrapped in Field.Root).       |
| `data-dirty`    | Present when the input's value has changed (when wrapped in Field.Root).    |
| `data-filled`   | Present when the input is filled (when wrapped in Field.Root).              |
| `data-focused`  | Present when the input is focused (when wrapped in Field.Root).             |

### PasswordFieldToggle

An inline-end add-on containing an icon button that toggles password visibility; requires `aria-label`. Renders a `<div>` element containing a `<button>`.

**Props**

| Prop         | Type      | Default | Description                                                                                                        |
| ------------ | --------- | ------- | ------------------------------------------------------------------------------------------------------------------ |
| `aria-label` | `string`  | —       | Names the toggle for assistive technology.                                                                         |
| `disabled?`  | `boolean` | `false` | Whether the toggle should ignore user interaction. Also disabled when the surrounding `PasswordField` is disabled. |
