Skip to content
On this page

updateTextArea

This is a helper function to mutate the value of a textarea.

Types

ts
interface SelectionRange {
    start: number;
    end: number;
}

interface UpdateTextAreaOptions = {
  /**
   * Selection range of text to be replaced.
   * Leaving this empty replaces whole text.
   */
  replace?: SelectionRange;
  /**
   * Selection/Caret to be set after updating the textarea.
   * Leaving this empty sets the caret at the end of the replaced text.
   */
  selectionAfter?: SelectionRange;
  /**
   * Optionally provide logic for updating the textarea value.
   * Leaving this empty wil use `element.value = value` to update the textarea.
   */
  setValue?(text: string): void;
}

type UpdateTextArea = (
    /**
     * The agent input field element.
     */
    element: HTMLTextAreaElement,
    /**
     * The text being inserted into the agent input field.
     */
    insertText: string,
    /**
     * Options to control the insert behaviour.
     */
    options?: UpdateTextAreaOptions
) => void;

Usage example

Example textarea with text:

html
<textarea id="input">Hello Person, how do you do?</textarea>

Replace 'Person' with 'John':

ts
import { updateTextArea } from '@deepdesk/deepdesk-sdk';

const element = document.getElementById("input");

updateTextArea(element, "John", {
    replace: { start: 6, end: 12 },
});

Textarea state after:

html
<textarea id="input">Hello John, how do you do?</textarea>

Note that the caret will be placed right after 'John'.