app-radio-group.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <view class="app-radio-group">
  3. <view class="dir-left-wrap">
  4. <template v-for="(item, index) in model">
  5. <view :key="index" class="item cross-center dir-left-nowrap" :style="{height: `${height}rpx`,}">
  6. <template v-if="type==='round'">
  7. <view class="item-round"
  8. :class="[
  9. `white-background`,
  10. item.value?`${theme}-background`:``,
  11. `${theme}-border`,
  12. `${theme}-color`,
  13. ]" @click="handleClick(index)">{{item.label}}
  14. </view>
  15. </template>
  16. <template v-else>
  17. <view class="checker">
  18. <app-radio v-model="item.value" type="round" @input="handleInput" :sign="index"></app-radio>
  19. </view>
  20. <view class="label">{{item.label}}</view>
  21. </template>
  22. </view>
  23. </template>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. import AppRadio from "./app-radio";
  29. import {mapState} from 'vuex';
  30. export default {
  31. name: 'app-radio-group',
  32. components: {AppRadio},
  33. props: {
  34. type: {
  35. default: 'default',
  36. },
  37. value: {
  38. default: null,
  39. },
  40. list: {
  41. type: Array,
  42. default: [],
  43. },
  44. height: {
  45. type: Number,
  46. default: 88,
  47. },
  48. sign: {
  49. default: null,
  50. },
  51. },
  52. data() {
  53. const list = this.list;
  54. for (let i in list) {
  55. if (list[i].label === this.value) {
  56. list[i].value = true;
  57. }
  58. }
  59. return {
  60. model: this.list,
  61. };
  62. },
  63. computed: {
  64. ...mapState({
  65. theme: state => state.mallConfig.theme,
  66. }),
  67. },
  68. methods: {
  69. handleInput(e, index) {
  70. if (e === false) {
  71. this.model[index].value = true;
  72. return;
  73. }
  74. for (let i in this.model) {
  75. if (index != i) {
  76. this.model[i].value = false;
  77. }
  78. }
  79. this.$emit('input', this.model[index].label, this.sign);
  80. this.$emit('change', this.model, this.sign);
  81. },
  82. handleClick(index) {
  83. for (let i in this.model) {
  84. if (i == index) {
  85. if (!this.model[i].value) {
  86. this.model[i].value = true;
  87. this.$emit('input', this.model[index].label, this.sign);
  88. this.$emit('change', this.model, this.sign);
  89. }
  90. } else {
  91. this.model[i].value = false;
  92. }
  93. }
  94. },
  95. },
  96. }
  97. </script>
  98. <style scoped lang="scss">
  99. .label {
  100. color: #666666;
  101. }
  102. .item {
  103. margin-right: #{36rpx};
  104. }
  105. .item:last-child {
  106. margin-right: 0;
  107. }
  108. .checker {
  109. margin-right: #{16rpx};
  110. }
  111. .item-round {
  112. display: inline-block;
  113. height: #{56rpx};
  114. line-height: #{54rpx};
  115. border: #{1rpx} solid;
  116. padding: 0 #{28rpx};
  117. border-radius: #{1000rpx};
  118. font-size: $uni-font-size-general-one;
  119. }
  120. </style>