Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ghost opened this issue Sep 7, 2017 · 11 comments
Closed

Comments

@ghost
Copy link

ghost commented Sep 7, 2017

$(".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">

@billba
Copy link
Member

billba commented Sep 8, 2017

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 billba closed this as completed Sep 8, 2017
@danmarshall
Copy link
Contributor

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?

@ghost
Copy link
Author

ghost commented Sep 8, 2017 via email

@danmarshall
Copy link
Contributor

Related to #476

@danmarshall
Copy link
Contributor

We can continue this conversation in #476

@mrPrateek95
Copy link

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: facebook/react#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();

@Snekha-Shivakumar
Copy link

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

@mrPrateek95
Copy link

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

@Snekha-Shivakumar
Copy link

@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.

@mrPrateek95
Copy link

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

@gideon-harpanahalli
Copy link

gideon-harpanahalli commented Oct 10, 2023

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();
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants