Custom views in React AI AssistView component

14 Dec 202417 minutes to read

Adding custom views

By using the ViewsDirective tag you can define the collection of different assist view models in the AI AssistView. You can customize the default and the custom views added.

Setting view type

You can set the type of view by using the type property within the ViewDirective tag. It accepts two values such as Assist and Custom.

import { AIAssistViewComponent, ViewsDirective, ViewDirective } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {

    return (
        // specifies the tag for render the AI AssistView component
        <AIAssistViewComponent id="aiAssistView">
            <ViewsDirective>
                <ViewDirective type='Assist'></ViewDirective>
                <ViewDirective type='Custom'></ViewDirective>
            </ViewsDirective>
        </AIAssistViewComponent>
    );
}

ReactDOM.render(<App />, document.getElementById('container'));

Setting name

You can use the name property to specifies the header name of the Assist or Custom views in the AI AssistView.

import { AIAssistViewComponent, ViewsDirective, ViewDirective } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {
  
    return (
        // specifies the tag for render the AI AssistView component
        <AIAssistViewComponent id="aiAssistView">
            <ViewsDirective>
                <ViewDirective type='Assist' name='Prompt'></ViewDirective>
                <ViewDirective type='Custom' name='Response'></ViewDirective>
            </ViewsDirective>
        </AIAssistViewComponent>
    );
}

ReactDOM.render(<App />, document.getElementById('container'));

Setting iconCss

You can customize the view icons by using the iconCss property. By default the e-assistview-icon class is added as built-in header icon for the AI AssistView.

import { AIAssistViewComponent, ViewsDirective, ViewDirective } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {
  
    return (
        // specifies the tag for render the AI AssistView component
        <AIAssistViewComponent id="aiAssistView">
            <ViewsDirective>
                <ViewDirective type='Assist' name='Prompt' iconCss='e-assistview-icon'></ViewDirective>
                <ViewDirective type='Custom' name='Response' iconCss='e-comment-show'></ViewDirective>
            </ViewsDirective>
        </AIAssistViewComponent>
    );
}

ReactDOM.render(<App />, document.getElementById('container'));

The following example illustrates how types, name, and iconCss are used in a AI AssistView component.

import { AIAssistViewComponent, ViewsDirective, ViewDirective } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {
    const assistInstance = React.useRef(null);
    const responseViewTemplate = '<div class="view-container"><h5>Response view content</h5></div>';

    const onPromptRequest = (args) => {
        setTimeout(() => {
            let defaultResponse = 'For real-time prompt processing, connect the AIAssistView 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.';
            assistInstance.current.addPromptResponse(defaultResponse);
          }, 1000);
    };
  
    return (
        // specifies the tag for render the AI AssistView component
        <AIAssistViewComponent id="aiAssistView" ref={assistInstance} promptRequest={onPromptRequest}>
            <ViewsDirective>
                <ViewDirective type='Assist' name='Prompt' iconCss='e-assistview-icon'></ViewDirective>
                <ViewDirective type='Custom' name='Response' iconCss='e-comment-show' viewTemplate={responseViewTemplate}></ViewDirective>
            </ViewsDirective>
        </AIAssistViewComponent>
    );
}

ReactDOM.render(<App />, document.getElementById('container'));
import { AIAssistViewComponent, PromptRequestEventArgs, ViewsDirective, ViewDirective } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {

    const assistInstance = React.useRef<AIAssistViewComponent>(null);
    const responseViewTemplate: string = '<div class="view-container"><h5>Response view content</h5></div>';

    const onPromptRequest = (args: PromptRequestEventArgs) => {
        setTimeout(() => {
            let defaultResponse = 'For real-time prompt processing, connect the AIAssistView 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.';
            assistInstance.current.addPromptResponse(defaultResponse);
          }, 1000);
    };
  
    return (
        // specifies the tag for render the AI AssistView component
        <AIAssistViewComponent id="aiAssistView" ref={assistInstance} promptRequest={onPromptRequest}>
            <ViewsDirective>
                <ViewDirective type='Assist' name='Prompt' iconCss='e-assistview-icon'></ViewDirective>
                <ViewDirective type='Custom' name='Response' iconCss='e-comment-show' viewTemplate={responseViewTemplate}></ViewDirective>
            </ViewsDirective>
        </AIAssistViewComponent>
    );
}

ReactDOM.render(<App />, document.getElementById('container'));
/* Represents the styles for loader */
#loader {
    color: #008cff;
    height: 40px;
    left: 45%;
    position: absolute;
    top: 45%;
    width: 30%;
}

#container {
    margin: 20px auto;
}

#aiAssistView .view-container {
    height: inherit;
    display: flex;
    align-items: center;
    justify-content: center;
}

Setting view template

You can use the viewTemplate property to add the view content of the multiple views added in the AI AssistView.

import { AIAssistViewComponent, ViewsDirective, ViewDirective } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {
    const assistInstance = React.useRef(null);

    const promptViewTemplate = '<div class="view-container"><h5>Prompt view content</h5></div>';
    const responseViewTemplate = '<div class="view-container"><h5>Response view content</h5></div>';
  
    return (
        // specifies the tag for render the AI AssistView component
        <AIAssistViewComponent id="aiAssistView" ref={assistInstance}>
            <ViewsDirective>
                <ViewDirective type='Assist' name='Prompt' viewTemplate={promptViewTemplate}></ViewDirective>
                <ViewDirective type='Custom' name='Response' iconCss='e-comment-show' viewTemplate={responseViewTemplate}></ViewDirective>
            </ViewsDirective>
        </AIAssistViewComponent>
    );
}

ReactDOM.render(<App />, document.getElementById('container'));
import { AIAssistViewComponent, PromptRequestEventArgs, ViewsDirective, ViewDirective } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {

    const assistInstance = React.useRef<AIAssistViewComponent>(null);

    const promptViewTemplate: string = '<div class="view-container"><h5>Prompt view content</h5></div>';
    const responseViewTemplate: string = '<div class="view-container"><h5>Response view content</h5></div>';
  
    return (
        // specifies the tag for render the AI AssistView component
        <AIAssistViewComponent id="aiAssistView" ref={assistInstance}>
            <ViewsDirective>
                <ViewDirective type='Assist' name='Prompt' viewTemplate={promptViewTemplate}></ViewDirective>
                <ViewDirective type='Custom' name='Response' iconCss='e-comment-show' viewTemplate={responseViewTemplate}></ViewDirective>
            </ViewsDirective>
        </AIAssistViewComponent>
    );
}

ReactDOM.render(<App />, document.getElementById('container'));
/* Represents the styles for loader */
#loader {
    color: #008cff;
    height: 40px;
    left: 45%;
    position: absolute;
    top: 45%;
    width: 30%;
}

#container {
    margin: 20px auto;
}

.view-container {
    margin: 20px auto;
    width: 80%;
  }

Setting active view

You can use the activeView property to set the active view in the AI AssistView. By default, the value is 0.

import { AIAssistViewComponent, ViewsDirective, ViewDirective } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {
    const assistInstance = React.useRef(null);
    const responseViewTemplate = '<div class="view-container"><h5>Response view content</h5></div>';

    const onPromptRequest = (args) => {
        setTimeout(() => {
            let defaultResponse = 'For real-time prompt processing, connect the AIAssistView 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.';
            assistInstance.current.addPromptResponse(defaultResponse);
          }, 1000);
    };
  
    return (
        // specifies the tag for render the AI AssistView component
        <AIAssistViewComponent id="aiAssistView" ref={assistInstance} promptRequest={onPromptRequest} activeView={1}>
            <ViewsDirective>
                <ViewDirective type='Assist' name='Prompt'></ViewDirective>
                <ViewDirective type='Custom' name='Response' iconCss='e-comment-show' viewTemplate={responseViewTemplate}></ViewDirective>
            </ViewsDirective>
        </AIAssistViewComponent>
    );
}

ReactDOM.render(<App />, document.getElementById('container'));
import { AIAssistViewComponent, PromptRequestEventArgs, ViewsDirective, ViewDirective } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {

    const assistInstance = React.useRef<AIAssistViewComponent>(null);
    const responseViewTemplate: string = '<div class="view-container"><h5>Response view content</h5></div>';

    const onPromptRequest = (args: PromptRequestEventArgs) => {
        setTimeout(() => {
            let defaultResponse = 'For real-time prompt processing, connect the AIAssistView 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.';
            assistInstance.current.addPromptResponse(defaultResponse);
          }, 1000);
    };
  
    return (
        // specifies the tag for render the AI AssistView component
        <AIAssistViewComponent id="aiAssistView" ref={assistInstance} promptRequest={onPromptRequest} activeView={1}>
            <ViewsDirective>
                <ViewDirective type='Assist' name='Prompt'></ViewDirective>
                <ViewDirective type='Custom' name='Response' iconCss='e-comment-show' viewTemplate={responseViewTemplate}></ViewDirective>
            </ViewsDirective>
        </AIAssistViewComponent>
    );
}

ReactDOM.render(<App />, document.getElementById('container'));
/* Represents the styles for loader */
#loader {
    color: #008cff;
    height: 40px;
    left: 45%;
    position: absolute;
    top: 45%;
    width: 30%;
}

#container {
    margin: 20px auto;
}

#aiAssistView .view-container {
    height: inherit;
    display: flex;
    align-items: center;
    justify-content: center;
}