Skip to content

Commit 883ec18

Browse files
committed
propagate CopilotTextArea contents etc.
1 parent cce0b59 commit 883ec18

2 files changed

Lines changed: 44 additions & 14 deletions

File tree

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
import { CopilotTextarea } from "@copilotkit/react-textarea";
2+
import { useState } from "react";
23

34
export function VacationNotes(): JSX.Element {
4-
return <CopilotTextarea />;
5+
const [text, setText] = useState("");
6+
7+
return (
8+
<CopilotTextarea
9+
className="p-4"
10+
value={text}
11+
onChange={(value: string) => setText(value)}
12+
/>
13+
);
514
}

packages/react-textarea/src/components/copilot-textarea/copilot-textarea.tsx

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,18 @@ declare module "slate" {
5050
}
5151
}
5252

53-
export interface CopilotTextareaProps {}
53+
export interface CopilotTextareaProps {
54+
className?: string;
55+
placeholder?: string;
56+
value?: string;
57+
onChange?: (value: string) => void;
58+
}
5459

5560
export function CopilotTextarea(props: CopilotTextareaProps): JSX.Element {
5661
const initialValue: Descendant[] = [
5762
{
5863
type: "paragraph",
59-
children: [
60-
{ text: "A line of text in a paragraph.", isSuggestion: false },
61-
],
62-
},
63-
{
64-
type: "copilot-suggestion",
65-
children: [
66-
{ text: "A line of text in a paragraph.", isSuggestion: false },
67-
{ text: "A line of text in a paragraph.", isSuggestion: true },
68-
],
64+
children: [{ text: props.value || "", isSuggestion: false }],
6965
},
7066
];
7167

@@ -93,13 +89,38 @@ export function CopilotTextarea(props: CopilotTextareaProps): JSX.Element {
9389
);
9490
}, []);
9591

96-
const [editor] = useState(() => withReact(createEditor()));
92+
const [editor] = useState(() => {
93+
const editor = withReact(createEditor());
94+
editor.onChange = () => {
95+
const suggestionAwareTextComponents: SuggestionAwareText[][] =
96+
editor.children.map((node) => {
97+
if (Element.isElement(node)) {
98+
return node.children.map((child) => {
99+
return child;
100+
});
101+
} else {
102+
return [node];
103+
}
104+
});
105+
106+
const flattened = suggestionAwareTextComponents.reduce(
107+
(acc, val) => acc.concat(val),
108+
[]
109+
);
110+
const text = flattened
111+
.map((textComponent) => textComponent.text)
112+
.join("\n");
113+
114+
props.onChange?.(text);
115+
};
116+
return editor;
117+
});
97118

98119
return (
99120
// Add the editable component inside the context.
100121
<Slate editor={editor} initialValue={initialValue}>
101122
<Editable
102-
className="p-4"
123+
className={props.className}
103124
renderElement={renderElement}
104125
renderLeaf={renderLeaf}
105126
onKeyDown={(event) => {

0 commit comments

Comments
 (0)