# chatty-contact-form

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

## Key rules

- `chatty-contact-form` khai báo 5 attribute, 4 property không có attribute, 3 event, 1 slot, 3 part và 0 CSS custom property.
- Import 1 module `@avada/chatty-ui/elements/contact-form.js` là define `chatty-contact-form`; 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

Validates contact details locally; the host owns persistence and pending state. Class `ChattyContactForm`.

### Attributes

| Name | Property | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `fields` | `fields` | `array` | `['email']` | Ordered field names or descriptors; payloadKey defaults to key. Descriptors have key, label, optional type (text/email/tel/textarea), required, payloadKey and placeholder. |
| `description` | `description` | `string` | `'Where can we reply?'` | Form purpose shown before the fields. |
| `loading` | `loading` | `boolean` | `false` | Keeps fields focusable and read-only while the host request is pending. |
| `skippable` | `skippable` | `boolean` | `false` | Shows the anonymous option. |
| `error` | `error` | `string` | `''` | Host request error. |

### Properties

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

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `values` | `object` | `{}` | Contact values keyed by each field’s payloadKey. |
| `required` | `object` | `{email: true}` | Per-field required flags. |
| `strings` | `object` | `{ email: 'Email address', name: 'Name', phone: 'Phone number', submit: 'Continue', skip: 'Chat anonymously', invalidEmail: 'Enter a valid email address.', invalidPhone: 'Enter a valid phone number.', requiredEmail: 'Enter your email address.', requiredName: 'Enter your name.', requiredPhone: 'Enter your phone number.', requiredField: 'Complete this field.' }` | Translated field, action and validation labels. |
| `skipping` | `boolean` | `false` | Host-owned anonymous request pending; blocks repeated submit and skip. |

### Events

| Name | Detail type | Description |
| --- | --- | --- |
| `chatty-submit` | `CustomEvent<{values: Object}>` | Valid contact values. |
| `chatty-skip` | `CustomEvent<{}>` | Anonymous chat requested. |
| `click` | `MouseEvent` | Native contact action click. |

### Slots

| Name | Description |
| --- | --- |
| `privacy` | Privacy explanation under contact fields. |

### Parts

| Name | Description |
| --- | --- |
| `base` | Contact form. |
| `error` | Inline field or host error. |
| `submit` | Submit button. |

### CSS custom properties

Not declared in the manifest.

## Usage

```js
import '@avada/chatty-ui/tokens/tokens.css';
import {html} from 'lit';
import '@avada/chatty-ui/elements/contact-form.js';
export function Standard() {
  return html`<div
    @chatty-submit=${(event) => {
      event.currentTarget.querySelector('[role=status]').textContent =
        `Received: ${event.detail.values.email}`;
    }}
  >
    <chatty-contact-form
      .fields=${[
        {key: 'email', label: 'Email address', type: 'email', required: true},
        {
          key: 'description',
          payloadKey: 'issue',
          label: 'Describe your issue',
          type: 'textarea',
          required: true,
        },
        {key: 'order', label: 'Order number', type: 'text'},
      ]}
    >
      <small slot="privacy">Your email is used to reply to this conversation.</small>
    </chatty-contact-form>
    <p role="status"></p>
  </div>`;
}
```

## 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.

```js
import '@avada/chatty-ui/tokens/tokens.css';
import {html} from 'lit';
import '@avada/chatty-ui/elements/contact-form.js';
export function Standard() {
  return html`<div
    @chatty-submit=${(event) => {
      event.currentTarget.querySelector('[role=status]').textContent =
        `Received: ${event.detail.values.email}`;
    }}
  >
    <chatty-contact-form
      .fields=${[
        {key: 'email', label: 'Email address', type: 'email', required: true},
        {
          key: 'description',
          payloadKey: 'issue',
          label: 'Describe your issue',
          type: 'textarea',
          required: true,
        },
        {key: 'order', label: 'Order number', type: 'text'},
      ]}
    >
      <small slot="privacy">Your email is used to reply to this conversation.</small>
    </chatty-contact-form>
    <p role="status"></p>
  </div>`;
}
```

## 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ố.

```js
import '@avada/chatty-ui/tokens/tokens.css';
import {html} from 'lit';
import '@avada/chatty-ui/elements/contact-form.js';
export function Standard() {
  return html`<div
    @chatty-submit=${(event) => {
      event.currentTarget.querySelector('[role=status]').textContent =
        `Received: ${event.detail.values.email}`;
    }}
  >
    <chatty-contact-form
      .fields=${[
        {key: 'email', label: 'Email address', type: 'email', required: true},
        {
          key: 'description',
          payloadKey: 'issue',
          label: 'Describe your issue',
          type: 'textarea',
          required: true,
        },
        {key: 'order', label: 'Order number', type: 'text'},
      ]}
    >
      <small slot="privacy">Your email is used to reply to this conversation.</small>
    </chatty-contact-form>
    <p role="status"></p>
  </div>`;
}
```

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

## Keyboard

| Key | Action |
| --- | --- |
| Enter | Validate and submit the contact fields; IME composition does not submit. |

### Source

- chatty-ui/custom-elements.json
