u-choose.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <style scoped lang="scss">
  2. .up-choose {
  3. ::v-deep .up-tag {
  4. font-weight: 600;
  5. }
  6. &:last-child {
  7. margin-right: 0;
  8. }
  9. }
  10. .up-choose-wrap {
  11. flex-wrap: wrap;
  12. }
  13. .up-choose-nowrap {
  14. flex-wrap: nowrap;
  15. white-space: nowrap;
  16. }
  17. </style>
  18. <template>
  19. <scroll-view
  20. :scroll-x="wrap === false"
  21. :class="['up-choose', wrap ? 'up-choose-wrap' : 'up-choose-nowrap']">
  22. <template :key="item.id" v-for="(item,index) in options">
  23. <view :style="{width: width, display: 'inline-block'}">
  24. <slot :item="item" :index="index">
  25. <up-tag :type="index == currentIndex ? 'primary' : 'info'"
  26. size="large" :plain="index == currentIndex ? false : true"
  27. :class="currentIndex === index ? 'active': ''" :height="itemHeight"
  28. :style="{width: itemWidth, padding: itemPadding}"
  29. @click="change(index)">
  30. {{item[labelName]}}
  31. </up-tag>
  32. </slot>
  33. </view>
  34. </template>
  35. </scroll-view>
  36. </template>
  37. <script>
  38. export default {
  39. name: 'up-choose',
  40. props: {
  41. options:{
  42. type: Array,
  43. default: ()=>{
  44. return [];
  45. }
  46. },
  47. modelValue: {
  48. type: [Number,String,Array],
  49. default: false
  50. },
  51. type: {
  52. type: [String],
  53. default: 'radio'
  54. },
  55. itemWidth: {
  56. type: [String],
  57. default: 'auto'
  58. },
  59. itemHeight: {
  60. type: [String],
  61. default: '50px'
  62. },
  63. itemPadding: {
  64. type: [String],
  65. default: '8px'
  66. },
  67. labelName: {
  68. type: String,
  69. default: 'title'
  70. },
  71. valueName: {
  72. type: String,
  73. default: 'value'
  74. },
  75. customClick: {
  76. type: Boolean,
  77. default: false
  78. },
  79. // 是否换行
  80. wrap: {
  81. type: Boolean,
  82. default: true
  83. }
  84. },
  85. data() {
  86. return {
  87. currentIndex: ''
  88. }
  89. },
  90. created: function () {
  91. this.currentIndex = this.modelValue;
  92. },
  93. emits: ['update:modelValue', 'custom-click'],
  94. methods: {
  95. change(index){
  96. if (this.customClick) {
  97. this.$emit('custom-click', index);
  98. } else {
  99. this.currentIndex = index;
  100. this.$emit('update:modelValue', index);
  101. }
  102. }
  103. }
  104. }
  105. </script>