Blame view

src/components/select/option.vue 2.84 KB
e355dd49   梁灏   add Select Component
1
  <template>
e4ebd304   梁灏   update Select com...
2
      <li :class="classes" @click.stop="select" @mouseout.stop="blur" v-show="!hidden"><slot>{{ showLabel }}</slot></li>
e355dd49   梁灏   add Select Component
3
4
  </template>
  <script>
4aec6a66   梁灏   support Select
5
      import Emitter from '../../mixins/emitter';
fed3e09d   梁灏   add AutoComplete ...
6
      import { findComponentUpward } from '../../utils/assist';
4aec6a66   梁灏   support Select
7
  
e355dd49   梁灏   add Select Component
8
9
10
      const prefixCls = 'ivu-select-item';
  
      export default {
4aec6a66   梁灏   support Select
11
12
13
          name: 'iOption',
          componentName: 'select-item',
          mixins: [ Emitter ],
e355dd49   梁灏   add Select Component
14
15
16
17
18
19
20
21
22
23
24
25
26
          props: {
              value: {
                  type: [String, Number],
                  required: true
              },
              label: {
                  type: [String, Number]
              },
              disabled: {
                  type: Boolean,
                  default: false
              }
          },
e355dd49   梁灏   add Select Component
27
28
29
30
          data () {
              return {
                  selected: false,
                  index: 0,    // for up and down to focus
e4ebd304   梁灏   update Select com...
31
32
                  isFocus: false,
                  hidden: false,    // for search
fed3e09d   梁灏   add AutoComplete ...
33
34
                  searchLabel: '',    // the value is slot,only for search
                  autoComplete: false
b0893113   jingsam   :art: add eslint
35
              };
e355dd49   梁灏   add Select Component
36
37
38
39
40
41
42
          },
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-disabled`]: this.disabled,
fed3e09d   梁灏   add AutoComplete ...
43
                          [`${prefixCls}-selected`]: this.selected && !this.autoComplete,
e355dd49   梁灏   add Select Component
44
45
                          [`${prefixCls}-focus`]: this.isFocus
                      }
b0893113   jingsam   :art: add eslint
46
                  ];
e355dd49   梁灏   add Select Component
47
48
              },
              showLabel () {
b0893113   jingsam   :art: add eslint
49
                  return (this.label) ? this.label : this.value;
e355dd49   梁灏   add Select Component
50
51
52
53
54
55
56
57
              }
          },
          methods: {
              select () {
                  if (this.disabled) {
                      return false;
                  }
  
4aec6a66   梁灏   support Select
58
                  this.dispatch('iSelect', 'on-select-selected', this.value);
e355dd49   梁灏   add Select Component
59
60
61
              },
              blur () {
                  this.isFocus = false;
e4ebd304   梁灏   update Select com...
62
63
              },
              queryChange (val) {
d94d98c4   梁灏   fixed #215
64
65
                  const parsedQuery = val.replace(/(\^|\(|\)|\[|\]|\$|\*|\+|\.|\?|\\|\{|\}|\|)/g, '\\$1');
                  this.hidden = !new RegExp(parsedQuery, 'i').test(this.searchLabel);
e1b86bcf   梁灏   fixed #1865
66
67
68
              },
              // 在使用函数防抖后,设置 key 后,不更新组件了,导致SearchLabel 不更新 #1865
              updateSearchLabel () {
dfae43a7   Sergio Crisostomo   correct input val...
69
                  this.searchLabel = this.$el.textContent;
e355dd49   梁灏   add Select Component
70
71
              }
          },
4aec6a66   梁灏   support Select
72
          mounted () {
e1b86bcf   梁灏   fixed #1865
73
              this.updateSearchLabel();
ed91d9b0   梁灏   update Select
74
              this.dispatch('iSelect', 'append');
4aec6a66   梁灏   support Select
75
              this.$on('on-select-close', () => {
e355dd49   梁灏   add Select Component
76
                  this.isFocus = false;
4aec6a66   梁灏   support Select
77
78
              });
              this.$on('on-query-change', (val) => {
e4ebd304   梁灏   update Select com...
79
                  this.queryChange(val);
4aec6a66   梁灏   support Select
80
              });
fed3e09d   梁灏   add AutoComplete ...
81
82
83
  
              const Select = findComponentUpward(this, 'iSelect');
              if (Select) this.autoComplete = Select.autoComplete;
ed91d9b0   梁灏   update Select
84
85
86
          },
          beforeDestroy () {
              this.dispatch('iSelect', 'remove');
e355dd49   梁灏   add Select Component
87
          }
b0893113   jingsam   :art: add eslint
88
89
      };
  </script>