docs

Implementing Focus Handling

OpenUI5 provides mechanisms for observing the moving focus in an application page for controls. This information is then preserved for refocusing elements after rerendering. The focus triggers event firing. However, due to the high degree of flexibility in control rendering, a functionality tailored to the respective controls is required. For this, the framework provides helper functions for the implementation of focus handling.

Each control provided by the OpenUI5 framework has its own behavior for focus handling, depending on the functionality that is provided by the control. Complex controls and their embedded content constitute the highest level of complexity.

The base class for elements (Element.js) provides the following five methods to support the implementation of focus handling:


Example

In the following example, the control would usually set the focus on the second child node of its root node. In this case, simply override the getFocusDomRef method:

sap.ui.define([
    "sap/ui/core/Control"
], (Control) => {
    "use strict";

    return Control.extend("my.custom.SampleControl1", {
        // ...

        getFocusDomRef() {
            return this.getDomRef()?.firstChild?.nextSibling;
        }
    });
});

Another control generally sets the focus back to the element that previously had the focus. Therefore, it overrides the methods getFocusInfo and applyFocusInfo.

sap.ui.define([
    "sap/ui/core/Control"
], (Control) => {
    "use strict";

    return Control.extend("my.custom.SampleControl2", {
        // ...

        getFocusInfo() {
            return {
                id: this.getId(),
                idx: this.myFocusElementIndex
            };
        },

        applyFocusInfo(oFocusInfo) {
            const oDomRef = this.getDomRef();
            if (oDomRef) {
                this.myFocusElementIndex = oFocusInfo.idx;
                this.focus();
            }
        }
    });
});

Below is a sample implementation of a custom onfocusfail handler that redirects the focus to a specific element in the event of focus loss. In this example, if a Button element loses focus (due to it being disabled, hidden, or destroyed), the focus is reset to an alternative element like a TextArea within the same container.

sap.ui.define([
    "sap/ui/core/Control",
    "sap/m/Button",
    "sap/m/TextArea"
], (Control, Button, TextArea) => {
    "use strict";

    return Control.extend("my.custom.SampleControl3", {
        metadata: {
            aggregations: {
                "_myButton": {
                    multiple: false,
                    visibility: "hidden",
                    type: "sap.m.Button"
                },
                "_myTextArea": {
                    multiple: false,
                    visibility: "hidden",
                    type: "sap.m.TextArea"
                }
            }
        },

        init() {
            Control.prototype.init.apply(this, arguments);
            this.setAggregation("_myButton", new Button({
                text: "Action Button",
                press: [this.onButtonPress, this]
            }));
            this.setAggregation("_myTextArea", new TextArea({
                value: "Default Fallback Focus"
            }));
        },

        onButtonPress() {
            this.getAggregation("_myButton").setEnabled(false); // making it no longer focusable
        },

        // Custom onfocusfail handler to redirect focus
        onfocusfail(oEvent) {
            const oControlLosingFocus = oEvent.srcControl;
            if (oControlLosingFocus === this.getAggregation("_myButton")) {
                // Shift focus to the specified alternative element
                this.getAggregation("_myTextArea").focus();
            } else {
                // Optional: fallback to the default onfocusfail behavior
                Control.prototype.onfocusfail.apply(this, arguments);
            }
        },

        // ...
    });
});

API Reference

sap.ui.core.Element

getCurrentFocusedControlId