From d9f11b0f0bdfeda4370ace5c8e4d66537283bc86 Mon Sep 17 00:00:00 2001 From: Gorzas Date: Fri, 18 Aug 2017 11:24:00 +0200 Subject: [PATCH 1/3] Add actions of base input in md-input Activates the following actions: - onKeyPress - onKeyUp - enter It also mantains the compatibility with the method sendAction (Ember 2.7 and before) and it introduces the action functions as proposed in the lastest versions of Ember: https://guides.emberjs.com/v2.14.0/components/triggering-changes-with-actions/ --- addon/components/md-input.js | 27 +++++++++++++++++++++++++ addon/templates/components/md-input.hbs | 8 ++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) 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')}} From 855a2df8dfc39463ee116162ef3fd5d2096eec0b Mon Sep 17 00:00:00 2001 From: Gorzas Date: Fri, 18 Aug 2017 11:26:34 +0200 Subject: [PATCH 2/3] Include example of inputs with actions --- tests/dummy/app/controllers/forms.js | 21 ++++++++++++++++--- tests/dummy/app/templates/forms.hbs | 2 ++ .../templates/snippets/input-with-actions.hbs | 10 +++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 tests/dummy/app/templates/snippets/input-with-actions.hbs 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..16449a81 100644 --- a/tests/dummy/app/templates/forms.hbs +++ b/tests/dummy/app/templates/forms.hbs @@ -19,6 +19,8 @@ {{/options-panel}}
Basic Input
{{example-snippet snippet='input-basic' class='input-example'}} +
Input with actions
+ {{example-snippet snippet='input-with-actions' class='input-example'}}
Input With Icon
{{example-snippet snippet='input-with-icon' class='input-example'}}
Typed Inputs
diff --git a/tests/dummy/app/templates/snippets/input-with-actions.hbs b/tests/dummy/app/templates/snippets/input-with-actions.hbs new file mode 100644 index 00000000..fb7e0959 --- /dev/null +++ b/tests/dummy/app/templates/snippets/input-with-actions.hbs @@ -0,0 +1,10 @@ +{{md-input + class="col s6" + label='Example' + placeholder="Start typing!" + value="" + + onKeyPress="keyPress" + onKeyUp="keyUp" + onEnter="enter" +}} From 6231b8d9f47b0eaefa3b6b43f08532733edc9017 Mon Sep 17 00:00:00 2001 From: Gorzas Date: Fri, 18 Aug 2017 11:28:39 +0200 Subject: [PATCH 3/3] Add docs about actions on md-input Close #413 --- tests/dummy/app/templates/forms.hbs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/dummy/app/templates/forms.hbs b/tests/dummy/app/templates/forms.hbs index 16449a81..907f22a0 100644 --- a/tests/dummy/app/templates/forms.hbs +++ b/tests/dummy/app/templates/forms.hbs @@ -16,6 +16,10 @@ {{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}}
Basic Input
{{example-snippet snippet='input-basic' class='input-example'}}