diff --git a/addon/components/md-input.js b/addon/components/md-input.js index fcca04e8..af5a0df0 100644 --- a/addon/components/md-input.js +++ b/addon/components/md-input.js @@ -1,6 +1,11 @@ +import Ember from 'ember'; import MaterializeInputField from './md-input-field'; import layout from '../templates/components/md-input'; +const { + get, +} = Ember; + export default MaterializeInputField.extend({ layout, type: 'text', @@ -9,5 +14,27 @@ export default MaterializeInputField.extend({ this._super(...arguments); // make sure the label moves when a value is bound. this._setupLabel(); + }, + + actions: { + enter(...args) { + this._sendAction('onEnter', ...args); + }, + + keyPress(...args) { + this._sendAction('onKeyPress', ...args); + }, + + keyUp(...args) { + this._sendAction('onKeyUp', ...args); + } + }, + + _sendAction(action, ...args) { + if (typeof get(this, action) === 'function') { + get(this, action)(...args); + } else { + this.sendAction(action, ...args); + } } }); diff --git a/addon/templates/components/md-input.hbs b/addon/templates/components/md-input.hbs index 5d1d761f..bb58e056 100644 --- a/addon/templates/components/md-input.hbs +++ b/addon/templates/components/md-input.hbs @@ -13,8 +13,12 @@ disabled=disabled autocomplete=autocomplete autofocus=autofocus - focusIn=(action 'inputFocusIn') step=step min=min - max=max}} + max=max + + focusIn=(action 'inputFocusIn') + enter=(action 'enter') + key-press=(action 'keyPress') + key-up=(action 'keyUp')}} diff --git a/tests/dummy/app/controllers/forms.js b/tests/dummy/app/controllers/forms.js index 1e55a7dd..3e0766b8 100644 --- a/tests/dummy/app/controllers/forms.js +++ b/tests/dummy/app/controllers/forms.js @@ -7,7 +7,8 @@ const { computed, isPresent, later, - computed: { not } + computed: { not }, + set, } = Ember; function asJSON(propKey) { @@ -108,5 +109,19 @@ export default Controller.extend({ switchesSelectionsString: asJSON('switchesSelections'), checkboxChoicesString: asJSON('checkboxChoices'), - checkboxSelectionsString: asJSON('checkboxSelections') -}); \ No newline at end of file + checkboxSelectionsString: asJSON('checkboxSelections'), + + actions: { + enter() { + console.log('enter'); + }, + + keyPress() { + console.log('key-press'); + }, + + keyUp() { + console.log('key-up'); + }, + } +}); diff --git a/tests/dummy/app/templates/forms.hbs b/tests/dummy/app/templates/forms.hbs index 17955b6b..907f22a0 100644 --- a/tests/dummy/app/templates/forms.hbs +++ b/tests/dummy/app/templates/forms.hbs @@ -16,9 +16,15 @@ {{component-option optionId="pattern"}} {{component-option optionId="validate" default="false"}} {{component-option optionId="autocomplete"}} + {{component-option optionId="onKeyPress" default="null"}} + {{component-option optionId="onKeyUp" default="null"}} + {{component-option optionId="onEnter" default="null"}} + {{/options-panel}}