fs-select.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <fs-popover
  3. v-model="selectedAction"
  4. :placement="placement"
  5. :actions="actions"
  6. :width="width"
  7. :actionWidth="actionWidth"
  8. :valueKey="valueKey"
  9. @clickAction="handleClickAction"
  10. @visibleChange="handleChange"
  11. >
  12. <template #refer>
  13. <view class="fs-select" :class="[{ 'fs-select-border': border }]">
  14. <view class="fs-select-content line1">{{ selectedAction.text || placeholder }}</view>
  15. <view class="fs-select-icon"><fs-icon type="icon-d-down" :rotate="state.rotate" size="30rpx"></fs-icon></view>
  16. </view>
  17. </template>
  18. <!-- <slot></slot> -->
  19. </fs-popover>
  20. </template>
  21. <script>
  22. /**
  23. * 选择器组件
  24. * @description 选择器组件
  25. * @property {Array} actions 选项列表
  26. * @property {String} valueKey 作为 value 唯一标识的键名
  27. * @property {String} placement = [top | top-start | top-end | bottom | bottom-start | bottom-end,left | left-start | left-end,right | right-start | right-end] 弹出位置
  28. * @property {String} placeholder 占位符
  29. * @property {String} width select宽度
  30. * @property {String} actionWidth 选项列表宽度
  31. * @property {Boolean} border 显示边框
  32. * @event {Function} change 选项变化事件
  33. */
  34. export default {
  35. name: 'fs-select'
  36. }
  37. </script>
  38. <script setup>
  39. import { ref, reactive, computed } from 'vue'
  40. const props = defineProps({
  41. modelValue: [Object],
  42. actions: Array,
  43. border: Boolean,
  44. placeholder: String,
  45. width: {
  46. type: String,
  47. default: '100%'
  48. },
  49. actionWidth: {
  50. type: String,
  51. default: 'auto'
  52. },
  53. placement: {
  54. type: String,
  55. default: 'bottom',
  56. validator(value) {
  57. return [
  58. 'top',
  59. 'top-start',
  60. 'top-end',
  61. 'bottom',
  62. 'bottom-start',
  63. 'bottom-end',
  64. 'left',
  65. 'left-start',
  66. 'left-end',
  67. 'right',
  68. 'right-start',
  69. 'right-end'
  70. ].includes(value)
  71. }
  72. },
  73. valueKey: {
  74. type: String,
  75. default: 'id'
  76. }
  77. })
  78. const emits = defineEmits(['change', 'update:modelValue'])
  79. const selectedAction = computed({
  80. get: () => props.modelValue,
  81. set: value => {
  82. emits('update:modelValue', value)
  83. }
  84. })
  85. const state = reactive({
  86. rotate: '0'
  87. })
  88. const handleClickAction = item => {
  89. emits('update:modelValue', item)
  90. emits('change', item)
  91. }
  92. const handleChange = () => {
  93. state.rotate = state.rotate === '0' ? '180' : '0'
  94. }
  95. </script>
  96. <style lang="scss" scoped>
  97. .fs-select {
  98. display: flex;
  99. height: 70rpx;
  100. align-items: center;
  101. justify-content: space-between;
  102. position: relative;
  103. background-color: inherit;
  104. position: relative;
  105. padding-right: 20rpx;
  106. max-width: 100%;
  107. &-border {
  108. border: 2rpx solid var(--border-color) !important;
  109. border-radius: 4rpx !important;
  110. padding-left: 20rpx;
  111. &.fs-select-content {
  112. padding: 20rpx;
  113. }
  114. }
  115. &-content {
  116. flex: 1;
  117. width: 400rpx;
  118. }
  119. &-icon {
  120. padding-left: 20rpx;
  121. flex-shrink: 0;
  122. }
  123. .visible {
  124. transform: rotate(180deg);
  125. transform-origin: center center;
  126. }
  127. }
  128. </style>