Skip to content Skip to sidebar Skip to footer

How To Disable Focus For An Option In `dijit/form/Select`?

I need to change behavior for a widget dijit/form/Select. Widget should not allow focus (from mouse or using the keyboard) for options which have property disabled: true. I would l

Solution 1:

I was able to solve this issue using a monkey patch.

The solution consist into:

  • Adding markup in label so we can customize the look.
  • Adding property disabled:true in item options, so item won't fire onChange.
  • Disabling focus on group item when mouse and keyboard are used.

I still very interesting to know if another better/cleaner solutions exists.

  require([
    'dijit/form/Select',
    'dojo/_base/window',
    'dojo/domReady!'
  ], function(Select, win) {
    var select = new Select({
      name: 'select2',
      options: [{
        label: '<i>Swedish Cars</i>',
        value: '',
        group: true,
        disabled: true
      }, {
        label: 'Volvo',
        value: 'volvo'
      }, {
        label: 'Saab',
        value: 'saab',
        selected: true
      }, {
        label: '<i>German Cars</i>',
        value: '',
        group: true,
        disabled: true
      }, {
        label: 'Mercedes',
        value: 'mercedes'
      }, {
        label: 'Audi',
        value: 'audi'
      }]
    }, 'select');

    select.on('change', function(value) {
      alert(value);
    });

    select.dropDown._onUpArrow = function() {
      this.dropDown.focusPrev()
    }.bind(select);

    select.dropDown._onDownArrow = function() {
      this.dropDown.focusNext()
    }.bind(select);


    select.dropDown.focusNext = function() {
      var next = this.dropDown._getNextFocusableChild(this.dropDown.focusedChild, 1);
      var nextSuccessive;
      var nextFinal;
      if (next.option.group) {
        var next = this.dropDown._getNextFocusableChild(this.dropDown.focusedChild, 1);
        this.dropDown.focusChild(next);
        nextSuccessive = this.dropDown._getNextFocusableChild(this.dropDown.focusedChild, 1);
        nextFinal = nextSuccessive;
      } else {
        nextFinal = next;
      }
      this.dropDown.focusChild(nextFinal);
    }.bind(select);

    select.dropDown.focusPrev = function() {
      var prev = this.dropDown._getNextFocusableChild(this.dropDown.focusedChild, -1);
      var prevSuccessive;
      var prevFinal;
      if (prev.option.group) {
        var prev = this.dropDown._getNextFocusableChild(this.dropDown.focusedChild, -1);
        this.dropDown.focusChild(prev);
        prevSuccessive = this.dropDown._getNextFocusableChild(this.dropDown.focusedChild, -1);
        prevFinal = prevSuccessive;
      } else {
        prevFinal = prev;
      }
      this.dropDown.focusChild(prevFinal, true);
    }.bind(select);

    select.dropDown.onItemHover = function( /*MenuItem*/ item) {
      if (item.option.group) {
        item._set("hovering", false);
        console.log('skip hover');
        return;
      }

      if (this.activated) {
        this.set("selected", item);
        if (item.popup && !item.disabled && !this.hover_timer) {
          this.hover_timer = this.defer(function() {
            this._openItemPopup(item);
          }, this.popupDelay);
        }
      } else if (this.passivePopupDelay < Infinity) {
        if (this.passive_hover_timer) {
          this.passive_hover_timer.remove();
        }
        this.passive_hover_timer = this.defer(function() {
          this.onItemClick(item, {
            type: "click"
          });
        }, this.passivePopupDelay);
      }

      this._hoveredChild = item;

      item._set("hovering", true);
    }.bind(select.dropDown);

    select.dropDown._onItemFocus = function( /*MenuItem*/ item) {
      if (item.option.group) {
        return;
      }
      if (this._hoveredChild && this._hoveredChild != item) {
        this.onItemUnhover(this._hoveredChild);
      }
      this.set("selected", item);
    }.bind(select.dropDown);
  });
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/dojo/1.12.1/dijit/themes/claro/claro.css" />

<script>
  window.dojoConfig = {
    parseOnLoad: false,
    async: true
  };
</script>


<script src="//ajax.googleapis.com/ajax/libs/dojo/1.12.1/dojo/dojo.js"></script>

<body class="claro">
  <div id="select"></div>
</body>

Post a Comment for "How To Disable Focus For An Option In `dijit/form/Select`?"