Last active
April 5, 2017 23:12
-
-
Save lennyburdette/fd17180ca5f3e8580d8fc712a4b6b7d1 to your computer and use it in GitHub Desktop.
sq-field-input-searchable additive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
function wrap(options) { | |
return options.map(value => ({ value })); | |
} | |
export default Ember.Component.extend({ | |
allowNew: false, | |
_filter: null, | |
_internalOptions: null, | |
didReceiveAttrs() { | |
this.set('_internalOptions', wrap(this.get('options'))); | |
const filter = this.get('_filter'); | |
const isEditing = this.get('isEditing'); | |
if (!isEditing) { | |
this.set('_filter', this.get('value')); | |
} | |
}, | |
focusIn() { | |
this.set('isEditing', true); | |
}, | |
focusOut() { | |
this.set('isEditing', false); | |
}, | |
input(event) { | |
const value = event.target.value; | |
this.set('_filter', value); | |
if (this.filterChanged) { | |
this.sendAction('filterChanged', value); | |
} else { | |
let filtered = this.get('options').slice(); | |
if (value && value.length) { | |
filtered = filtered.filter(v => v.startsWith(value)); | |
if (this.get('allowNew')) { | |
filtered = [{ value, isNew: true }, ...wrap(filtered)]; | |
} | |
} else { | |
filtered = wrap(filtered); | |
} | |
this.set('_internalOptions', filtered); | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
const colors = 'red orange yellow green blue indigo violet'.w(); | |
export default Ember.Controller.extend({ | |
colors: Ember.computed(function() { | |
return colors; | |
}), | |
options: Ember.computed(function() { | |
return colors.slice(); | |
}), | |
value: 'orange', | |
actions: { | |
valueChanged(value) { | |
this.set('value', value); | |
}, | |
filterChanged(value) { | |
let options = colors.filter(v => v.startsWith(value)); | |
if (value && value.length && !colors.includes(value)) { | |
options = [value, ...options]; | |
} | |
this.set('options', options); | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export function contains([needle, haystack]) { | |
return haystack.includes(needle); | |
} | |
export default Ember.Helper.helper(contains); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.12.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.12.0", | |
"ember-template-compiler": "2.12.0", | |
"ember-testing": "2.12.0" | |
}, | |
"addons": { | |
"ember-data": "2.12.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment