# Contact form

Thu thập thông tin liên hệ, kiểm tra field trước khi host lưu.

## Key rules

- Element prototype là chuẩn để chatty-ui port theo.
- Host giữ dữ liệu, dịch nhãn và xử lý event; preview chỉ dùng dữ liệu cục bộ.

## Use when / Avoid

Dùng trong cuộc trò chuyện. Tránh nối trực tiếp element với API của shop.

### Use instead

| Component | Use when |
| --- | --- |
| `ContactComposer` | Implementation React đang chạy. |
| Element prototype | Consumer chuyển sang custom element. |

## Live preview

Example (Current (React), being replaced) — Component React thật; dữ liệu fixture, không gửi request.

```jsx
import React, {useState} from 'react';
import {ContactComposer} from '@avada/chatty-ui/canonical/ContactComposer.jsx';

export function Form() {
  const [values, setValues] = useState({email: '', firstName: '', phone: ''});
  const [status, setStatus] = useState('');
  return (
    <div>
      <ContactComposer
        fields={['email', 'name', 'phone']}
        values={values}
        required={{email: true}}
        t={text => text}
        description="Where can we reply?"
        privacy="Your email is used to reply to this conversation."
        autoFocus={false}
        onChange={(key, value) => setValues(previous => ({...previous, [key]: value}))}
        onSubmit={() => setStatus('Contact details validated locally. No message was sent.')}
        onSkip={() => setStatus('Anonymous chat selected locally.')}
      />
      <p role="status">{status}</p>
    </div>
  );
}
```

Example (Current (React), being replaced) — Component React thật; dữ liệu fixture, không gửi request.

```jsx
import React, {useState} from 'react';
import {ContactComposer} from '@avada/chatty-ui/canonical/ContactComposer.jsx';

export function Form() {
  const [values, setValues] = useState({email: '', firstName: '', phone: ''});
  const [status, setStatus] = useState('');
  return (
    <div>
      <ContactComposer
        fields={['email', 'name', 'phone']}
        values={values}
        required={{email: true}}
        t={text => text}
        description="Where can we reply?"
        privacy="Your email is used to reply to this conversation."
        autoFocus={false}
        onChange={(key, value) => setValues(previous => ({...previous, [key]: value}))}
        onSubmit={() => setStatus('Contact details validated locally. No message was sent.')}
        onSkip={() => setStatus('Anonymous chat selected locally.')}
      />
      <p role="status">{status}</p>
    </div>
  );
}
```

## Standard

Example (Standard) — Element prototype với token trung tính.

```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>`;
}
```

Admin chỉ dùng trong inbox và chỉ light; admin-dark là N/A. Surface chưa có React tương ứng ghi Not available bên dưới.

Gán `.fields`, `.values`, `.required` và `.strings`. `chatty-submit` mang `{values}` sau native validation; host đặt `loading` và `error`. Field lỗi đầu nhận focus. Email phải có domain với dấu chấm; phone chỉ nhận ký tự số, dấu cộng đầu, khoảng trắng, dấu ngoặc, dấu chấm và dấu gạch nối, với số chữ số theo rule của widget. `.strings.invalidEmail` và `.strings.invalidPhone` dịch lỗi tương ứng. Slot privacy giữ giải thích dùng dữ liệu.

Standard cố ý dùng nhãn field hiện rõ và nút Continue có chữ thay layout composer chỉ có placeholder của React. Cách này giữ accessible name khi đã nhập và giúp nhận biết field bắt buộc; khi port cần giữ nhãn, không đổi về placeholder-only. Field bắt buộc có dấu *. Required/semantic errors dùng `.strings` và aria-invalid; host error dùng critical. Pending giữ focus với read-only/aria-disabled và spinner.

### Element

| Element | Module |
| --- | --- |
| `chatty-contact-form` | @avada/chatty-ui/elements/contact-form.js |

### API

| Kind | Name | Type | Default | Description |
| --- | --- | --- | --- | --- |
| attribute | `fields` | array | `['email']` | Ordered field names or descriptors (key, label, type, required, payloadKey). |
| attribute | `description` | string | `'Where can we reply?'` | Form purpose shown before the fields. |
| attribute | `loading` | boolean | `false` | Keeps fields focusable and read-only while the host request is pending. |
| attribute | `skippable` | boolean | `false` | Shows the anonymous option. |
| attribute | `error` | string | `''` | Host request error. |
| event | `chatty-submit` | CustomEvent<{values: Object}> | Not available | Valid contact values. |
| event | `chatty-skip` | CustomEvent<{}> | Not available | Anonymous chat requested. |
| event | `click` | MouseEvent | Not available | Native contact action click. |
| slot | `privacy` | Not available | Not available | Privacy explanation under contact fields. |
| part | `base` | Not available | Not available | Contact form. |
| part | `error` | Not available | Not available | Critical host error. |
| part | `submit` | Not available | Not available | Submit button. |

## Classic

Token values dưới đây là fallback cục bộ; theme của caller có thể override.

### Import / element

| Component / element | Import / file | Scope root |
| --- | --- | --- |
| `ContactComposer` | @avada/chatty-ui/canonical/ContactComposer.jsx | ContactComposer |

### Anatomy

| Part | Element |
| --- | --- |
| 1 | ContactComposer |

### Placement / order

| Context | Order |
| --- | --- |
| Conversation | Caller supplies surrounding content |

### Props

| Prop | Type | Values | Default | Notes |
| --- | --- | --- | --- | --- |
| `fields` | See source | Caller supplied | See source | Enabled contact fields |

### Sizes

| Size | Height | Padding | Gap | Radius | Font role (px/rem) | Hit target | Breakpoint / mechanism |
| --- | --- | --- | --- | --- | --- | --- | --- |
| Field base | min-height 52px | 6px 9px 6px 14px | 10px | 28px | input-value | Native input box | Base CSS; caller may override |
| Grouped field | min-height 44px | 0 6px 0 12px | 8px | 17px | input-value | 44px | Multiple enabled fields |

### Tokens

| Part | Token | Light | Dark |
| --- | --- | --- | --- |
| Field text | `--chatty-contact-fg` | Fallback #1f1f1f | Fallback #1f1f1f |
| Field fill | `--chatty-contact-bg` | Fallback #f2f2f2 | Fallback #f2f2f2 |

### States

| State | Trigger | Visual change (values) | ARIA/attribute |
| --- | --- | --- | --- |
| Loading | loading prop | Controls disabled | aria-busy |
| Invalid | Field validation | Invalid field state | aria-invalid |

### Behaviour

| Interaction | Result |
| --- | --- |
| Submit | Validates contact fields before onSubmit |

### Copy

| String | i18n key | Where |
| --- | --- | --- |
| Caller translated | Not available | ContactComposer |

### Source

| File | Evidence |
| --- | --- |
|  | Owning React implementation. |

## AI Mode

Token values dưới đây là fallback cục bộ; theme của caller có thể override.

### Import / element

| Component / element | Import / file | Scope root |
| --- | --- | --- |
| `ContactComposer` | @avada/chatty-ui/canonical/ContactComposer.jsx | ContactComposer |

### Anatomy

| Part | Element |
| --- | --- |
| 1 | ContactComposer |

### Placement / order

| Context | Order |
| --- | --- |
| Conversation | Caller supplies surrounding content |

### Props

| Prop | Type | Values | Default | Notes |
| --- | --- | --- | --- | --- |
| `fields` | See source | Caller supplied | See source | Enabled contact fields |

### Sizes

| Size | Height | Padding | Gap | Radius | Font role (px/rem) | Hit target | Breakpoint / mechanism |
| --- | --- | --- | --- | --- | --- | --- | --- |
| Field base | min-height 52px | 6px 9px 6px 14px | 10px | 28px | input-value | Native input box | Base CSS; caller may override |
| Grouped field | min-height 44px | 0 6px 0 12px | 8px | 17px | input-value | 44px | Multiple enabled fields |

### Tokens

| Part | Token | Light | Dark |
| --- | --- | --- | --- |
| Field text | `--chatty-contact-fg` | Fallback #1f1f1f | Fallback #1f1f1f |
| Field fill | `--chatty-contact-bg` | Fallback #f2f2f2 | Fallback #f2f2f2 |

### States

| State | Trigger | Visual change (values) | ARIA/attribute |
| --- | --- | --- | --- |
| Loading | loading prop | Controls disabled | aria-busy |
| Invalid | Field validation | Invalid field state | aria-invalid |

### Behaviour

| Interaction | Result |
| --- | --- |
| Submit | Validates contact fields before onSubmit |

### Copy

| String | i18n key | Where |
| --- | --- | --- |
| Caller translated | Not available | ContactComposer |

### Source

| File | Evidence |
| --- | --- |
|  | Owning React implementation. |

## Do / Don't

Do: giữ host làm owner của dữ liệu.

Don't: gửi lại chỉ vì element render.

## Accessibility

| Surface | Role / name | Keyboard / focus | Live region | Contrast pair / ratio |
| --- | --- | --- | --- | --- |
| Classic | Named form | Enter submits; invalid field receives focus | Not available | See neutral token preview |
| AI Mode | Named form | Enter submits; invalid field receives focus | Not available | See neutral token preview |

