From 394b1941359c9f7ab6ab4248deb8dfde6d18d538 Mon Sep 17 00:00:00 2001 From: Eric White Date: Wed, 8 Aug 2018 17:04:42 -0400 Subject: [PATCH 1/3] clear filter --- addon/components/inputs/autocomplete.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/addon/components/inputs/autocomplete.js b/addon/components/inputs/autocomplete.js index c6d3bb62..5a988aba 100644 --- a/addon/components/inputs/autocomplete.js +++ b/addon/components/inputs/autocomplete.js @@ -1,7 +1,7 @@ import SelectInput from './select' import layout from 'ember-frost-bunsen/templates/components/frost-bunsen-input-autocomplete' import Ember from 'ember' -const {A, getWithDefault, isEmpty} = Ember +const {A, getWithDefault, isEmpty, isPresent} = Ember import computed, {readOnly} from 'ember-computed-decorators' export default SelectInput.extend({ @@ -83,6 +83,11 @@ export default SelectInput.extend({ parseValue (data) { return data }, + didUpdateAttrs () { + const {_focused, _isTyping, filter, value} = this.getProperties('_isTyping', '_focused', 'filter', 'value') + // Clear filter when value is cleared + if (isEmpty(value) && isPresent(filter) && !_isTyping && !_focused) this.set('filter', '') + }, // == Actions =============================================================== actions: { onInput (filterValue) { From 3444ca66c1c92b53270479e401a538e0b6797408 Mon Sep 17 00:00:00 2001 From: Eric White Date: Wed, 8 Aug 2018 17:23:56 -0400 Subject: [PATCH 2/3] Clear filter when value cleared --- addon/components/inputs/autocomplete.js | 3 ++ .../components/inputs/autocomplete-test.js | 50 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/addon/components/inputs/autocomplete.js b/addon/components/inputs/autocomplete.js index 5a988aba..8084cb77 100644 --- a/addon/components/inputs/autocomplete.js +++ b/addon/components/inputs/autocomplete.js @@ -83,11 +83,14 @@ export default SelectInput.extend({ parseValue (data) { return data }, + + // == Events ================================================================= didUpdateAttrs () { const {_focused, _isTyping, filter, value} = this.getProperties('_isTyping', '_focused', 'filter', 'value') // Clear filter when value is cleared if (isEmpty(value) && isPresent(filter) && !_isTyping && !_focused) this.set('filter', '') }, + // == Actions =============================================================== actions: { onInput (filterValue) { diff --git a/tests/unit/components/inputs/autocomplete-test.js b/tests/unit/components/inputs/autocomplete-test.js index a9cb5c57..cbdab8e1 100644 --- a/tests/unit/components/inputs/autocomplete-test.js +++ b/tests/unit/components/inputs/autocomplete-test.js @@ -116,4 +116,54 @@ describe('Unit: frost-bunsen-input-autocomplete', function () { expect(result).to.equal(undefined) }) }) + + describe('clear filter when value cleared', function () { + beforeEach(function () { + component.setProperties({ + filter: 'foo', + value: 'boop', + _isTyping: false, + _focused: false + }) + sinon.spy(component, 'set') + }) + afterEach(function () { + component.set.restore() + }) + it('should clear filter', function () { + component.setProperties({value: undefined}) + component.didUpdateAttrs() + expect(component.set.calledOnce).to.equal(true) + expect(component.set.calledWith('filter', '')).to.equal(true) + }) + + it('should not clear filter if value present', function () { + component.didUpdateAttrs() + expect(component.set.called).to.equal(false) + }) + + it('should not clear filter when typing', function () { + component.setProperties({value: undefined, _isTyping: true}) + component.didUpdateAttrs() + expect(component.set.called).to.equal(false) + }) + + it('should not clear filter when focused', function () { + component.setProperties({value: undefined, _focused: true}) + component.didUpdateAttrs() + expect(component.set.called).to.equal(false) + }) + + it('should not clear filter when no value or filter', function () { + component.setProperties({value: undefined, filter: ''}) + component.didUpdateAttrs() + expect(component.set.called).to.equal(false) + }) + + it('should not clear filter when value and filter updated', function () { + component.setProperties({value: 'test', filter: 'test'}) + component.didUpdateAttrs() + expect(component.set.called).to.equal(false) + }) + }) }) From 746256a9a39596a16d430413843aa6229e7f8a17 Mon Sep 17 00:00:00 2001 From: Eric White Date: Thu, 9 Aug 2018 10:28:12 -0400 Subject: [PATCH 3/3] no need for after each restore --- tests/unit/components/inputs/autocomplete-test.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/unit/components/inputs/autocomplete-test.js b/tests/unit/components/inputs/autocomplete-test.js index cbdab8e1..52edbc69 100644 --- a/tests/unit/components/inputs/autocomplete-test.js +++ b/tests/unit/components/inputs/autocomplete-test.js @@ -127,9 +127,6 @@ describe('Unit: frost-bunsen-input-autocomplete', function () { }) sinon.spy(component, 'set') }) - afterEach(function () { - component.set.restore() - }) it('should clear filter', function () { component.setProperties({value: undefined}) component.didUpdateAttrs()