en-radio.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <view class="box">
  3. <view class="input-box">
  4. <view class="input-box-left" :style="{'fontSize':textSize + 'rpx'}">
  5. {{ label }}
  6. </view>
  7. <view class="input-box-right">
  8. <radio-group class="radio-data" @change="radioChange">
  9. <label class="radio-item" v-for="(radioItem,index) in radioArr">
  10. <radio :id="name+index" :value="radioItem.id" />
  11. <text class="iconfont icon-ok" v-if="inputValue===radioItem.id">&#xe608;</text>
  12. <text class="iconfont" v-else> &#xead9;</text>
  13. <text :style="{'fontSize':textSize + 'rpx'}">{{radioItem.name}}</text>
  14. </label>
  15. </radio-group>
  16. </view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. name: 'en-radio',
  23. props: {
  24. textSize:{
  25. type: Number,
  26. default: 32
  27. },
  28. type: {
  29. type: String,
  30. default: 'text'
  31. },
  32. label: {
  33. type: String,
  34. default: '标题'
  35. },
  36. radioData: {
  37. type: Array,
  38. default: []
  39. },
  40. radioKey:{
  41. type:String,
  42. default:'id'
  43. },
  44. radioValue:{
  45. type:String,
  46. default:'name'
  47. },
  48. disabled: {
  49. default: false
  50. },
  51. name: {
  52. type: String,
  53. default: 'text'
  54. },
  55. value: {
  56. default: '1'
  57. }
  58. },
  59. data() {
  60. return {
  61. inputValue: [],
  62. radioArr:[],
  63. labelWidth:0
  64. }
  65. },
  66. components: {},
  67. mounted() {
  68. this.setValue()
  69. this.setRadioData()
  70. this.setLabelWidth()
  71. },
  72. watch: {
  73. 'value': function () {
  74. if (this.inputValue !== this.value) {
  75. this.setValue()
  76. }
  77. },
  78. 'inputValue': function () {
  79. this.$emit('input', this.inputValue)
  80. }
  81. },
  82. methods: {
  83. radioChange(e){
  84. this.inputValue=e.detail.value
  85. console.log(this.inputValue)
  86. },
  87. setRadioData(){
  88. if(this.radioData.length<=0){
  89. return ;
  90. }
  91. this.radioData.forEach((item)=>{
  92. if(item[this.radioKey] && item[this.radioValue]){
  93. this.radioArr.push({
  94. 'id':item[this.radioKey]+'',
  95. 'name':item[this.radioValue]
  96. })
  97. }
  98. })
  99. },
  100. setValue(){
  101. this.inputValue = this.value+''
  102. },
  103. setLabelWidth(){
  104. let differenceNum=4- this.label.length;
  105. if(differenceNum===2){
  106. this.labelWidth='2em'
  107. }else if(differenceNum===1){
  108. this.labelWidth='0.5em'
  109. }
  110. }
  111. }
  112. }
  113. </script>
  114. <style lang="scss" scoped>
  115. @import url("../../static/css/en-common.css");
  116. .box{
  117. .input-box {
  118. display: flex;
  119. align-items: center;
  120. .input-box-left {
  121. width: 210rpx;
  122. min-width: 210rpx;
  123. color: #333333;
  124. }
  125. .input-box-right{
  126. display: flex;
  127. align-items: center;
  128. .radio-data{
  129. display: flex;
  130. flex-wrap: wrap;
  131. .radio-item{
  132. display: flex;
  133. align-items: center;
  134. margin: 6rpx 20rpx 0 6rpx;
  135. .iconfont{
  136. padding-right: 6rpx;
  137. }
  138. .icon-ok{
  139. color: var(--selected-color);
  140. }
  141. .icon-no{
  142. color: var(--unselected-color);
  143. }
  144. radio{
  145. display: none;
  146. }
  147. }
  148. }
  149. }
  150. }
  151. }
  152. </style>