JavaScript/jQuery Chat - Configure Suggestions
DevExtreme Chat can display suggestion buttons (a DevExtreme ButtonGroup component) above the message input field. To enable this feature, use the suggestions object.
To display suggestion buttons, specify the suggestions.items[] array. Use ButtonGroup item fields to configure the appearance and visual elements of individual buttons. You can also specify custom fields to implement additional functionality. For instance, you can store predefined messages in a custom field and use short labels (items[].text values).
Fill the Chat Input Field with Predefined Messages
To configure suggestion items for the Chat's input field, update the inputFieldText property in the suggestions.onItemClick handler:
- $('#chat').dxChat({
- suggestions: {
- items: [
- { text: 'Summarize text' },
- { text: 'Write an email' },
- { text: 'Fix my writing' },
- ],
- onItemClick(e) {
- $('#chat').dxChat('instance').option('inputFieldText', e.itemData.text);
- },
- },
- });
Send Predefined Messages on Suggestion Click
To send predefined messages when a suggestions button is clicked, call renderMessage() in suggestions.onItemClick:
- $('#chat').dxChat({
- suggestions: {
- items: [
- { text: 'Summarize text' },
- { text: 'Write an email' },
- { text: 'Fix my writing' },
- ],
- onItemClick(e) {
- const newMessage = {
- id: Date.now(),
- timestamp: new Date(),
- text: e.itemData.text,
- };
- $('#chat').dxChat('instance').renderMessage(newMessage);
- }
- }
- });
Configure Disappearing Suggestions
To hide a suggestion item after a user clicks the button, update the suggestions.items[] array:
- $('#chat').dxChat({
- suggestions: {
- items: [
- { text: 'Summarize text' },
- { text: 'Write an email' },
- { text: 'Fix my writing' },
- ],
- onItemClick(e) {
- const chatInstance = $('#chat').dxChat('instance');
- const currentSuggestions = chatInstance.option('suggestions');
- chatInstance.option('suggestions', {
- items: currentSuggestions.items.filter((item) => item.text !== e.itemData.text),
- });
- // ...
- chatInstance.renderMessage(newMessage);
- },
- },
- });
Alternatively, you can disable clicked items:
- $('#chat').dxChat({
- suggestions: {
- // ...
- onItemClick(e) {
- const chatInstance = $('#chat').dxChat('instance');
- const currentSuggestions = chatInstance.option('suggestions');
- currentSuggestions.find((item) => item.text === e.itemData.text).disabled = true;
- chatInstance.option('suggestions', { items: currentSuggestions });
- // ...
- chatInstance.renderMessage(newMessage);
- },
- },
- });