Skip to content

Commit a0c8efe

Browse files
authored
Merge pull request sagalbot#512 from eriknygren/fix-unexpected-wrapping-on-single-select
Fixing unexpected linebreak on single selects
2 parents fd32f50 + 16b6bd7 commit a0c8efe

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed

src/components/Select.vue

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
padding: 0;
7474
background: none;
7575
border: 1px solid rgba(60, 60, 60, .26);
76+
min-height: 36px;
7677
border-radius: 4px;
7778
white-space: normal;
7879
}
@@ -213,6 +214,17 @@
213214
.v-select.unsearchable input[type="search"]:hover {
214215
cursor: pointer;
215216
}
217+
.v-select input[type="search"].hidden {
218+
width: 0px;
219+
padding: 0;
220+
}
221+
.v-select input[type="search"].shrunk {
222+
width: auto;
223+
}
224+
.v-select input[type="search"].empty {
225+
width: 100%;
226+
}
227+
216228
/* List Items */
217229
.v-select li {
218230
line-height: 1.42857143; /* Normalize line height */
@@ -336,12 +348,12 @@
336348
@focus="onSearchFocus"
337349
type="search"
338350
class="form-control"
351+
:class="inputClasses"
339352
autocomplete="off"
340353
:disabled="disabled"
341354
:placeholder="searchPlaceholder"
342355
:tabindex="tabindex"
343356
:readonly="!searchable"
344-
:style="{ width: isValueEmpty ? '100%' : 'auto' }"
345357
:id="inputId"
346358
aria-label="Search for option"
347359
>
@@ -972,6 +984,18 @@
972984
}
973985
},
974986
987+
/**
988+
* Classes to be output on input.form-control
989+
* @return {Object}
990+
*/
991+
inputClasses() {
992+
return {
993+
hidden: !this.multiple && !this.isValueEmpty && !this.dropdownOpen,
994+
shrunk: this.multiple && !this.isValueEmpty,
995+
empty: this.isValueEmpty,
996+
}
997+
},
998+
975999
/**
9761000
* If search text should clear on blur
9771001
* @return {Boolean} True when single and clearSearchOnSelect

test/unit/specs/Select.spec.js

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,36 @@ describe('Select.vue', () => {
249249
expect(vm.$children[0].isOptionSelected('foo')).toEqual(true)
250250
}),
251251

252+
it('applies the "empty" class to the search input when no value is selected', () => {
253+
const vm = new Vue({
254+
template: '<div><v-select :options="options" multiple v-model="value"></v-select></div>',
255+
components: {vSelect},
256+
data: {
257+
value: null,
258+
options: [{label: 'one'}]
259+
}
260+
}).$mount()
261+
262+
expect(vm.$children[0].inputClasses.empty).toEqual(true)
263+
expect(vm.$children[0].inputClasses.shrunk).toEqual(false)
264+
expect(vm.$children[0].inputClasses.hidden).toEqual(false)
265+
}),
266+
267+
it('applies the "shrunk" class to the search input when one or more value is selected', () => {
268+
const vm = new Vue({
269+
template: '<div><v-select :options="options" multiple v-model="value"></v-select></div>',
270+
components: {vSelect},
271+
data: {
272+
value: [{label: 'one'}],
273+
options: [{label: 'one'}]
274+
}
275+
}).$mount()
276+
277+
expect(vm.$children[0].inputClasses.shrunk).toEqual(true)
278+
expect(vm.$children[0].inputClasses.empty).toEqual(false)
279+
expect(vm.$children[0].inputClasses.hidden).toEqual(false)
280+
}),
281+
252282
describe('change Event', () => {
253283
it('will trigger the input event when the selection changes', (done) => {
254284
const vm = new Vue({
@@ -1318,7 +1348,56 @@ describe('Select.vue', () => {
13181348
expect(vm.$refs.select.search).toEqual('')
13191349
done()
13201350
})
1321-
})
1351+
})
1352+
1353+
it('should apply the "empty" class to the search input when it does not have a selected value', () => {
1354+
const vm = new Vue({
1355+
template: '<div><v-select ref="select" :options="options" :value="value"></v-select></div>',
1356+
data: {
1357+
value: '',
1358+
options: ['one', 'two', 'three']
1359+
}
1360+
}).$mount()
1361+
expect(vm.$children[0].inputClasses.empty).toEqual(true)
1362+
expect(vm.$children[0].inputClasses.shrunk).toEqual(false)
1363+
expect(vm.$children[0].inputClasses.hidden).toEqual(false)
1364+
})
1365+
1366+
it('should apply the "hidden" class to the search input when a value is present', () => {
1367+
const vm = new Vue({
1368+
template: '<div><v-select ref="select" :options="options" :value="value"></v-select></div>',
1369+
data: {
1370+
value: 'one',
1371+
options: ['one', 'two', 'three']
1372+
}
1373+
}).$mount()
1374+
1375+
expect(vm.$children[0].inputClasses.hidden).toEqual(true)
1376+
expect(vm.$children[0].inputClasses.empty).toEqual(false)
1377+
expect(vm.$children[0].inputClasses.shrunk).toEqual(false)
1378+
})
1379+
1380+
1381+
it('should not apply the "hidden" class to the search input when a value is present, and the dropdown is open', () => {
1382+
const vm = new Vue({
1383+
template: '<div><v-select ref="select" :options="options" :value="value"></v-select></div>',
1384+
data: {
1385+
value: 'one',
1386+
options: ['one', 'two', 'three'],
1387+
open: true
1388+
}
1389+
}).$mount()
1390+
vm.$children[0].toggleDropdown({target: vm.$children[0].$refs.search})
1391+
Vue.nextTick(() => {
1392+
Vue.nextTick(() => {
1393+
expect(vm.$children[0].open).toEqual(true)
1394+
expect(vm.$children[0].inputClasses.hidden).toEqual(false)
1395+
expect(vm.$children[0].inputClasses.empty).toEqual(false)
1396+
expect(vm.$children[0].inputClasses.shrunk).toEqual(false)
1397+
done()
1398+
})
1399+
})
1400+
})
13221401

13231402
it ('should not reset the search input on focus lost when clearSearchOnSelect is false', (done) => {
13241403
const vm = new Vue({

0 commit comments

Comments
 (0)