Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow selectedValue to accept null for clearing selection #45

Merged
merged 1 commit into from
Mar 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ ember install ember-frost-select
| `on-change` | `string` | `<action-name>` | The action callback to call when the value of the select component changes |
| `on-input` | `string` | `<action-name>` | The action callback to call when the value of the filter changes as the user types |
| `selected` | `number` or `array` | `1` or `[1, 2]` | The indices of the pre-selected values corresponding to values in the passed-in data. |
| `selectedValue` | `any` or `array`, if using multi-select | `'bar'` or `['bar', 'buzz']` | A value to choose in the drop down programmatically, or an array of values if using multi-select. Takes precedence over `selected` attribute. |
| `selectedValue` | `any`, `array` if using multi-select, `null` to clear | `'bar'` or `['bar', 'buzz']` | A value to choose in the drop down programmatically, or an array of values if using multi-select. Takes precedence over `selected` attribute. Passing `null` will clear the selected state. |

## Examples
Assuming the following data is available in the consuming context:
Expand Down
9 changes: 7 additions & 2 deletions addon/components/frost-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function isAttrDifferent (newAttrs, oldAttrs, attributeName) {
let oldValue = _.get(oldAttrs, attributeName + '.value')
let newValue = _.get(newAttrs, attributeName + '.value')

if (newValue && !_.isEqual(oldValue, newValue)) {
if (newValue !== undefined && !_.isEqual(oldValue, newValue)) {
return true
}
return false
Expand Down Expand Up @@ -388,7 +388,7 @@ export default Ember.Component.extend({

// TODO: add jsdoc
select (index) {
let selected = [index]
let selected = index === null ? [] : [index]
let values = this.getValues(selected)
this.set('selected', selected)
this.closeList()
Expand All @@ -399,6 +399,11 @@ export default Ember.Component.extend({

// TODO: add jsdoc
selectOptionByValue (selectedValue) {
if (selectedValue === null) {
this.select(null)
return
}

// Find index
let valueIndex = _.findIndex(this.get('items'), (item) => _.isEqual(item.value, selectedValue))

Expand Down
11 changes: 11 additions & 0 deletions tests/integration/components/frost-multi-select-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,18 @@ describeComponent(
wait(() => {
let input = this.$('.frost-select .trigger')
expect(input.val()).to.eql('Ghostface')
done()
})
})
})

it('clears selection if selectedValue is set to null', function (done) {
this.$('.frost-select li:first-child').click()
wait(() => {
this.set('selectedValue', null)
wait(() => {
let input = this.$('.frost-select .trigger')
expect(input.val()).to.eql('')
done()
})
})
Expand Down