# Message Editor

> Editing a sent message in place, and stepping between the versions it leaves behind.

Component · AI · Pro kit · [Live page](https://ui.twentyfirst.media/pro/components/MessageEditor/)

Editing a sent message in place, and stepping between the versions it leaves behind. `ChatMessage` uses both (`onEdit`, `versionCount`); they work on their own too.

**MessageEditor**: `value` / `defaultValue` / `onChange`, `onSubmit(text)`, `onCancel`, `hint` (default "The conversation runs again from here"), `submitLabel`, `busy`, `autoFocus`.

- Enter sends, Shift+Enter adds a line, Esc cancels. The field grows with the text and opens with the caret at the end.
- Say what happens: sending an edit runs the conversation again from that message.

**VersionSwitch**: "‹ 2/3 ›". `count`, `index` (0-based, default the newest) / `defaultIndex` / `onChange`, `busy` (the arrows wait while a new version is made), `label`.

- Show it on an edited user message and on a regenerated answer; the answer below follows the version shown.

## Example

```tsx
import * as React from "react";
import { MessageEditor, VersionSwitch } from "@twentyfirstmedia/pro";

export default function Preview() {
  const [version, setVersion] = React.useState(1);
  const [busy, setBusy] = React.useState(false);
  return (
    <div className="tf-panel" style={{ gap: 24 }}>
      <MessageEditor
        autoFocus={false}
        defaultValue={"Northwind wants 40 more seats before the renewal.\nUse the new price list."}
        onSubmit={() => {}}
        onCancel={() => {}}
      />
      <div className="tf-row" style={{ gap: 24, alignItems: "center" }}>
        <VersionSwitch count={3} index={version} onChange={setVersion} />
        <VersionSwitch
          count={3}
          index={2}
          busy={busy}
          onChange={() => {
            setBusy(true);
            setTimeout(() => setBusy(false), 1500);
          }}
        />
      </div>
    </div>
  );
}
```

## Props

### MessageEditor

| Prop | Type | Notes |
| --- | --- | --- |
| `value?` | `string` |  |
| `defaultValue?` | `string` |  |
| `onChange?` | `(v: string) => void` |  |
| `onSubmit?` | `(text: string) => void` | Enter (or the button) sends the new text. |
| `onCancel?` | `() => void` | Esc (or the button) cancels. |
| `hint?` | `string` | The line under the field (default "The conversation runs again from here"). |
| `submitLabel?` | `string` |  |
| `label?` | `string` |  |
| `busy?` | `boolean` |  |
| `autoFocus?` | `boolean` | Focuses the field with the caret at the end (default true). |
| `className?` | `string` | Extra class on the root element. |
| `style?` | `CSSProperties` | Inline style on the root element. |

### VersionSwitch

| Prop | Type | Notes |
| --- | --- | --- |
| `count` | `number` | How many versions there are. |
| `index?` | `number` | The version shown, 0-based (default the newest). |
| `defaultIndex?` | `number` |  |
| `onChange?` | `(index: number) => void` |  |
| `busy?` | `boolean` | While a new version is being made: the arrows wait. |
| `label?` | `string` |  |
| `className?` | `string` | Extra class on the root element. |
| `style?` | `CSSProperties` | Inline style on the root element. |
