ember-power-select-with-create
Advanced tools
Comparing version
import Ember from 'ember'; | ||
import layout from '../templates/components/power-select-with-create'; | ||
const { computed, isBlank } = Ember; | ||
import { filterOptions, defaultMatcher } from 'ember-power-select/utils/group-utils'; | ||
const { computed, get } = Ember; | ||
@@ -8,2 +9,3 @@ export default Ember.Component.extend({ | ||
layout: layout, | ||
matcher: defaultMatcher, | ||
@@ -13,17 +15,8 @@ // Lifecycle hooks | ||
this._super(...arguments); | ||
this.suggestion = { __id__: '__suggestion__', value: '' }; | ||
Ember.assert('{{power-select}} requires an `oncreate` function', this.get('oncreate') && typeof this.get('oncreate') === 'function'); | ||
Ember.assert('{{power-select-with-create}} requires an `oncreate` function', this.get('oncreate') && typeof this.get('oncreate') === 'function'); | ||
}, | ||
// CPs | ||
optionsArray: computed('options.[]', 'searchTerm', 'labelPath', function() { | ||
const { searchTerm, suggestion, labelPath } = this.getProperties('searchTerm', 'suggestion', 'labelPath'); | ||
const optionsAry = Ember.A(this.get('options')).toArray(); | ||
if (isBlank) { | ||
return optionsAry; | ||
} | ||
suggestion.value = searchTerm; | ||
suggestion[labelPath] = `Add "${searchTerm}"`; | ||
optionsAry.unshift(suggestion); | ||
return optionsAry; | ||
optionsArray: computed('options.[]', function() { | ||
return Ember.A(this.get('options')).toArray(); | ||
}), | ||
@@ -34,9 +27,17 @@ | ||
searchAndSuggest(term) { | ||
let newOptions; | ||
if (term.length > 0) { | ||
newOptions = this.get('optionsArray').filter(e => e.name.indexOf(term) > -1); | ||
newOptions.unshift({ __id__: '__suggestion__', __value__: term, name: this.buildSuggestionLabel(term) }); | ||
} else { | ||
newOptions = this.get('optionsArray'); | ||
let newOptions = this.get('optionsArray'); | ||
if (term.length === 0) { | ||
return newOptions; | ||
} | ||
if (this.get('search')) { | ||
return Ember.RSVP.resolve(this.get('search')(term)).then((results) => { | ||
results.unshift(this.buildSuggestionForTerm(term)); | ||
return results; | ||
}); | ||
} | ||
newOptions = this.filter(Ember.A(newOptions), term); | ||
newOptions.unshift(this.buildSuggestionForTerm(term)); | ||
return newOptions; | ||
@@ -46,3 +47,3 @@ }, | ||
selectOrCreate(option) { | ||
if (option && option.__id__ === '__suggestion__') { | ||
if (option && option.__isSuggestion__) { | ||
this.get('oncreate')(option.__value__); | ||
@@ -56,2 +57,20 @@ } else { | ||
// Methods | ||
filter(options, searchText) { | ||
let matcher; | ||
if (this.get('searchField')) { | ||
matcher = (option, text) => this.matcher(get(option, this.get('searchField')), text); | ||
} else { | ||
matcher = (option, text) => this.matcher(option, text); | ||
} | ||
return filterOptions(options || [], searchText, matcher); | ||
}, | ||
buildSuggestionForTerm(term) { | ||
return { | ||
__isSuggestion__: true, | ||
__value__: term, | ||
text: this.buildSuggestionLabel(term), | ||
}; | ||
}, | ||
buildSuggestionLabel(term) { | ||
@@ -58,0 +77,0 @@ let buildSuggestion = this.get('buildSuggestion'); |
@@ -9,2 +9,11 @@ /*jshint node:true*/ | ||
{ | ||
name: 'ember-1.13', | ||
dependencies: { | ||
'ember': '1.13.11' | ||
}, | ||
resolutions: { | ||
'ember': '1.13.11' | ||
} | ||
}, | ||
{ | ||
name: 'ember-release', | ||
@@ -11,0 +20,0 @@ dependencies: { |
{ | ||
"name": "ember-power-select-with-create", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "The default blueprint for ember-cli addons.", | ||
@@ -39,3 +39,3 @@ "directories": { | ||
"ember-pagefront": "0.9.9", | ||
"ember-power-select": "^0.7.0-beta.5", | ||
"ember-power-select": "^0.7.0-beta.6", | ||
"ember-try": "~0.0.8" | ||
@@ -42,0 +42,0 @@ }, |
@@ -5,2 +5,8 @@ # Ember-power-select-with-create | ||
### Installation | ||
``` | ||
ember install ember-power-select-with-create | ||
``` | ||
### Usage | ||
@@ -16,2 +22,2 @@ | ||
[https://ember-power-select-with-create.pagefrontapp.com/](https://ember-power-select-with-create.pagefrontapp.com/) | ||
[https://ember-power-select-with-create.pagefrontapp.com/](https://ember-power-select-with-create.pagefrontapp.com/) |
Sorry, the diff of this file is not supported yet
8406
8.03%145
18.85%22
46.67%