u-select.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <view class="u-select">
  3. <view class="u-select__content">
  4. <view class="u-select__label" @click="openSelect">
  5. <slot name="text">
  6. <text class="u-select__text">
  7. {{ label }}
  8. </text>
  9. </slot>
  10. <slot name="icon">
  11. <u-icon name="arrow-down" :size="iconSize" :color="iconColor"></u-icon>
  12. </slot>
  13. </view>
  14. <view class="u-select__options"
  15. :style="{zIndex: zIndex}" v-if="isOpen">
  16. <slot name="options">
  17. <view class="u-select__options_item"
  18. :class="current == item[keyName] ? 'active': ''"
  19. :key="index" v-for="(item, index) in options"
  20. @click="selectItem(item)">
  21. <slot name="optionItem" :item="item">
  22. <text class="u-select__item_text" :style="{color: itemColor}">
  23. {{item.name}}
  24. </text>
  25. </slot>
  26. </view>
  27. </slot>
  28. </view>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. export default {
  34. name:"up-select",
  35. emits: ['update:current', 'select'],
  36. props: {
  37. label: {
  38. type: String,
  39. default: '选项'
  40. },
  41. options: {
  42. type: Array,
  43. default: () => {
  44. return []
  45. }
  46. },
  47. keyName: {
  48. type: String,
  49. default: 'id'
  50. },
  51. current: {
  52. type: [String, Number],
  53. default: ''
  54. },
  55. zIndex: {
  56. type: Number,
  57. default: 10
  58. },
  59. itemColor: {
  60. type: String,
  61. default: '#333333'
  62. },
  63. iconColor: {
  64. type: String,
  65. default: ''
  66. },
  67. iconSize: {
  68. type: [String],
  69. default: '13px'
  70. }
  71. } ,
  72. data() {
  73. return {
  74. isOpen: false
  75. }
  76. },
  77. mounted() {
  78. },
  79. methods: {
  80. openSelect() {
  81. this.isOpen = !this.isOpen
  82. },
  83. selectItem(item) {
  84. this.isOpen = false
  85. this.$emit('update:current', item[this.keyName])
  86. this.$emit('select', item)
  87. }
  88. }
  89. }
  90. </script>
  91. <style lang="scss" scoped>
  92. .u-select__content {
  93. position: relative;
  94. .u-select__label {
  95. display: flex;
  96. /* #ifdef H5 */
  97. &:hover {
  98. cursor: pointer;
  99. }
  100. /* #endif */
  101. }
  102. .u-select__text {
  103. margin-right: 2px;
  104. }
  105. .u-select__options {
  106. min-width: 100px;
  107. box-sizing: border-box;
  108. border-radius: 4px;
  109. border: 1px solid #f1f1f1;
  110. background-color: #fff;
  111. position: absolute;
  112. top: 20px;
  113. left: 0;
  114. .u-select__options_item {
  115. padding: 10px 12px;
  116. box-sizing: border-box;
  117. width: 100%;
  118. height: 100%;
  119. &:hover {
  120. background-color: #f7f7f7;
  121. }
  122. /* #ifdef H5 */
  123. &:hover {
  124. cursor: pointer;
  125. }
  126. .u-select__item_text {
  127. &:hover {
  128. cursor: pointer;
  129. }
  130. }
  131. /* #endif */
  132. }
  133. }
  134. }
  135. </style>