cancel
Showing results for 
Search instead for 
Did you mean: 

How to execute/fire onPost when user press enter on FeedInput element of SAP UI5?

mrj_marcelo
Participant
0 Kudos

Hi.

How to execute fire onPost when user press enter on FeedInput element of SAP UI5? I need that when the user press enter in FeedInput element in SAP fiori the system behaves as when the user click on post button.

Thanks and best regards.

Accepted Solutions (0)

Answers (1)

Answers (1)

sajid-khan
Explorer
0 Kudos

The FeedInput component naturally inserts a new line when you press Enter. I wouldn't suggest changing this default behavior, however if your application still requires onFocus to be fired when enter is pressed on input area, here's how it can be achived:

Add this onAfterRendering method to your View Controller:

 

onAfterRendering : function () {
    //note: replace 'myFeedInputID' with ID of FeedInput component
    let myFeedInput = this.byId('myFeedInputID');
    let dom = myFeedInput.getFocusDomRef();
    if (dom) {
        dom.addEventListener('keypress', (evt) => {
            if ((evt.keyCode || evt.which) == 13) {
                if (evt.preventDefault)
                    evt.preventDefault();
                myFeedInput.firePost();
                // you can also call your custom code directly here (instead of firing post event)
            }
        });
    }
},

 

Note that the provided code should be executed only after the FeedInput component has been rendered. To ensure this, I've wrapped the code inside the onAfterRendering method of the view controller.

 

Please upvote if this helps. Thanks!

- Sajid Khan