# chatty-text-field

API chuẩn của element chatty-text-field, sinh từ custom-elements.json của prototype trong guide.

## Key rules

- `chatty-text-field` khai báo 11 attribute, 0 property không có attribute, 2 event, 1 slot, 4 part và 3 CSS custom property.
- Import 1 module `@avada/chatty-ui/elements/text-field.js` là define `chatty-text-field`; chunk chỉ import element nó dùng. Prototype chuẩn:  trong guide.
- Đổi tên attribute, event, slot, part hay CSS custom property là đổi API: diff của custom-elements.json phải có trong 1 MR.

## Overview

A form-associated single-line input with its own label and error message. Class `ChattyTextField`.

### Attributes

| Name | Property | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `label` | `label` | `string` | `''` | Visible label and accessible name of the input. |
| `value` | `value` | `string` | `''` | Current value; also the form value. |
| `name` | `name` | `string` | `''` | Form field name. |
| `type` | `type` | `'text' \| 'email' \| 'tel' \| 'url' \| 'search' \| 'password'` | `'text'` | Native input type. |
| `placeholder` | `placeholder` | `string` | `''` | Hint shown while empty; not a label. |
| `autocomplete` | `autocomplete` | `string` | `''` | Native autocomplete token, e.g. email. |
| `size` | `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | Input height 32, 40 or 48px; touch raises sm and md to 44px. |
| `error` | `error` | `string` | `''` | Error message; sets aria-invalid and a custom validity. |
| `disabled` | `disabled` | `boolean` | `false` | Disables the input; not submitted. |
| `required` | `required` | `boolean` | `false` | Requires a value (valueMissing when empty). |
| `readonly` | `readonly` | `boolean` | `false` | Value can be read and focused but not edited. |

### Properties

Attributes also expose the properties listed in the Property column above.

Not declared in the manifest.

### Events

| Name | Detail type | Description |
| --- | --- | --- |
| `change` | `Event` | Committed edit, re-dispatched from the host (native change does not cross the shadow root). |
| `input` | `InputEvent` | Native input on every edit; value is on the host. |

### Slots

| Name | Description |
| --- | --- |
| `help` | Help text under the input; also its accessible description. |

### Parts

| Name | Description |
| --- | --- |
| `base` | Field wrapper (label, input, messages). |
| `label` | Visible label. |
| `control` | Native input. |
| `error` | Error message. |

### CSS custom properties

| Name | Default | Description |
| --- | --- | --- |
| `--chatty-text-field-radius` | – | Input corner radius. |
| `--chatty-text-field-height` | – | Input height; overrides size. |
| `--chatty-text-field-border-color` | – | Input border colour; error uses the critical colour. |

## Usage

```js
import '@avada/chatty-ui/tokens/tokens.css';
import '@avada/chatty-ui/elements/text-field.js';
```

```html
<chatty-text-field label="Name" size="sm" placeholder="Your name" style="flex-basis:100%;max-inline-size:320px"></chatty-text-field>
<chatty-text-field label="Email" type="email" autocomplete="email" placeholder="you@example.com" style="flex-basis:100%;max-inline-size:320px"></chatty-text-field>
<chatty-text-field label="Order number" size="lg" value="#1001" style="flex-basis:100%;max-inline-size:320px"
  ><span slot="help">Find it in your confirmation email.</span></chatty-text-field
>
```

## Live preview

Component thật dùng cùng story với trang component; dữ liệu mẫu nằm trong frame cục bộ.

Example (Standard) — Default: trạng thái mặc định và nội dung mẫu của element.

```html
<chatty-text-field label="Name" size="sm" placeholder="Your name" style="flex-basis:100%;max-inline-size:320px"></chatty-text-field>
<chatty-text-field label="Email" type="email" autocomplete="email" placeholder="you@example.com" style="flex-basis:100%;max-inline-size:320px"></chatty-text-field>
<chatty-text-field label="Order number" size="lg" value="#1001" style="flex-basis:100%;max-inline-size:320px"
  ><span slot="help">Find it in your confirmation email.</span></chatty-text-field
>
```

Example (Standard) — Các variant hoặc state chính đặt cạnh nhau trong cùng ví dụ.

```html
<chatty-text-field label="Email" type="email" value="anna@" error="Enter a valid email address." style="flex-basis:100%;max-inline-size:320px"></chatty-text-field>
<chatty-text-field label="Phone" type="tel" required style="flex-basis:100%;max-inline-size:320px"></chatty-text-field>
<chatty-text-field label="Store" value="example.myshopify.com" disabled style="flex-basis:100%;max-inline-size:320px"></chatty-text-field>
<chatty-text-field label="Ticket" value="T-2041" readonly style="flex-basis:100%;max-inline-size:320px"></chatty-text-field>
```

## Anatomy

Số trong hình khớp bảng bên dưới. Phần chỉ hiện ở state khác được ghi Not shown; ví dụ không thêm phần tử giả.

Example (Standard) — Anatomy: một instance mặc định riêng, có khoảng trống quanh các phần được đánh số.

```html
<chatty-text-field label="Name" size="sm" placeholder="Your name" style="flex-basis:100%;max-inline-size:320px"></chatty-text-field>
<chatty-text-field label="Email" type="email" autocomplete="email" placeholder="you@example.com" style="flex-basis:100%;max-inline-size:320px"></chatty-text-field>
<chatty-text-field label="Order number" size="lg" value="#1001" style="flex-basis:100%;max-inline-size:320px"
  ><span slot="help">Find it in your confirmation email.</span></chatty-text-field
>
```

| Part | Element | Source |
| --- | --- | --- |
| 1 | CSS part `base` | chatty-ui/custom-elements.json |
| 2 | CSS part `label` | chatty-ui/custom-elements.json |
| 3 | CSS part `control` | chatty-ui/custom-elements.json |
| 4 | CSS part `error` | chatty-ui/custom-elements.json |

## Keyboard

| Key | Action |
| --- | --- |
| Tab / Shift+Tab | Focus or leave the native text control. |

### Source

- chatty-ui/custom-elements.json
