Templates in React Inline AI Assist
11 Aug 202624 minutes to read
The Inline AI Assist provides the template options to customize the response and footer items.
Editor template
You can use the editorTemplate property to customize the default footer area and manage prompt request actions in the Inline AI Assist. This allows users to create unique footers that meet their specific needs.
import { InlineAIAssistComponent } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { enableRipple } from '@syncfusion/ej2-base';
enableRipple(true);
function App() {
const assistRef = React.useRef(null);
const editableRef = React.useRef(null);
const handleSummarizeBtnClick = () => {
if (assistRef.current && typeof assistRef.current.showPopup === 'function') {
assistRef.current.showPopup();
}
};
const footerContent = () => {
return (
<div className="custom-footer">
<textarea id="promptTextArea" className="e-input" rows={2} placeholder="Enter your prompt here"></textarea>
<button id="sendPrompt" className="e-btn e-primary" onClick={() => {
const textArea = document.getElementById('promptTextArea');
if (textArea && assistRef.current) {
assistRef.current.executePrompt(textArea.value);
textArea.value = '';
}
}}>Generate</button>
</div>
);
};
const handlePromptRequest = () => {
setTimeout(() => {
const defaultResponse = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
if (assistRef.current && typeof assistRef.current.addResponse === 'function') {
assistRef.current.addResponse(defaultResponse);
}
}, 1000);
};
const handleResponseItemSelect = (args) => {
if (args.command.label === 'Accept') {
if (editableRef.current && assistRef.current) {
const lastResponse = assistRef.current.prompts && assistRef.current.prompts.length > 0
? assistRef.current.prompts[assistRef.current.prompts.length - 1].response
: '';
if (lastResponse) {
editableRef.current.innerHTML = '<p>' + lastResponse + '</p>';
}
}
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
} else if (args.command.label === 'Discard') {
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
}
};
return (
<div>
<button
id="summarizeBtn"
className="e-btn e-primary"
style=
onClick={handleSummarizeBtnClick}
>
Content Summarize
</button>
<div
id="editableText"
contentEditable="true"
style=
ref={editableRef}
>
<p>Inline AI Assist component provides intelligent text processing capabilities that enhance user productivity. It leverages advanced natural language processing to understand context and deliver precise suggestions. Users can seamlessly integrate AI-powered features into their applications.</p>
<p>With real-time response streaming and customizable prompts, developers can create interactive experiences. The component supports multiple response modes including inline editing and popup-based interactions.</p>
</div>
<InlineAIAssistComponent
id="defaultInlineAssist"
ref={assistRef}
relateTo="#summarizeBtn"
editorTemplate={footerContent}
promptRequest={handlePromptRequest}
responseSettings=
/>
</div>
);
};
ReactDOM.render(<App />, document.getElementById('container'));import { InlineAIAssistComponent, ResponseItemSelectEventArgs } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { enableRipple } from '@syncfusion/ej2-base';
enableRipple(true);
const App: React.FC = () => {
const assistRef = React.useRef<InlineAIAssistComponent>(null);
const editableRef = React.useRef<HTMLDivElement>(null);
const handleSummarizeBtnClick = (): void => {
if (assistRef.current && typeof assistRef.current.showPopup === 'function') {
assistRef.current.showPopup();
}
};
const footerContent = () => {
return (
<div className="custom-footer">
<textarea id="promptTextArea" className="e-input" rows={2} placeholder="Enter your prompt here"></textarea>
<button id="sendPrompt" className="e-btn e-primary" onClick={() => {
const textArea = document.getElementById('promptTextArea') as HTMLTextAreaElement;
if (textArea && assistRef.current) {
assistRef.current.executePrompt(textArea.value);
textArea.value = '';
}
}}>Generate</button>
</div>
);
};
const handlePromptRequest = (): void => {
setTimeout(() => {
const defaultResponse = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
if (assistRef.current && typeof assistRef.current.addResponse === 'function') {
assistRef.current.addResponse(defaultResponse);
}
}, 1000);
};
const handleResponseItemSelect = (args: ResponseItemSelectEventArgs): void => {
if (args.command.label === 'Accept') {
if (editableRef.current && assistRef.current) {
const lastResponse = assistRef.current.prompts && assistRef.current.prompts.length > 0
? assistRef.current.prompts[assistRef.current.prompts.length - 1].response
: '';
if (lastResponse) {
editableRef.current.innerHTML = '<p>' + lastResponse + '</p>';
}
}
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
} else if (args.command.label === 'Discard') {
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
}
};
return (
<div>
<button
id="summarizeBtn"
className="e-btn e-primary"
style=
onClick={handleSummarizeBtnClick}
>
Content Summarize
</button>
<div
id="editableText"
contentEditable="true"
style=
ref={editableRef}
>
<p>Inline AI Assist component provides intelligent text processing capabilities that enhance user productivity. It leverages advanced natural language processing to understand context and deliver precise suggestions. Users can seamlessly integrate AI-powered features into their applications.</p>
<p>With real-time response streaming and customizable prompts, developers can create interactive experiences. The component supports multiple response modes including inline editing and popup-based interactions.</p>
</div>
<InlineAIAssistComponent
id="defaultInlineAssist"
ref={assistRef}
relateTo="#summarizeBtn"
editorTemplate={footerContent}
promptRequest={handlePromptRequest}
responseSettings=
/>
</div>
);
};
ReactDOM.render(<App />, document.getElementById('container'));#loader {
color: #008cff;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}
#container {
margin: 20px auto;
}
#editableText {
width: 100%;
min-height: 120px;
max-height: 300px;
overflow-y: auto;
font-size: 16px;
padding: 12px;
border-radius: 4px;
border: 1px solid;
}
.custom-footer {
display: flex;
gap: 10px;
padding: 10px;
background-color: transparent;
}
#promptTextArea {
width: 100%;
padding: 12px;
min-height: 46px;
border-radius: 5px;
border: 1px solid #ccc;
margin-bottom: 0;
}
#sendPrompt {
padding: 5px 15px;
align-self: center;
}Response template
You can use the responseTemplate property to customize response items within the Inline AI Assist. The template context includes the response and toolbarItems values.
import { InlineAIAssistComponent } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { enableRipple } from '@syncfusion/ej2-base';
enableRipple(true);
function App() {
const assistRef = React.useRef(null);
const editableRef = React.useRef(null);
const promptsData = [
{
prompt: "What is AI?",
response: `<div>AI stands for Artificial Intelligence, enabling machines to mimic human intelligence for tasks such as learning, problem-solving, and decision-making.</div>`
}
];
const handleSummarizeBtnClick = () => {
if (assistRef.current && typeof assistRef.current.showPopup === 'function') {
assistRef.current.showPopup();
}
};
const responseItemContent = (ctx) => {
return <div className="responseItemContent">
<div className="response-header">
<span className="e-icons e-assistview-icon"></span>
Inline AI Assist
</div>
<div className="responseContent">{ctx.response}</div>
</div>;
};
const handlePromptRequest = (args) => {
setTimeout(() => {
let foundPrompt = promptsData.find((promptObj) => promptObj.prompt === args.prompt);
let defaultResponse = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
if (assistRef.current && typeof assistRef.current.addResponse === 'function') {
assistRef.current.addResponse((foundPrompt ? foundPrompt.response : defaultResponse));
}
}, 1000);
};
const handleResponseItemSelect = (args) => {
if (args.command.label === 'Accept') {
if (editableRef.current && assistRef.current) {
const lastResponse = assistRef.current.prompts && assistRef.current.prompts.length > 0
? assistRef.current.prompts[assistRef.current.prompts.length - 1].response
: '';
if (lastResponse) {
editableRef.current.innerHTML = '<p>' + lastResponse + '</p>';
}
}
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
} else if (args.command.label === 'Discard') {
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
}
};
return (
<div>
<button
id="summarizeBtn"
className="e-btn e-primary"
style=
onClick={handleSummarizeBtnClick}
>
Content Summarize
</button>
<div
id="editableText"
contentEditable="true"
style=
ref={editableRef}
>
<p>Inline AI Assist component provides intelligent text processing capabilities that enhance user productivity. It leverages advanced natural language processing to understand context and deliver precise suggestions. Users can seamlessly integrate AI-powered features into their applications.</p>
<p>With real-time response streaming and customizable prompts, developers can create interactive experiences. The component supports multiple response modes including inline editing and popup-based interactions.</p>
</div>
<InlineAIAssistComponent
id="defaultInlineAssist"
ref={assistRef}
prompts={promptsData}
relateTo="#summarizeBtn"
responseTemplate={responseItemContent}
promptRequest={handlePromptRequest}
responseSettings=
/>
</div>
);
};
ReactDOM.render(<App />, document.getElementById('container'));import { InlineAIAssistComponent, InlinePromptRequestEventArgs, PromptResponseModel, ResponseItemSelectEventArgs } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { enableRipple } from '@syncfusion/ej2-base';
enableRipple(true);
const App: React.FC = () => {
const assistRef = React.useRef<InlineAIAssistComponent>(null);
const editableRef = React.useRef<HTMLDivElement>(null);
const promptsData: PromptResponseModel[] = [
{
prompt: "What is AI?",
response: `<div>AI stands for Artificial Intelligence, enabling machines to mimic human intelligence for tasks such as learning, problem-solving, and decision-making.</div>`
}
];
const handleSummarizeBtnClick = (): void => {
if (assistRef.current && typeof assistRef.current.showPopup === 'function') {
assistRef.current.showPopup();
}
};
const responseItemContent = (ctx: any) => {
return <div className="responseItemContent">
<div className="response-header">
<span className="e-icons e-assistview-icon"></span>
Inline AI Assist
</div>
<div className="responseContent">{ctx.response}</div>
</div>;
};
const handlePromptRequest = (args: InlinePromptRequestEventArgs): void => {
setTimeout(() => {
let foundPrompt = promptsData.find((promptObj: PromptResponseModel) => promptObj.prompt === args.prompt);
let defaultResponse = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
if (assistRef.current && typeof assistRef.current.addResponse === 'function') {
assistRef.current.addResponse((foundPrompt ? foundPrompt.response : defaultResponse) as string);
}
}, 1000);
};
const handleResponseItemSelect = (args: ResponseItemSelectEventArgs): void => {
if (args.command.label === 'Accept') {
if (editableRef.current && assistRef.current) {
const lastResponse = assistRef.current.prompts && assistRef.current.prompts.length > 0
? assistRef.current.prompts[assistRef.current.prompts.length - 1].response
: '';
if (lastResponse) {
editableRef.current.innerHTML = '<p>' + lastResponse + '</p>';
}
}
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
} else if (args.command.label === 'Discard') {
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
}
};
return (
<div>
<button
id="summarizeBtn"
className="e-btn e-primary"
style=
onClick={handleSummarizeBtnClick}
>
Content Summarize
</button>
<div
id="editableText"
contentEditable="true"
style=
ref={editableRef}
>
<p>Inline AI Assist component provides intelligent text processing capabilities that enhance user productivity. It leverages advanced natural language processing to understand context and deliver precise suggestions. Users can seamlessly integrate AI-powered features into their applications.</p>
<p>With real-time response streaming and customizable prompts, developers can create interactive experiences. The component supports multiple response modes including inline editing and popup-based interactions.</p>
</div>
<InlineAIAssistComponent
id="defaultInlineAssist"
ref={assistRef}
prompts={promptsData}
relateTo="#summarizeBtn"
responseTemplate={responseItemContent}
promptRequest={handlePromptRequest}
responseSettings=
/>
</div>
);
};
ReactDOM.render(<App />, document.getElementById('container'));#loader {
color: #008cff;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}
#container {
margin: 20px auto;
}
#editableText {
width: 100%;
min-height: 120px;
max-height: 300px;
overflow-y: auto;
font-size: 16px;
padding: 12px;
border-radius: 4px;
border: 1px solid;
}
.responseItemContent {
display: flex;
flex-direction: column;
gap: 10px;
}
.responseItemContent .response-header {
font-size: 20px;
font-weight: bold;
display: flex;
align-items: center;
}
.responseItemContent .responseContent {
margin-left: 35px;
}
.responseItemContent .response-header .e-assistview-icon:before {
margin-right: 10px;
}
#response-item .e-response-item-template .e-toolbar-items {
margin-left: 35px;
}