2
0
mirror of https://github.com/tenrok/vue-select.git synced 2026-06-22 10:30:34 +03:00

Potentially ready to merge!

This commit is contained in:
Jeff Sagal
2016-05-26 17:10:07 -07:00
parent 655e612aaf
commit 5214b56885
2 changed files with 71 additions and 31 deletions
+20 -6
View File
@@ -278,9 +278,14 @@
default: false default: false
}, },
/**
* When true, newly created tags will be added to
* the options list.
* @type {Boolean}
*/
pushTags: { pushTags: {
type: Boolean, type: Boolean,
default: true default: false
}, },
/** /**
@@ -473,7 +478,7 @@
if( this.filteredOptions[ this.typeAheadPointer ] ) { if( this.filteredOptions[ this.typeAheadPointer ] ) {
this.select( this.filteredOptions[ this.typeAheadPointer ] ); this.select( this.filteredOptions[ this.typeAheadPointer ] );
} else if (this.taggable && this.search.length){ } else if (this.taggable && this.search.length){
this.select(this.search) this.select(this.search)
} }
if( this.clearSearchOnSelect ) { if( this.clearSearchOnSelect ) {
@@ -505,7 +510,13 @@
} }
}, },
/**
* Determine if an option exists
* within this.options array.
*
* @param {Object || String} option
* @return {boolean}
*/
optionExists(option) { optionExists(option) {
let exists = false let exists = false
@@ -546,9 +557,12 @@
}, },
/** /**
* The currently available options, filtered * The currently displayed options, filtered
* by the search elements value. * by the search elements value. If tagging
* @return {[type]} [description] * true, the search text will be prepended
* if it doesn't already exist.
*
* @return {array}
*/ */
filteredOptions() { filteredOptions() {
let options = this.$options.filters.filterBy(this.options, this.search) let options = this.$options.filters.filterBy(this.options, this.search)
+51 -25
View File
@@ -3,6 +3,13 @@
import Vue from 'vue' import Vue from 'vue'
import vSelect from '../../src/components/Select.vue' import vSelect from '../../src/components/Select.vue'
/**
* Simulate a DOM event.
* @param target
* @param event
* @param process
* @returns {Event}
*/
function trigger(target, event, process) { function trigger(target, event, process) {
var e = document.createEvent('HTMLEvents') var e = document.createEvent('HTMLEvents')
e.initEvent(event, true, true) e.initEvent(event, true, true)
@@ -11,7 +18,16 @@ function trigger(target, event, process) {
return e return e
} }
function searchSubmit(vm) { /**
* Optionally set the search term, then simulate a return keypress.
* @param vm
* @param search
*/
function searchSubmit(vm, search = false) {
if( search ) {
vm.$children[0].search = search
}
trigger(vm.$children[0].$els.search, 'keyup', function (e) { trigger(vm.$children[0].$els.search, 'keyup', function (e) {
e.keyCode = 13 e.keyCode = 13
}) })
@@ -228,7 +244,7 @@ describe('Select.vue', () => {
describe('When Tagging Is Enabled', () => { describe('When Tagging Is Enabled', () => {
it('can determine if a given option string already exists', () => { it('can determine if a given option string already exists', () => {
const vm = new Vue({ const vm = new Vue({
template: '<div><v-select v-ref:select :options="options" :taggable="true"></v-select></div>', template: '<div><v-select v-ref:select :options="options" taggable></v-select></div>',
components: {vSelect}, components: {vSelect},
data: { data: {
options: ['one', 'two'] options: ['one', 'two']
@@ -241,7 +257,7 @@ describe('Select.vue', () => {
it('can determine if a given option object already exists', () => { it('can determine if a given option object already exists', () => {
const vm = new Vue({ const vm = new Vue({
template: '<div><v-select v-ref:select :options="options" :taggable="true"></v-select></div>', template: '<div><v-select v-ref:select :options="options" taggable></v-select></div>',
components: {vSelect}, components: {vSelect},
data: { data: {
options: [{label: 'one'}, {label: 'two'}] options: [{label: 'one'}, {label: 'two'}]
@@ -254,7 +270,7 @@ describe('Select.vue', () => {
it('can determine if a given option object already exists when using custom labels', () => { it('can determine if a given option object already exists when using custom labels', () => {
const vm = new Vue({ const vm = new Vue({
template: '<div><v-select v-ref:select :options="options" label="foo" :taggable="true"></v-select></div>', template: '<div><v-select v-ref:select :options="options" label="foo" taggable></v-select></div>',
components: {vSelect}, components: {vSelect},
data: { data: {
options: [{foo: 'one'}, {foo: 'two'}] options: [{foo: 'one'}, {foo: 'two'}]
@@ -267,7 +283,7 @@ describe('Select.vue', () => {
it('can add the current search text as the first item in the options list', () => { it('can add the current search text as the first item in the options list', () => {
const vm = new Vue({ const vm = new Vue({
template: '<div><v-select :options="options" :value.sync="value" :multiple="true" :taggable="true"></v-select></div>', template: '<div><v-select :options="options" :value.sync="value" :multiple="true" taggable></v-select></div>',
components: {vSelect}, components: {vSelect},
data: { data: {
value: ['one'], value: ['one'],
@@ -281,7 +297,7 @@ describe('Select.vue', () => {
it('can select the current search text', (done) => { it('can select the current search text', (done) => {
const vm = new Vue({ const vm = new Vue({
template: '<div><v-select :options="options" :value.sync="value" :multiple="true" :taggable="true"></v-select></div>', template: '<div><v-select :options="options" :value.sync="value" :multiple="true" taggable></v-select></div>',
components: {vSelect}, components: {vSelect},
data: { data: {
value: ['one'], value: ['one'],
@@ -289,10 +305,7 @@ describe('Select.vue', () => {
} }
}).$mount() }).$mount()
vm.$children[0].search = 'three' searchSubmit(vm, 'three')
searchSubmit(vm)
Vue.nextTick(() => { Vue.nextTick(() => {
expect(vm.$children[0].$get('value')).toEqual(['one', 'three']) expect(vm.$children[0].$get('value')).toEqual(['one', 'three'])
done() done()
@@ -301,7 +314,7 @@ describe('Select.vue', () => {
it('will add a freshly created option/tag to the options list when pushTags is true', () => { it('will add a freshly created option/tag to the options list when pushTags is true', () => {
const vm = new Vue({ const vm = new Vue({
template: '<div><v-select :options="options" pushTags :value.sync="value" :multiple="true" :taggable="true"></v-select></div>', template: '<div><v-select :options="options" push-tags :value.sync="value" :multiple="true" taggable></v-select></div>',
components: {vSelect}, components: {vSelect},
data: { data: {
value: ['one'], value: ['one'],
@@ -309,17 +322,28 @@ describe('Select.vue', () => {
} }
}).$mount() }).$mount()
vm.$children[0].search = 'three' searchSubmit(vm, 'three')
searchSubmit(vm)
expect(vm.$children[0].options).toEqual(['one', 'two', 'three']) expect(vm.$children[0].options).toEqual(['one', 'two', 'three'])
}) })
it('wont add a freshly created option/tag to the options list when pushTags is false', () => {
const vm = new Vue({
template: '<div><v-select :options="options" :value.sync="value" :multiple="true" :taggable="true"></v-select></div>',
components: {vSelect},
data: {
value: ['one'],
options: ['one', 'two']
}
}).$mount()
searchSubmit(vm, 'three')
expect(vm.$children[0].options).toEqual(['one', 'two'])
})
it('will select an existing option if the search string matches a string from options', (done) => { it('will select an existing option if the search string matches a string from options', (done) => {
let two = 'two' let two = 'two'
const vm = new Vue({ const vm = new Vue({
template: '<div><v-select :options="options" :value.sync="value" :multiple="true" :taggable="true"></v-select></div>', template: '<div><v-select :options="options" :value.sync="value" :multiple="true" taggable></v-select></div>',
components: {vSelect}, components: {vSelect},
data: { data: {
value: null, value: null,
@@ -337,24 +361,26 @@ describe('Select.vue', () => {
}) })
it('will select an existing option if the search string matches an objects label from options', (done) => { it('will select an existing option if the search string matches an objects label from options', (done) => {
let two = {label: 'two'}
const vm = new Vue({ const vm = new Vue({
template: '<div><v-select :options="options" :value.sync="value" multiple :taggable="true"></v-select></div>', template: '<div><v-select :options="options" taggable></v-select></div>',
components: {vSelect}, components: {vSelect},
data: { data: {
value: null, options: [{label: 'one'}, two]
options: [{label: 'one'}, {label: 'two'}]
} }
}).$mount() }).$mount()
vm.$children[0].search = 'two' vm.$children[0].search = 'two'
searchSubmit(vm)
Vue.nextTick(() => { Vue.nextTick(() => {
console.dir(JSON.stringify(vm.$children[0].options)) searchSubmit(vm)
console.dir(JSON.stringify(vm.$children[0].value))
expect(vm.$children[0].value).toEqual({label: 'two'}) // This needs to be wrapped in nextTick() twice so that filteredOptions can
done() // calculate after setting the search text, and move the typeAheadPointer index to 0.
Vue.nextTick(() => {
expect(vm.$children[0].value.label).toBe(two.label)
done()
})
}) })
}) })
}) })