A customer recently asked me if I could create a "quick entry" form that excluded certain fields from the regular tab order. Oracle JET input components have a tab index for a reason. They are designed for accessibility which means they work with accessible input devices and don't require a mouse. Removing data entry fields from the tab order means I have to use a pointing device, such as a mouse or tap, to select a skipped field. Although this goes against my better judgement, I understand the use case. If you are entering information in rapid, data entry mode, you don't want to tab over fields you rarely use. The issue I ran into is this:
How do you remove the tab index from certain Oracle JET components?
Input elements such as ojInputDate display a native HTML element and will automatically be part of the tab order. We can easily remove these elements from the tab order by setting the tabIndex
to -1 like this:
<input tabindex="-1" data-bind="ojComponent: { component: 'ojInputDate', value: selectedDate}" />
Other elements, such as ojSelect, use a clever collection of non-input HTML elements to capture input. For accessibility reasons, Oracle JET adds tabindex="0"
to these input elements to ensure accessible devices can enter data. Since many of these Oracle JET elements begin life as a real HTML input element (input, select, etc), one might think that changing the tab index would be as easy as setting the tab index on the original input element. Unfortunately, it is not that easy. Another idea is to include tabIndex
in the rootAttributes
collection of an OJ component. Unfortunately, that doesn't work either. To remove these elements from the tab order, we must first identify the element with a tab index, and then change the value of the tab index. To do this effectively, we have to answer two questions:
- How do I identify the element with a
tabIndex
attribute? - Timing wise, when will the DOM be ready for me to change the
tabIndex
?
Let's start with question #1. Study the following structure screenshot for a moment.
In the above screenshot, the JET generated oj-select
is highlighted at the top. The very next element, the oj-select-choice
element is the element with the tabIndex
and is a child element of the oj-select
. This is the element we want to modify and we need to find a way to programatically identify this element. Notice that the original select
element is a sibling of the generated oj-select
element. We could use a sibling selector. Sibling selectors are great for collections, where you want to style elements differently based on their position in a collection, but a sibling selector makes me a little nervous in this instance. I'm not sure we can depend on the sibling relationship identified in this screenshot. Rather than use a sibling selector, we can use Oracle JET's getNodeBySubId method. For Oracle JET components that are composed of several elements, we can identify individual pieces of the component by Sub ID. You can find the list of valid ojSelect
Sub IDs here. We specifically want to select the oj-select-choice
node. Unfortunately, oj-select-choice
is not a node with a known Sub ID. If I expanded the oj-select-choice
node in the structure screenshot, you would see that oj-select-chosen
is a direct descendant of oj-select-choice
. The oj-select-chosen
node is selectable by Sub ID. With a little jQuery, we can easily traverse from oj-select-chosen
up to oj-select-choice
. We can test this by selecting our select
element in the structure browser and then entering the following into the console:
$($0) .ojSelect( "getNodeBySubId", {'subId': 'oj-select-chosen'} ) .closest( ".oj-select-choice" )
The following screenshot shows the results of that command (notice I also expanded the oj-select-choice
to reveal the child oj-select-chosen
).
A quick word about jQuery traversal methods... Using jQuery, there are often multiple ways to solve the same problem. In this situation we could either use the parents() or closest() methods to work our way up the hierarchy. Parents and Closest are similar, but, as the docs say, "The differences between the two, though subtle, are significant." We want to find the most immediate ancestor matching a selector. Closest accomplishes this. It identifies the match and then stops. The parents
method, on the other hand, finds all matches and returns them as a collection. We have no need to walk the entire document hierarchy, so closest
is the appropriate choice for this scenario. We have now answered question #1: How do I identify the element with a tabIndex attribute?
Next we need to handle the Life Cycle Management issue: How do we know when the ojSelect
is available in the DOM (question #2)? Even though ojModule has Life Cycle Management events that tell us when the DOM is available, I don't suggest using them for this specific scenario. ojModule
LCM events will work just fine with DOM elements that are hard coded into the view, but I'm not sure we should count on them for dynamically generated elements, such as those bound to an ko.observableArray. A better approach is to use a component-specific life cycle management approach such as custom bindings (ko.bindingHandlers). This allows us to manipulate an element as soon as it appears in the DOM. Here is an example ko.bindingHandler:
ko.bindingHandlers.inaccessibleOjSelect = { init: function(element, valueAccessor, allBindingsAccessor, ctx) { var options = allBindingsAccessor().ojSelectOptions || {}; var multiple = !!options.multiple; var tabEl = $(element) // initialize ojSelect .ojSelect(options) // bind value change handler .on({ 'ojoptionchange': function (event, data) { if (data.option === "value") { var observable = valueAccessor(); if (ko.isObservable(observable)) { if (multiple) { observable(data.value); } else { // unwrap from array if single select observable(data.value[0]); } } // if not observable, just throw away the value } } }) // get reference to item with tabIndex .ojSelect( "getNodeBySubId", {'subId': 'oj-select-chosen'}) .closest(".oj-select-choice"); $(tabEl).attr("tabIndex", -1); }, update: function(element, valueAccessor) { var value = ko.utils.unwrapObservable(valueAccessor()); $(element).ojSelect("option", "value", [value]); } };
Note: The ojSelect value expects an array. In this JavaScript, notice that I distinguish between multi and single selection modes. For single-select ojSelect elements, I am unwrapping the value array and just returning the single value. This is for convenience and is in lieu of my other method for unwrapping ojSelect values.
You would use this with HTML similar to the following:
<select id="basicSelect" data-bind="inaccessibleOjSelect: browser, ojSelectOptions: {optionChange: browserChangedHandler, rootAttributes: {style:'max-width:20em'}}"> <option value="IE">Internet Explorer <option value="FF">Firefox <option value="CH">Chrome <option value="OP">Opera <option value="SA">Safari </select>
And finally, here is the jsFiddle so you can test it out:
As you review the code, you will notice that it is very ojSelect
specific. As I mentioned before, not every Oracle JET input component requires a custom handler to change the tabIndex
. For those that do, however, it wouldn't take much to rework this custom handler into something generic that could be used with a variety of ojComponent
elements.