Skip to content

Commit f7594fa

Browse files
committed
[dropdown] force selection on search mode
1 parent a69d5e8 commit f7594fa

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

demo/src/docs/decorators/form/basic/view.pug

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ div
33
ui-input(label='Last Name', placeholder='Please enter last name', v-model='lastName')
44
ui-input(label='Email', placeholder='Please enter email address', v-model='email')
55
ui-input(label='Street Address', placeholder='Please enter street address', v-model='street')
6-
ui-dropdown(label='Country', v-model='country', placeholder='Please enter country')
7-
ui-dropdown-item(v-for='c in countries', :value='c') {{c.name}}
6+
ui-dropdown(label='Country', v-model='country', placeholder='Please enter country', :search="true")
7+
ui-dropdown-item(v-for='c in countries', :value='c.id') {{c.name}}
88
ui-numeric(label='Age', v-model='age', :integer='true')
99
ui-date(label='Date of birth', v-model='dob')
1010
ui-currency(label='Salary', v-model='salary')

dist/vue-typed-ui.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,8 @@ var Dropdown = function (_DropdownBase2) {
14591459
}, {
14601460
key: 'mounted',
14611461
value: function mounted() {
1462+
var _this2 = this;
1463+
14621464
var self = this;
14631465
var el = this.$el.querySelector('.ui.dropdown');
14641466
// search ?
@@ -1491,6 +1493,21 @@ var Dropdown = function (_DropdownBase2) {
14911493
self.selectedItems.splice(self.selectedItems.indexOf(val), 1);
14921494
}
14931495
});
1496+
// manual force selection
1497+
$(this.$el).find('input.search').on('blur', function (e) {
1498+
var text = $(e.target).val();
1499+
if (text) {
1500+
text = text.trim().toUpperCase();
1501+
var val = _this2.sui('get value');
1502+
$(_this2.$el).find('.menu').children('.item').each(function (i, el) {
1503+
if (el.textContent.trim().toUpperCase().startsWith(text)) {
1504+
val = $(el).data('value');
1505+
}
1506+
});
1507+
_this2.sui('set selected', val);
1508+
$(e.target).val('');
1509+
}
1510+
});
14941511
// make up data validate attr
14951512
Util.setDataValidateAttr(this, $(this.$el).find('input'));
14961513
// assign selected values

src/components/form-inputs/dropdown/index.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class Dropdown extends _DropdownBase {
1515

1616
if (this.css)
1717
css += ' ' + this.css
18-
18+
1919
return ch('div', { 'class': css }, [
2020
ch('input', { attrs: { type: 'hidden', name: this.name } }),
2121
ch('i', { 'class': 'dropdown icon' }),
@@ -66,6 +66,24 @@ export class Dropdown extends _DropdownBase {
6666
});
6767

6868

69+
// manual force selection
70+
$(this.$el).find('input.search').on('blur', (e) => {
71+
let text = $(e.target).val()
72+
if (text) {
73+
text = text.trim().toUpperCase()
74+
let val = this.sui('get value')
75+
76+
$(this.$el).find('.menu').children('.item').each(function (i, el) {
77+
if (el.textContent.trim().toUpperCase().startsWith(text)) {
78+
val = $(el).data('value')
79+
}
80+
})
81+
82+
this.sui('set selected', val)
83+
$(e.target).val('')
84+
}
85+
})
86+
6987
// make up data validate attr
7088
Util.setDataValidateAttr(this, $(this.$el).find('input'))
7189

@@ -81,7 +99,7 @@ export class Dropdown extends _DropdownBase {
8199
el.addClass('disabled')
82100
else
83101
el.removeClass('disabled')
84-
102+
85103
this.sui('refresh')
86104
}
87105

0 commit comments

Comments
 (0)