Search code examples
dartdart-polymerpaper-elements

Consuming keys in dart


I have the following

  <paper-input floatinglabel
    id="frequency"
    label="Frequency"
    type='number'
    inputValue='{{frequency.frequency | asInteger}}'
    validate='{{digits_regex}}'
    error="{{errorMsg}}"
    on-keyUp='{{consumeKey}}'
    on-mouseout='{{publishInstance}}'>
  </paper-input>

Given that the type is 'number', only when the key input is a number I would like for anything to be seen in the input field. How can I accomplish that with the consumeKey function?

I also thought that might be built-in in Dart. Is it planned?


Solution

  • You can't prevent input on on-keyup you can only use on-keydown and your consumeKey method should look like

    void consumeKey(KeyboardEvent e) {
      if(e.keyCode < '0'.codeUnits[0] || e.keyCode > '9'.codeUnits[0]) {
        e.preventDefault();
      }
    }
    

    If you want to allow other characters you have to extend the condition to allow for example - + , .