From 92f0abf9ac0c84ceec3d4846465ec09e9ca59bb6 Mon Sep 17 00:00:00 2001 From: Paul Nicholls Date: Fri, 13 Jan 2017 12:04:12 +1300 Subject: [PATCH] Chips: Add missing mouse interactions Add the ability to click anywhere in the chips element to give the input focus, plus the ability to click on a chip to focus it. --- addon/components/paper-chips.js | 32 ++++++++++++++++++- addon/templates/components/paper-chips.hbs | 4 +-- .../components/paper-contact-chips.hbs | 4 +-- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/addon/components/paper-chips.js b/addon/components/paper-chips.js index a02b6f84e..ffd848c26 100644 --- a/addon/components/paper-chips.js +++ b/addon/components/paper-chips.js @@ -33,6 +33,10 @@ export default Component.extend({ } }), + click() { + this.getInput().focus(); + }, + actions: { addItem(newItem) { if (this.get('requireMatch')) { @@ -58,6 +62,16 @@ export default Component.extend({ } }, + removeItem(item) { + this.sendAction('removeItem', item); + let current = this.get('activeChip'); + + if (current === -1 || current >= this.get('content').length) { + this.queueReset(); + this.set('activeChip', -1); + } + }, + inputFocus(autocomplete) { let input = this.getInput(); @@ -104,6 +118,22 @@ export default Component.extend({ chipsBlur(event) { if (!this.focusMovingTo(this.getInput(), event)) { this.set('focusedElement', 'none'); + this.set('activeChip', -1); + } + }, + + chipClick(index, event) { + // Prevent click from bubbling up to the chips element. + event.stopPropagation(); + + // If we have a valid chip index, make it active. + if (!isEmpty(index)) { + // Shift actual focus to wrap so that subsequent blur events work as expected. + this.$('md-chips-wrap').focus(); + + // Update state to reflect the clicked chip being active. + this.set('focusedElement', 'chips'); + this.set('activeChip', index); } }, @@ -229,7 +259,7 @@ export default Component.extend({ }, focusMovingTo(selector, event) { - if (!isEmpty(event) && !isEmpty(event.relatedTarget) && this.$(event.relatedTarget).is(selector)) { + if (!isEmpty(event) && !isEmpty(event.relatedTarget) && this.$().find(event.relatedTarget).is(selector)) { return true; } diff --git a/addon/templates/components/paper-chips.hbs b/addon/templates/components/paper-chips.hbs index edb6b0508..71afa391e 100644 --- a/addon/templates/components/paper-chips.hbs +++ b/addon/templates/components/paper-chips.hbs @@ -5,7 +5,7 @@ onfocus={{action "chipsFocus"}} onblur={{action "chipsBlur"}}> {{#each content as |item index|}} - +
{{#unless readOnly}} - diff --git a/addon/templates/components/paper-contact-chips.hbs b/addon/templates/components/paper-contact-chips.hbs index 38d11fcda..6c2d3ca2b 100644 --- a/addon/templates/components/paper-contact-chips.hbs +++ b/addon/templates/components/paper-contact-chips.hbs @@ -1,7 +1,7 @@ {{#each content as |item index|}} - +