c-radio.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <template>
  2. <view @click="_click" style="padding-left: 10px;padding-top: 10px;padding-bottom: 10px;">
  3. <view class="switch" :class="{active: isChecked}" >
  4. <view class="switch-tap">
  5. <u-icon v-if="isChecked" name="checkmark" color="#333" size="14"></u-icon>
  6. </view>
  7. </view>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. data() {
  13. return {
  14. isChecked: false,
  15. _cols: 1
  16. };
  17. },
  18. props: {
  19. value: false,
  20. defaultLabel: {
  21. type: String,
  22. default: '关闭'
  23. },
  24. defaultVal: {
  25. type: String | Number,
  26. default: '关'
  27. },
  28. activeLabel: {
  29. type: String,
  30. default: '开启'
  31. },
  32. activeVal: {
  33. type: String | Number,
  34. default: '开'
  35. },
  36. checked: {
  37. type: Boolean,
  38. default: false
  39. }
  40. },
  41. created() {
  42. },
  43. mounted() {
  44. // 初始化值
  45. if(String(this.value) == 'true'){
  46. this.isChecked = true;
  47. // this.$emit('input',this.activeVal)
  48. }else{
  49. this.isChecked = false;
  50. // this.$emit('input',this.defaultVal)
  51. }
  52. },
  53. watch:{
  54. value(nval,oVal){
  55. // 初始化值
  56. if(String(nval) == 'true'){
  57. this.isChecked = true;
  58. // this.$emit('input',this.activeVal)
  59. }else{
  60. this.isChecked = false;
  61. // this.$emit('input',this.defaultVal)
  62. }
  63. }
  64. },
  65. methods: {
  66. _click() {
  67. // 触发改变事件
  68. this.isChecked = !this.isChecked;
  69. if(String(this.isChecked) == 'true'){
  70. this.$emit('input',true)
  71. }else{
  72. this.$emit('input',false)
  73. }
  74. this.$forceUpdate()
  75. }
  76. },
  77. beforeDestroy() {
  78. }
  79. }
  80. </script>
  81. <style lang="scss" scoped>
  82. .switch{
  83. position: relative;
  84. display: flex;
  85. width: 30rpx;
  86. height: 30rpx;
  87. transition: background-color 0.2s;
  88. border: 1px solid #dedede;
  89. border-radius: 50%;
  90. font-size: 24rpx;
  91. &.active{
  92. border: 1px solid #333;
  93. }
  94. }
  95. </style>