Skip to content

Commit

Permalink
support for searchText binding (#524)
Browse files Browse the repository at this point in the history
* kludge to fix glimmer2 support pending interaction from ember team

* fixed issue with ember 2.7+ select-options

* API change for autocomplete and support for tracking search text

* fixed floating label issue

* added readonly helper to searchText
  • Loading branch information
ibarrick authored and miguelcobain committed Oct 17, 2016
1 parent 8b78516 commit 374c005
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
19 changes: 15 additions & 4 deletions addon/components/paper-autocomplete-trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ export default Component.extend({
tagName: 'md-autocomplete-wrap',
classNameBindings: ['noLabel:md-whiteframe-z1', 'select.isOpen:md-menu-showing'],

text: computed('selected', 'extra.labelPath', {
text: computed('selected', 'extra.labelPath', 'searchText', {
get() {
return this.getSelectedAsText();
if (this.get('selected')) {
return this.getSelectedAsText();
} else {
return this.get('searchText');
}
},
set(_, v) {
return v;
if (!this.get('selected') && this.get('searchText')) {
return this.get('searchText');
} else {
return v;
}
}
}),

Expand Down Expand Up @@ -66,11 +74,14 @@ export default Component.extend({
},

handleInputLocal(e) {
// If something is already selected when the user types, it should clear selection
if (this.get('selected')) {
this.get('select').actions.select(null);
}
this.get('onInput')(e.target ? e : { target: { value: e } });
this.set('text', e.target ? e.target.value : e);
}
},

// Methods
getSelectedAsText() {
let labelPath = this.get('extra.labelPath');
Expand Down
4 changes: 3 additions & 1 deletion addon/components/paper-autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export default PowerSelect.extend({
return { labelPath: this.get('labelPath'), label: this.get('label') };
}),

onchange: computed.alias('onChange'),
onchange: computed.alias('onSelectionChange'),
searchText: '',
onSearchTextChange: computed.alias('oninput'),
onfocus: computed.alias('onFocus'),
onblur: computed.alias('onBlur'),

Expand Down
1 change: 1 addition & 0 deletions app/templates/components/paper-autocomplete.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
onKeydown=(action "onKeydown")
searchEnabled=(readonly searchEnabled)
searchField=(readonly searchField)
searchText=(readonly searchText)
select=(readonly publicAPI)
selected=(readonly selected)
selectedItemComponent=(readonly selectedItemComponent)
Expand Down
10 changes: 7 additions & 3 deletions tests/dummy/app/templates/demo/autocomplete.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
onCreate=(action "addCountry")
options=items
allowClear=true
searchText=countrySearch
onSearchTextChange=(action (mut countrySearch))
selected=selectedCountry
search=(if simulateQuery (action "searchCountries"))
searchField="name"
labelPath="name"
placeholder="Select a Country..."
loadingMessage="Searching countries..."
noMatchesMessage="Oops this country doesn't exist. Create a new country?"
onChange=(action (mut selectedCountry)) as |country select|}}
onSelectionChange=(action (mut selectedCountry)) as |country select|}}
{{paper-autocomplete-highlight
label=country.name
searchText=select.searchText
Expand All @@ -34,6 +36,8 @@
{{else}}
Nothing selected...
{{/if}}
<br>
Search text is {{countrySearch}}
</p>
{{#paper-checkbox
value=firstDisabled
Expand Down Expand Up @@ -67,7 +71,7 @@
placeholder="Type e.g. ember, paper, one, two etc."
options=arrayOfItems
selected=selectedItem
onChange=(action (mut selectedItem))
onSelectionChange=(action (mut selectedItem))
as |item term|}}
<span class="item-title">
{{paper-icon "star"}}
Expand Down Expand Up @@ -113,7 +117,7 @@
label="Select a Country..."
loadingMessage="Searching countries..."
noMatchesMessage="Oops this country doesn't exist."
onChange=(action (mut selectedCountry2)) as |country select|}}
onSelectionChange=(action (mut selectedCountry2)) as |country select|}}
{{paper-autocomplete-highlight
label=country.name
searchText=select.searchText
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/components/paper-autocomplete-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test('opens on click', function(assert) {
placeholder="Item"
options=items
selected=selectedItem
onChange=(action (mut selectedItem))
onSelectionChange=(action (mut selectedItem))
as |item|
}}
{{item}}
Expand All @@ -40,7 +40,7 @@ test('backdrop removed if select closed', function(assert) {
placeholder="Item"
options=items
selected=selectedItem
onChange=(action (mut selectedItem))
onSelectionChange=(action (mut selectedItem))
as |item|
}}
{{item}}
Expand Down Expand Up @@ -71,7 +71,7 @@ test('should render only enough items to fill the menu + 3', function(assert) {
placeholder="Item"
options=items
selected=selectedItem
onChange=(action (mut selectedItem))
onSelectionChange=(action (mut selectedItem))
as |item|
}}
{{item}}
Expand Down

0 comments on commit 374c005

Please sign in to comment.