en-radio.vue 3.1 KB

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