BotFramework-WebChat icon indicating copy to clipboard operation
BotFramework-WebChat copied to clipboard

wc-shellinput value does not change after programatically injecting new value

Open ghost opened this issue 9 years ago • 11 comments

$(".wc-shellinput").prop({value: "Welcome"}) changes the display value of the text box but does not make changes in value of input class...

<input class="wc-shellinput" type="text" placeholder="Type your message..." value="" autocomplete="off">

ghost avatar Sep 07 '17 23:09 ghost

This is a React app, so it maintains DOM state internally and updates from there. Manipulating the DOM directly will have unexpected results. Don't do that.

billba avatar Sep 08 '17 12:09 billba

Agree this is not a bug. But, it could be a feature request. @ramgesg can you provide a scenario where you would inject text on the user's behalf?

danmarshall avatar Sep 08 '17 17:09 danmarshall

@dan, this is for injecting onselect value from autosuggest.

Sent from Mailhttps://go.microsoft.com/fwlink/?LinkId=550986 for Windows 10

From: Dan Marshallmailto:[email protected] Sent: Friday, September 8, 2017 11:23 PM To: Microsoft/BotFramework-WebChatmailto:[email protected] Cc: Ramakrishnan Smailto:[email protected]; Mentionmailto:[email protected] Subject: Re: [Microsoft/BotFramework-WebChat] wc-shellinput value does not change after programatically injecting new value (#680)

Agree this is not a bug. But, it could be a feature request. @ramgesghttps://github.com/ramgesg can you provide a scenario where you would inject text on the user's behalf?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/Microsoft/BotFramework-WebChat/issues/680#issuecomment-328171644, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AZKM-4CKk51hW7WvGp0u4ogJRg_0wbSaks5sgX8WgaJpZM4PQhNT.

ghost avatar Sep 08 '17 18:09 ghost

Related to #476

danmarshall avatar Sep 08 '17 18:09 danmarshall

We can continue this conversation in #476

danmarshall avatar Sep 08 '17 18:09 danmarshall

Not a clean method but you can use this.

//Fill the input field : Using a Hack. This might break after the next React update //Source: https://github.com/facebook/react/issues/11488 var input = document.getElementsByClassName("wc-shellinput")[0]; var lastValue = input.value; input.value = YOUR TEXT; var event = new CustomEvent('input', { bubbles: true }); // hack React15 event.simulated = true; // hack React16 var tracker = input._valueTracker; if (tracker) { tracker.setValue(lastValue); } input.dispatchEvent(event);

$(".wc-send:first").click();

mrPrateek95 avatar Apr 18 '18 06:04 mrPrateek95

@mrPrateek95 Thank you for this. But, it works in chrome and firefox and not in IE. Any idea on this?

Snekha-Shivakumar avatar Oct 11 '18 14:10 Snekha-Shivakumar

@Snekha-Shivakumar What version of IE are you using? I have tested it on IE11. Seems to be working there.

mrPrateek95 avatar Oct 11 '18 15:10 mrPrateek95

@mrPrateek95 Yeah I found the issue. new CustomEvent() is not working in IE. Just added a polyfill before the custom event and it works fine now. Here is the polyfill:

    (function () {
        function CustomEvent ( event, params ) {
        params = params || { bubbles: false, cancelable: false, detail: undefined };
        var evt = document.createEvent( 'CustomEvent' );
        evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
        return evt;
        };
        CustomEvent.prototype = window.Event.prototype;
        window.CustomEvent = CustomEvent;
    })();

Thank you.

Snekha-Shivakumar avatar Oct 12 '18 10:10 Snekha-Shivakumar

@Snekha-Shivakumar Checked on my client. I have also added this polyfill. Slipped my mind earlier.

mrPrateek95 avatar Oct 12 '18 12:10 mrPrateek95

Hey @mrPrateek95, I am trying to autofill input field when user types a question partially clicks on a button to autocomplete the question. This solution seems to not work in this case. The text previously entered text is sent rather than the text which is added programatically. Any suggestions to resolve this?

    // Event listeners
    $(document).on("click focus", ".suggestion-box", function () {
        const selectedQuestion = $(this).text();
        const inputElement = $(".wc-shellinput")[0]; // Get the DOM element from the jQuery object
        inputElement.value = selectedQuestion; // Set the value directly to the DOM element

        // Create a new event 
        var event = new CustomEvent('input', { bubbles: true });
        event.simulated = true;
        var tracker = input._valueTracker;
        if (tracker) {
            tracker.setValue(selectedQuestion);
        }
        // Dispatch the event on the input element
        inputElement.dispatchEvent(inputEvent);
        $(".wc-send:first").click();
    });

gideon-harpanahalli avatar Oct 10 '23 17:10 gideon-harpanahalli