app-diy-form-checkbox-group.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <view class="app-diy-form-checkbox-group">
  3. <view class="list">
  4. <view v-for="(item, index) in model"
  5. :key="index"
  6. @click="handleClick(index)" class="out-of-item">
  7. <view class="item"
  8. :class="[
  9. `white-background`,
  10. item.value?`${theme}-background`:``,
  11. `${theme}-border`,
  12. `${theme}-color`,
  13. ]">{{item.label}}
  14. </view>
  15. </view>
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. import {mapState} from 'vuex';
  21. export default {
  22. name: 'app-diy-form-checkbox-group',
  23. props: {
  24. sign: {
  25. default: null,
  26. },
  27. value: {
  28. type: Array,
  29. default: [],
  30. },
  31. list: {
  32. type: Array,
  33. default: [],
  34. }
  35. },
  36. data() {
  37. const model = this.list;
  38. for (let i in model) {
  39. let inArray = false;
  40. for (let j in this.value) {
  41. if (model[i].label === this.value[j]) {
  42. inArray = true;
  43. break;
  44. }
  45. }
  46. if (inArray) {
  47. model[i].value = true;
  48. }
  49. }
  50. return {
  51. model: model,
  52. };
  53. },
  54. computed: {
  55. ...mapState({
  56. theme: state => state.mallConfig.theme,
  57. }),
  58. },
  59. methods: {
  60. handleClick(index) {
  61. this.model[index].value = !this.model[index].value;
  62. this.outputData();
  63. },
  64. outputData() {
  65. const value = [];
  66. for (let i in this.model) {
  67. if (this.model[i].value === true) {
  68. value.push(this.model[i].label);
  69. }
  70. }
  71. this.$emit('change', value, this.sign);
  72. this.$emit('input', value, this.sign);
  73. },
  74. },
  75. }
  76. </script>
  77. <style scoped lang="scss">
  78. .app-diy-form-checkbox-group {
  79. width: 100%;
  80. overflow-x: hidden;
  81. }
  82. .list {
  83. margin: 0 -#{6rpx};
  84. }
  85. .out-of-item {
  86. padding: #{8rpx};
  87. display: inline-block;
  88. }
  89. .item {
  90. display: inline-block;
  91. height: #{56rpx};
  92. line-height: #{54rpx};
  93. border: #{1rpx} solid;
  94. padding: 0 #{28rpx};
  95. border-radius: #{1000rpx};
  96. font-size: $uni-font-size-general-one;
  97. }
  98. </style>