Skip to content

Commit

Permalink
Merge 746256a into 1021e9c
Browse files Browse the repository at this point in the history
  • Loading branch information
EWhite613 committed Aug 9, 2018
2 parents 1021e9c + 746256a commit b619e21
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
10 changes: 9 additions & 1 deletion addon/components/inputs/autocomplete.js
Original file line number Diff line number Diff line change
@@ -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({
Expand Down Expand Up @@ -83,6 +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) {
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/components/inputs/autocomplete-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,51 @@ 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')
})
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)
})
})
})

0 comments on commit b619e21

Please sign in to comment.