I am working on a requirement where I have to implement auto complete functionality in React Web Chat. I have created and deployed an Azure Echo Bot and used React Web Chat and directline for the UI. For adding Auto complete, I have created a list of sample utterances which should be rendered when user types something. Below is my App.js code where I have redux store concept from Bot framework webchat.
Here, when I run the code, suggestions list is being rendered in my UI, but the on-click event for selecting a suggestion is not working and also the user's input is not appearing in the chat window.
Can anyone help me with the solution below.
import React, { useMemo,useEffect } from 'react';
import ReactWebChat, { createDirectLine, createStore } from 'botframework-webchat';
const items = [
"What is your name?",
"What is your job?",
"Who are you?"];
function App() {
const [listItems, setlistItems] = React.useState([]); //for creating list of suggested values
const [textValue, setTextValue] = React.useState(''); // for saving the suggested value when user clicks
const store = createStore({},
store => next => action => {
if (action.type === 'WEB_CHAT/SET_SEND_BOX') {
let value = action.payload.text;
const regex = new RegExp(`^${value}`, 'i');
let suggestions = items.sort().filter(v => regex.test(v));
setlistItems(suggestions);
setTextValue(textValue);
}
return next(action);
}
);
function suggestionSelected(value) {
setlistItems([]);
setTextValue(value);
}
const renderSuggestions = () => {
if (listItems.length === 0) {
return null;
}
return (
<div className="srchList">
<ul>
{listItems.map((item, idx) => <li key={idx} onClick={() => suggestionSelected(item)}>{item}</li>)}
</ul>
</div>
);
}
const directLine = useMemo(() => createDirectLine({ secret: '**************' }), []);
return (
<div>
<h1> Hello</h1>
<ReactWebChat
directLine={directLine}
store={store}
/>
<div>{renderSuggestions()}</div>
</div>
)
}
export default App;
store.dispatch({ type: 'WEB_CHAT/SET_SEND_BOX', payload: { text: value, } });
– Sharvani Jun 11 '21 at 11:48