update Select component

update Select component:filterable
This commit is contained in:
梁灏 2016-10-24 14:22:33 +08:00
parent e4ebd30438
commit e4ce99177d
4 changed files with 81 additions and 31 deletions

View file

@ -18,8 +18,7 @@
}, },
data () { data () {
return { return {
prefixCls: prefixCls, prefixCls: prefixCls
hidden: false // for search
} }
} }
} }

View file

@ -12,17 +12,21 @@
<span :class="[`${prefixCls}-selected-value`]" v-show="!showPlaceholder && !multiple && !filterable">{{ selectedSingle }}</span> <span :class="[`${prefixCls}-selected-value`]" v-show="!showPlaceholder && !multiple && !filterable">{{ selectedSingle }}</span>
<input <input
type="text" type="text"
:class="[`${prefixCls}-input`]"
v-if="filterable" v-if="filterable"
v-model="query" v-model="query"
:placeholder="placeholder" :class="[`${prefixCls}-input`]"
:placeholder="showPlaceholder ? placeholder : ''"
:style="inputStyle" :style="inputStyle"
@blur="handleBlur"> @blur="handleBlur"
@keydown="resetInputState"
@keydown.delete="handleInputDelete"
v-el:input>
<Icon type="ios-close" :class="[`${prefixCls}-arrow`]" v-show="showCloseIcon" @click.stop="clearSingleSelect"></Icon> <Icon type="ios-close" :class="[`${prefixCls}-arrow`]" v-show="showCloseIcon" @click.stop="clearSingleSelect"></Icon>
<Icon type="arrow-down-b" :class="[`${prefixCls}-arrow`]"></Icon> <Icon type="arrow-down-b" :class="[`${prefixCls}-arrow`]"></Icon>
</div> </div>
<Dropdown v-show="visible" transition="slide-up" v-ref:dropdown> <Dropdown v-show="visible" transition="slide-up" v-ref:dropdown>
<ul :class="[`${prefixCls}-dropdown-list`]"><slot></slot></ul> <ul v-show="notFound" :class="[`${prefixCls}-not-found`]"><li>未找到</li></ul>
<ul v-else :class="[`${prefixCls}-dropdown-list`]"><slot></slot></ul>
</Dropdown> </Dropdown>
</div> </div>
</template> </template>
@ -85,7 +89,8 @@
selectedMultiple: [], selectedMultiple: [],
focusIndex: 0, focusIndex: 0,
query: '', query: '',
inputLength: 20 inputLength: 20,
notFound: false
} }
}, },
computed: { computed: {
@ -124,8 +129,12 @@
let style = {}; let style = {};
if (this.multiple) { if (this.multiple) {
if (this.showPlaceholder) {
style.width = '100%';
} else {
style.width = `${this.inputLength}px`; style.width = `${this.inputLength}px`;
} }
}
return style; return style;
} }
@ -245,6 +254,12 @@
return false; return false;
} }
this.model.splice(index, 1); this.model.splice(index, 1);
if (this.filterable && this.visible) {
this.$els.input.focus();
}
this.$broadcast('on-update-popper');
}, },
// to select option for single // to select option for single
toggleSingleSelected (value, init = false) { toggleSingleSelected (value, init = false) {
@ -404,6 +419,14 @@
} }
} }
}, 300); }, 300);
},
resetInputState () {
this.inputLength = this.$els.input.value.length * 12 + 20;
},
handleInputDelete () {
if (this.multiple && this.model.length && this.query === '') {
this.removeTag(this.model.length - 1);
}
} }
}, },
ready () { ready () {
@ -423,6 +446,9 @@
}, },
visible (val) { visible (val) {
if (val) { if (val) {
if (this.multiple && this.filterable) {
this.$els.input.focus();
}
this.$broadcast('on-update-popper'); this.$broadcast('on-update-popper');
} else { } else {
@ -430,6 +456,16 @@
}, },
query (val) { query (val) {
this.$broadcast('on-query-change', val); this.$broadcast('on-query-change', val);
let is_hidden = true;
this.$nextTick(() => {
this.findChild((child) => {
if (!child.hidden) {
is_hidden = false;
}
});
this.notFound = is_hidden;
});
} }
}, },
events: { events: {
@ -443,6 +479,12 @@
this.removeTag(index); this.removeTag(index);
} else { } else {
this.model.push(value); this.model.push(value);
this.$broadcast('on-update-popper');
}
if (this.filterable) {
this.query = '';
this.$els.input.focus();
} }
} else { } else {
this.model = value; this.model = value;

View file

@ -4,7 +4,7 @@
{{ city | json }}<br> {{ city | json }}<br>
<Button @click="city = 'hangzhou'">切换城市</Button> <Button @click="city = 'hangzhou'">切换城市</Button>
<br> <br>
<i-select v-if="true" :model.sync="city" style="width:200px" @on-change="change"> <i-select v-if="true" :model.sync="city" style="width:200px" filterable @on-change="change">
<i-option-group label="热门城市"> <i-option-group label="热门城市">
<i-option value="beijing">北京市</i-option> <i-option value="beijing">北京市</i-option>
<i-option value="shanghai" disabled label="上海市">上海市2</i-option> <i-option value="shanghai" disabled label="上海市">上海市2</i-option>
@ -13,31 +13,16 @@
<i-option-group label="二线城市"> <i-option-group label="二线城市">
<i-option value="nanjing">南京市</i-option> <i-option value="nanjing">南京市</i-option>
<i-option value="hangzhou">杭州市</i-option> <i-option value="hangzhou">杭州市</i-option>
</i-option-group>
<i-option value="heilongjiang" disabled>黑龙江市</i-option> <i-option value="heilongjiang" disabled>黑龙江市</i-option>
<i-option-group label="热门城市">
<i-option value="beijing">北京市</i-option>
<i-option value="shanghai" disabled label="上海市">上海市2</i-option>
<i-option value="shenzhen">深圳市</i-option>
</i-option-group> </i-option-group>
<i-option-group label="二线城市"> <i-option-group label="其它城市">
<i-option value="nanjing">南京市</i-option> <i-option value="jyg">嘉峪关市</i-option>
<i-option value="hangzhou">杭州市</i-option> <i-option value="lanzhou">兰州市</i-option>
<i-option value="beijingxi">北京西</i-option>
</i-option-group> </i-option-group>
<i-option value="heilongjiang" disabled>黑龙江市</i-option>
<i-option-group label="热门城市">
<i-option value="beijing">北京市</i-option>
<i-option value="shanghai" disabled label="上海市">上海市2</i-option>
<i-option value="shenzhen">深圳市</i-option>
</i-option-group>
<i-option-group label="二线城市">
<i-option value="nanjing">南京市</i-option>
<i-option value="hangzhou">杭州市</i-option>
</i-option-group>
<i-option value="heilongjiang" disabled>黑龙江市</i-option>
</i-select> </i-select>
<i-select :model.sync="focus" style="width:200px" @on-change="change" clearable filterable label-in-value> <i-select v-show="true" :model.sync="focus" style="width:200px" @on-change="change" clearable filterable label-in-value>
<i-option value="beijing">北京</i-option> <i-option value="beijing">北京</i-option>
<i-option value="shanghai" label="上海市">上海市</i-option> <i-option value="shanghai" label="上海市">上海市</i-option>
<i-option value="shenzhen" disabled>深圳市</i-option> <i-option value="shenzhen" disabled>深圳市</i-option>
@ -51,6 +36,19 @@
<i-option value="e">e市</i-option> <i-option value="e">e市</i-option>
</i-select> </i-select>
<i-select v-if="true" :model.sync="focus2" style="width:300px" @on-change="change" clearable filterable multiple>
<i-option value="beijing" label="北京市">北京2</i-option>
<i-option value="shanghai">上海市</i-option>
<i-option value="shenzhen" disabled>深圳市</i-option>
<i-option value="guangzhou">广州市</i-option>
<i-option value="shijiazhuang">石家庄市</i-option>
<i-option value="a">a1市</i-option>
<i-option value="b">b2市</i-option>
<i-option value="c">c1市</i-option>
<i-option value="d">d2市</i-option>
<i-option value="e">e1市</i-option>
</i-select>
<i-select v-if="true" :model.sync="focus2" style="width:300px" @on-change="change" clearable multiple> <i-select v-if="true" :model.sync="focus2" style="width:300px" @on-change="change" clearable multiple>
<i-option value="beijing" label="北京市">北京2</i-option> <i-option value="beijing" label="北京市">北京2</i-option>
<i-option value="shanghai">上海市</i-option> <i-option value="shanghai">上海市</i-option>
@ -84,7 +82,7 @@
return { return {
city: '', city: '',
focus: '', focus: '',
focus2: ['beijing', 'guangzhou', 'b'] focus2: ['beijing']
// focus2: [] // focus2: []
} }
}, },

View file

@ -163,6 +163,17 @@
&-small &-input{ &-small &-input{
height: @input-height-small; height: @input-height-small;
} }
&-multiple &-input{
height: 25px;
line-height: 28px;
padding: 0 0 0 6px;
}
&-not-found{
text-align: center;
color: @btn-disable-color;
}
} }
.@{select-item-prefix-cls} { .@{select-item-prefix-cls} {