How to disable focus for an option in `dijit/form/Select`?

假装没事ソ 提交于 2019-12-12 04:31:48

问题


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 like to know if you can point me out how to achieve this result.

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: '',
        disabled: true
      },
      {
        label: 'Volvo',
        value: 'volvo'
      },
      {
        label: 'Saab',
        value: 'saab',
        selected: true
      },
      {
        label: '<i>German Cars</i>',
        value: '',
        disabled: true
      },
      {
        label: 'Mercedes',
        value: 'mercedes'
      },
      {
        label: 'Audi',
        value: 'audi'
      }
    ]
  }, 'select');

  select.on('change', function(evt) {
    console.log('myselect_event');
  });
});
<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>

回答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>


来源:https://stackoverflow.com/questions/44385939/how-to-disable-focus-for-an-option-in-dijit-form-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!