From 477242e264553e4d51797e50317ab876c850bd38 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 | 30 ++++++++++++++++++++++ addon/templates/components/paper-chips.hbs | 4 +-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/addon/components/paper-chips.js b/addon/components/paper-chips.js index 093127c06..7846a2a6a 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(); @@ -106,6 +120,22 @@ export default Component.extend({ chipsBlur() { if (!this.focusMovingTo(this.getInput())) { 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); } }, diff --git a/addon/templates/components/paper-chips.hbs b/addon/templates/components/paper-chips.hbs index 05dad398a..c4bf1a0a6 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}} -