iview/src/components/select/option.vue

90 lines
2.8 KiB
Vue
Raw Normal View History

<template>
<li :class="classes" @click.stop="select" @mouseout.stop="blur" v-show="!hidden"><slot>{{ showLabel }}</slot></li>
</template>
<script>
2017-03-06 18:24:57 +08:00
import Emitter from '../../mixins/emitter';
2017-08-23 14:42:54 +08:00
import { findComponentUpward } from '../../utils/assist';
2017-03-06 18:24:57 +08:00
const prefixCls = 'ivu-select-item';
export default {
2017-03-06 18:24:57 +08:00
name: 'iOption',
componentName: 'select-item',
mixins: [ Emitter ],
props: {
value: {
type: [String, Number],
required: true
},
label: {
type: [String, Number]
},
disabled: {
type: Boolean,
default: false
}
},
data () {
return {
selected: false,
index: 0, // for up and down to focus
isFocus: false,
hidden: false, // for search
2017-08-23 14:42:54 +08:00
searchLabel: '', // the value is slot,only for search
autoComplete: false
2016-12-25 22:49:42 +08:00
};
},
computed: {
classes () {
return [
`${prefixCls}`,
{
[`${prefixCls}-disabled`]: this.disabled,
2017-08-23 14:42:54 +08:00
[`${prefixCls}-selected`]: this.selected && !this.autoComplete,
[`${prefixCls}-focus`]: this.isFocus
}
2016-12-25 22:49:42 +08:00
];
},
showLabel () {
2016-12-25 22:49:42 +08:00
return (this.label) ? this.label : this.value;
}
},
methods: {
select () {
if (this.disabled) {
return false;
}
2017-03-06 18:24:57 +08:00
this.dispatch('iSelect', 'on-select-selected', this.value);
},
blur () {
this.isFocus = false;
},
queryChange (val) {
2017-01-17 12:13:18 +08:00
const parsedQuery = val.replace(/(\^|\(|\)|\[|\]|\$|\*|\+|\.|\?|\\|\{|\}|\|)/g, '\\$1');
this.hidden = !new RegExp(parsedQuery, 'i').test(this.searchLabel);
2017-09-14 15:49:37 +08:00
},
// 在使用函数防抖后,设置 key 后不更新组件了导致SearchLabel 不更新 #1865
updateSearchLabel () {
2017-10-25 20:49:38 +02:00
this.searchLabel = this.$el.textContent;
}
},
2017-03-06 18:24:57 +08:00
mounted () {
2017-09-14 15:49:37 +08:00
this.updateSearchLabel();
2017-03-30 11:52:36 +08:00
this.dispatch('iSelect', 'append');
2017-03-06 18:24:57 +08:00
this.$on('on-select-close', () => {
this.isFocus = false;
2017-03-06 18:24:57 +08:00
});
this.$on('on-query-change', (val) => {
this.queryChange(val);
2017-03-06 18:24:57 +08:00
});
2017-08-23 14:42:54 +08:00
const Select = findComponentUpward(this, 'iSelect');
if (Select) this.autoComplete = Select.autoComplete;
2017-03-30 11:52:36 +08:00
},
beforeDestroy () {
this.dispatch('iSelect', 'remove');
}
2016-12-25 22:49:42 +08:00
};
</script>