select.vue 4.5 KB
Newer Older
A
Amy 已提交
1 2 3
<template>
  <div class="cube-select" :class="{ 'cube-select_active': active, 'cube-select_disabled': disabled }" @click="showPicker">
    <span v-if="selectedText" class="cube-select-text">{{ selectedText }}</span>
J
JiZhi 已提交
4
    <span v-else class="cube-select-placeholder">{{ _placeholder }}</span>
5
    <i class="cube-select-icon"></i>
A
Amy 已提交
6 7 8 9
  </div>
</template>

<script>
D
dolymood 已提交
10
  import { findIndex } from '../../common/helpers/util'
J
JiZhi 已提交
11 12
  import localeMixin from '../../common/mixins/locale'

A
Amy 已提交
13 14 15
  const COMPONENT_NAME = 'cube-select'

  const EVENT_CHANGE = 'change'
J
Jiaxun Li 已提交
16
  const EVENT_INPUT = 'input' // only used for v-model
17 18
  const EVENT_PICKER_SHOW = 'picker-show'
  const EVENT_PICKER_HIDE = 'picker-hide'
A
Amy 已提交
19 20 21

  export default {
    name: COMPONENT_NAME,
J
JiZhi 已提交
22
    mixins: [ localeMixin ],
A
Amy 已提交
23 24 25 26 27 28 29 30 31
    data() {
      return {
        active: false
      }
    },
    props: {
      options: {
        type: Array,
        default() {
D
doly mood 已提交
32
          /* istanbul ignore next */
A
Amy 已提交
33 34 35 36 37 38
          return []
        }
      },
      value: null,
      placeholder: {
        type: String,
J
JiZhi 已提交
39
        default: ''
A
Amy 已提交
40 41 42 43 44 45 46 47 48 49 50
      },
      autoPop: {
        type: Boolean,
        default: false
      },
      disabled: {
        type: Boolean,
        default: false
      },
      title: {
        type: String,
J
JiZhi 已提交
51
        default: ''
A
Amy 已提交
52 53 54
      },
      cancelTxt: {
        type: String,
J
JiZhi 已提交
55
        default: ''
A
Amy 已提交
56 57 58
      },
      confirmTxt: {
        type: String,
J
JiZhi 已提交
59
        default: ''
A
Amy 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73
      }
    },
    computed: {
      adaptOptions() {
        return [this.options.map(item => {
          if (typeof item !== 'object') {
            item = {
              value: item,
              text: item
            }
          }
          return item
        })]
      },
D
dolymood 已提交
74
      valueIndex() {
D
dolymood 已提交
75
        const val = this.value
D
dolymood 已提交
76
        const index = findIndex(this.adaptOptions[0], (item) => {
D
dolymood 已提交
77
          return item.value === val
A
Amy 已提交
78
        })
D
dolymood 已提交
79
        this.picker && this.picker.setData(this.adaptOptions, index !== -1 ? [index] : [0])
A
Amy 已提交
80

D
dolymood 已提交
81
        return index
A
Amy 已提交
82
      },
D
dolymood 已提交
83 84 85
      selectedIndex() {
        return this.valueIndex !== -1 ? [this.valueIndex] : [0]
      },
A
Amy 已提交
86
      selectedText() {
D
dolymood 已提交
87
        return this.valueIndex !== -1 ? this.adaptOptions[0][this.valueIndex].text : ''
J
JiZhi 已提交
88 89 90 91 92 93 94 95 96 97 98 99
      },
      _placeholder () {
        return this.placeholder || this.$t('selectText')
      },
      _title () {
        return this.title || this.$t('selectText')
      },
      _cancelTxt () {
        return this.cancelTxt || this.$t('cancel')
      },
      _confirmTxt () {
        return this.confirmTxt || this.$t('ok')
A
Amy 已提交
100 101 102 103
      }
    },
    created() {
      this.picker = this.$createPicker({
D
dolymood 已提交
104
        $props: {
J
JiZhi 已提交
105
          title: '_title',
D
dolymood 已提交
106 107
          data: 'adaptOptions',
          selectedIndex: 'selectedIndex',
J
JiZhi 已提交
108 109
          cancelTxt: '_cancelTxt',
          confirmTxt: '_confirmTxt'
D
dolymood 已提交
110 111 112 113 114
        },
        $events: {
          select: 'selectHandler',
          cancel: this.hided
        }
A
Amy 已提交
115 116 117 118 119 120 121 122 123 124
      })
      this.autoPop && this.showPicker()
    },
    methods: {
      showPicker() {
        if (this.disabled) {
          return
        }
        this.picker.show()
        this.active = true
125
        this.$emit(EVENT_PICKER_SHOW)
A
Amy 已提交
126 127 128
      },
      hided() {
        this.active = false
129
        this.$emit(EVENT_PICKER_HIDE)
A
Amy 已提交
130
      },
D
dolymood 已提交
131 132
      selectHandler(selectedVal, selectedIndex, selectedText) {
        this.hided()
A
Amy 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        if (selectedVal[0] !== this.value) {
          this.$emit(EVENT_INPUT, selectedVal[0])
          this.$emit(EVENT_CHANGE, selectedVal[0], selectedIndex[0], selectedText[0])
        }
      }
    }
  }
</script>
<style lang="stylus">
  @require "../../common/stylus/variable.styl"
  @require "../../common/stylus/mixin.styl"

  .cube-select
    position: relative
    box-sizing: border-box
D
doly mood 已提交
148
    padding: 10px 20px 10px 10px
A
Amy 已提交
149
    border-radius: 2px
150 151
    font-size: $fontsize-medium
    line-height: 1.429
A
Amy 已提交
152 153 154 155 156
    color: $select-color
    background-color: $select-bgc
    border-1px($select-border-color, 2px)
    > span
      display: inline-block
157 158
  .cube-select_active
    border-1px($select-border-active-color)
A
Amy 已提交
159
    .cube-select-icon
160 161 162 163 164 165 166 167 168
      transform: translate(0, -50%) rotate(180deg)
  .cube-select_disabled
    color: $select-disabled-color
    background-color: $select-disabled-bgc
    cursor: not-allowed
  .cube-select-placeholder
    color: $select-placeholder-color
  .cube-select-icon
    position: absolute
D
doly mood 已提交
169
    right: 8px
170 171 172 173 174 175
    top: 50%
    transform: translate(0, -50%)
    border-style: solid
    border-color: $select-icon-color transparent transparent transparent
    border-width: 4px 4px 0 4px
    transition: transform .3s ease-in-out
A
Amy 已提交
176
</style>