0.1.89 • Published 1 month ago

super-great-testing-chat-temp v0.1.89

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
1 month ago

Carbon for watsonx web chat with React

Be sure to review the chat documentation.

Installation

To install using npm:

npm install super-great-testing-chat-temp

Or using yarn:

yarn add super-great-testing-chat-temp

Using ChatContainer

Basic example

The WebChatContainer function component is intended to make it as easy as possible to include web chat in your React application. To use, you simply need to render this component anywhere in your application and provide the web chat configuration options object as a prop.

import React from 'react';
import { ChatContainer } from 'super-great-testing-chat-temp';

const webChatOptions = {
  // Your configuration object.
};

function App() {
  return <ChatContainer config={webChatOptions} />;
}

Props

ChatContainer has the following props.

AttributeRequiredTypeDescription
configYesobjectThe chat configuration options object. Note that any onLoad property will be ignored. If this prop is changed and a new object provided, then the current web chat will be destroyed and a new one created with the new object.
onBeforeRenderNofunctionThis is a callback function that is called after web chat has been loaded and before the render function is called. This function is passed a single argument which is the instance of web chat that was loaded. This function can be used to obtain a reference to the web chat instance if you want to make use of the instance methods that are available.
onAfterRenderNofunctionThis is a callback function that is called after web chat has been loaded and after the render function is called. This function is passed a single argument which is the instance of web chat that was loaded. This function can be used to obtain a reference to the web chat instance if you want to make use of the instance methods that are available.
renderUserDefinedResponseNofunctionThis function is a callback function that will be called by this container to render user defined responses. If this prop is provided, then the container will listen for user defined response events from web chat and will generate a React portal for each event. This function will be called once during component render for each user defined response event. This function takes two arguments. The first is the user defined response event that triggered the user defined response. The second is a convenience argument that is the instance of web chat. The function should return a ReactNode that renders the user defined content for the response.

Using ChatCustomElement

This library provides the component ChatCustomElement which can be used to aid in rendering web chat inside a custom element. This is needed if you want to be able to change the location where web chat is rendered. This component will render an element in your React app and use that element as the custom element for rendering web chat.

The default behavior of this component will add and remove a classname from the web chat main window as well as your custom element to control the visibility of web chat when it is opened or closed. It will also inject a style tag into your application to define the rules for these classnames. When web chat is closed, a classname will be added to the web chat main window to hide the element and a classname will be added to your custom element to set its width and height to 0 so it doesn't take up space. Note that the custom element should remain visible if you want to use the built-in web chat launcher which is also contained in your custom element.

If you don't want these behaviors, then provide your own onViewChange prop to ChatCustomElement and provide your own logic for controlling the visibility of web chat. If you want custom animations when web chat is opened and closed, this would be the mechanism to do that.

The simplest example is this:

import React from 'react';
import { ChatCustomElement } from 'super-great-testing-chat-temp';

import './App.css';

const webChatOptions = { /* Web chat options */ };

function App() {
  return <ChatCustomElement className="MyCustomElement" config={webChatOptions} />;
}
.MyCustomElement {
  position: absolute;
  left: 100px;
  top: 100px;
  width: 500px;
  height: 500px;
}

Props

ChatCustomElement inherits all of the props from ChatContainer. It also has the following additional optional props.

AttributeTypeDescription
classNamestringAn optional classname that will be added to the custom element.
idstringAn optional id that will be added to the custom element.
onViewChangefunctionAn optional listener for "view:change" events. Such a listener is required when using a custom element in order to control the visibility of the web chat main window. If no callback is provided here, a default one will be used that uses some classnames to control web chat and your custom element. You can provide a different callback here if you want custom behavior such as an animation when the main window is opened or closed. Note that this function can only be provided before web chat is loaded. After web chat is loaded, the event handler will not be updated.

Accessing instance methods

You can use the onBeforeRender or onAfterRender props to get access to the instance of web chat if you need call instance methods later. This example renders a button that toggles web chat open and is only rendered after the instance has become available.

import React, { useCallback, useState } from 'react';
import { ChatContainer } from 'super-great-testing-chat-temp';

const webChatOptions = {
  // Your configuration object.
};

function App() {
  const [instance, setInstance] = useState(null);

  const toggleWebChat = useCallback(() => {
    instance.toggleOpen();
  }, [instance]);

  return (
    <>
      {instance && (
        <button type="button" onClick={toggleWebChat}>
          Toggle web chat
        </button>
      )}
      <ChatContainer config={webChatOptions} onBeforeRender={(instance) => onBeforeRender(instance, setInstance)} />
    </>
  );
}

function onBeforeRender(instance, setInstance) {
  // Make the instance available to the React components.
  setInstance(instance);

  // Do any other work you might want to do before rendering. If you don't need any other work here, you can just use
  // onBeforeRender={setInstance} in the component above.
}

User defined responses

This component is also capable of managing user defined responses. To do so, you need to pass a renderUserDefinedResponse function as a render prop. This function should return a React component that will render the content for the specific message for that response. You should make sure that the ChatContainer component does not get unmounted in the middle of the life of your application because it will lose all user defined responses that were previously received by web chat.

You should treat the renderUserDefinedResponse prop like any typical React render prop; it is different from the userDefinedResponse event or a typical event handler. The event is fired only once when web chat initially receives the response from the server. The renderUserDefinedResponse prop however is called every time the App re-renders and it should return an up-to-date React component for the provided message item just like the render function would for a typical React component.

import React from 'react';
import { ChatContainer } from 'super-great-testing-chat-temp';

const webChatOptions = {
  // Your configuration object.
};

function App() {
  return <ChatContainer renderUserDefinedResponse={renderUserDefinedResponse} config={webChatOptions} />;
}

function renderUserDefinedResponse(event) {
  // The event here will contain details for each user defined response that needs to be rendered.
  // The "user_defined_type" property is just an example; it is not required. You can use any other property or
  // condition you want here. This makes it easier to handle different response types if you have more than
  // one user defined response type.
  if (event.data.message.user_defined && event.data.message.user_defined.user_defined_type === 'my-custom-type') {
    return <div>My custom content</div> // This can be any full React component.
  }
}
0.1.89

1 month ago

0.1.88

1 month ago

0.1.87

1 month ago

0.1.86

1 month ago

0.1.85

1 month ago

0.1.84

1 month ago

0.1.81

1 month ago

0.1.79

2 months ago

0.1.78

2 months ago

0.1.76

2 months ago

0.1.75

2 months ago

0.1.73

2 months ago

0.1.72

2 months ago

0.1.71

2 months ago

0.1.70

2 months ago

0.1.69

2 months ago

0.1.68

2 months ago

0.1.67

2 months ago

0.1.66

2 months ago

0.1.65

2 months ago

0.1.64

2 months ago

0.1.63

2 months ago

0.1.62

2 months ago

0.1.61

2 months ago

0.1.60

2 months ago

0.1.59

2 months ago

0.1.58

2 months ago

0.1.57

2 months ago

0.1.56

2 months ago

0.1.55

2 months ago

0.1.54

2 months ago

0.1.53

2 months ago

0.1.52

2 months ago

0.1.50

2 months ago

0.1.49

2 months ago

0.1.48

2 months ago

0.1.47

2 months ago

0.1.46

2 months ago

0.1.45

2 months ago

0.1.44

2 months ago

0.1.43

2 months ago

0.1.42

2 months ago

0.1.41

2 months ago

0.1.39

2 months ago

0.1.37

2 months ago

0.1.36

2 months ago

0.1.35

2 months ago

0.1.34

2 months ago

0.1.33

2 months ago

0.1.32

2 months ago

0.1.31

2 months ago

0.1.30

2 months ago

0.1.28

2 months ago

0.1.27

2 months ago

0.1.26

2 months ago

0.1.25

2 months ago

0.1.23

2 months ago

0.1.22

2 months ago

0.1.21

2 months ago

0.1.20

2 months ago

0.1.19

2 months ago

0.1.18

2 months ago

0.1.17

2 months ago

0.1.16

2 months ago

0.1.14

2 months ago

0.1.12

2 months ago

0.1.11

2 months ago

0.1.10

2 months ago

0.1.9

2 months ago

0.1.8

2 months ago

0.1.7

2 months ago

0.1.6

2 months ago

0.1.5

2 months ago

0.1.4

2 months ago

0.1.3

2 months ago

0.1.2

2 months ago

0.1.1

2 months ago

0.1.0

2 months ago