cl-radio.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <view class="cl-radio" :class="[classList]" @tap="change">
  3. <view class="cl-radio__input">
  4. <text class="cl-icon-toast-success" v-if="checked"></text>
  5. </view>
  6. <view class="cl-radio__label">
  7. <slot></slot>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. import Emitter from "../../mixins/emitter";
  13. import Form from "../../mixins/form";
  14. import Parent from "../../mixins/parent";
  15. /**
  16. * radio 单选框
  17. * @description 单选框
  18. * @tutorial https://docs.cool-js.com/uni/components/form/radio.html
  19. * @property {String, Number} value 绑定值
  20. * @property {String, Number} label 标识
  21. * @property {Boolean} disabled 是否禁用
  22. * @property {Boolean} border 是否边框样式
  23. * @event {Function} change 绑定值改变时触发
  24. * @example <cl-radio label="1"></cl-radio>
  25. */
  26. export default {
  27. name: "cl-radio",
  28. componentName: "ClRadio",
  29. props: {
  30. // 绑定值
  31. value: [String, Number],
  32. // 标识
  33. label: [String, Number],
  34. // 是否禁用
  35. disabled: Boolean,
  36. // 是否边框样式
  37. border: Boolean,
  38. },
  39. mixins: [Emitter, Form, Parent],
  40. data() {
  41. return {
  42. checked: false,
  43. Keys: ["border", "disabled", "value"],
  44. ComponentName: "ClRadioGroup",
  45. };
  46. },
  47. watch: {
  48. value: {
  49. immediate: true,
  50. handler(val) {
  51. this.checked = val === this.label;
  52. },
  53. },
  54. "parent.value": {
  55. immediate: true,
  56. handler(val) {
  57. if (this.hasParent) {
  58. this.checked = val === this.label;
  59. }
  60. },
  61. },
  62. },
  63. computed: {
  64. isBorder() {
  65. return this.hasParent ? this.parent.border || this.border : this.border;
  66. },
  67. isDisabled() {
  68. let disabled = this.hasParent ? this.parent.disabled || this.disabled : this.disabled;
  69. return this.$form ? this.$form.disabled || disabled : disabled;
  70. },
  71. classList() {
  72. let list = [];
  73. if (this.isBorder) {
  74. list.push("is-border");
  75. }
  76. if (this.isDisabled) {
  77. list.push("is-disabled");
  78. }
  79. if (this.checked) {
  80. list.push("is-checked");
  81. }
  82. return list.join(" ");
  83. },
  84. },
  85. methods: {
  86. change() {
  87. if (this.isDisabled) {
  88. return false;
  89. }
  90. this.checked = true;
  91. // 回调到组件或者单选框组
  92. if (this.hasParent) {
  93. this.dispatch("ClRadioGroup", "radio.change", this.label);
  94. } else {
  95. this.$emit("input", this.label);
  96. this.$emit("change", this.label);
  97. }
  98. },
  99. },
  100. };
  101. </script>