ly-radio-group.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <view :style="[getStyle]"
  3. class="ly-radio-group"
  4. >
  5. <view v-for="(item,index) in list" :key="index" :class="{'action':item[valueName] == value}"
  6. class="ly-radio-group-item" @click="changeHandle(item)">
  7. {{ item[labelName] }}
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. props: {
  14. list: {
  15. type: [Object, Array],
  16. default: []
  17. },
  18. labelName: {
  19. type: String,
  20. default: 'label'
  21. },
  22. valueName: {
  23. type: String,
  24. default: 'value'
  25. },
  26. value: {
  27. type: [String, Number],
  28. default: '',
  29. },
  30. change: {
  31. type: Function
  32. },
  33. height: {
  34. type: [Number, String],
  35. default: 70,
  36. },
  37. size: {
  38. type: [Number, String],
  39. default: 26,
  40. },
  41. color: {
  42. type: String,
  43. default: '#303133',
  44. },
  45. bgColor: {
  46. type: String,
  47. default: 'transparent',
  48. },
  49. bColor: {
  50. type: String,
  51. default: '#dcdfe6',
  52. },
  53. actionColor: {
  54. type: String,
  55. default: '#409eff',
  56. }
  57. },
  58. model: {
  59. prop: 'value',
  60. //这个事件名可以随意写,它实际上是规定了子组件要更新父组件值需要注册的方法
  61. event: 'input'
  62. },
  63. data() {
  64. return {}
  65. },
  66. computed: {
  67. getStyle() {
  68. let data = {
  69. '--color': this.color,
  70. '--bg-color': this.bgColor,
  71. '--b-color': this.bColor,
  72. '--action-color': this.actionColor,
  73. }
  74. return data
  75. },
  76. },
  77. methods: {
  78. changeHandle(item) {
  79. this.$emit('input', item[this.valueName])
  80. this.$emit('change', item)
  81. },
  82. }
  83. }
  84. </script>
  85. <style lang="scss" scoped>
  86. .ly-radio-group {
  87. display: inline-flex;
  88. width: 100%;
  89. &-item {
  90. border: 2rpx solid var(--b-color);
  91. border-left: 0;
  92. height: 70rpx;
  93. font-size: 26rpx;
  94. //padding: 0 20rpx;
  95. display: flex;
  96. align-items: center;
  97. justify-content: center;
  98. color: var(--color);
  99. background: var(--bg-color);
  100. box-sizing: content-box;
  101. flex: 1;
  102. &:first-child {
  103. border-left: 2rpx solid var(--b-color);
  104. border-top-left-radius: 10rpx;
  105. border-bottom-left-radius: 10rpx;
  106. }
  107. &:last-child {
  108. border-top-right-radius: 10rpx;
  109. border-bottom-right-radius: 10rpx;
  110. }
  111. &.action {
  112. border-color: var(--action-color);
  113. background: var(--action-color);
  114. color: #FFFFFF;
  115. }
  116. }
  117. }
  118. </style>