tui-radio-group.vue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <template>
  2. <!-- #ifdef APP-PLUS || H5 || MP-ALIPAY || MP-TOUTIAO -->
  3. <radio-group :name="name">
  4. <slot></slot>
  5. </radio-group>
  6. <!-- #endif -->
  7. <!-- #ifdef MP-WEIXIN || MP-BAIDU || MP-QQ -->
  8. <tui-form-field :name="name" :value="val">
  9. <slot></slot>
  10. </tui-form-field>
  11. <!-- #endif -->
  12. </template>
  13. <script>
  14. export default {
  15. name: "tui-radio-group",
  16. emits: ['change', 'input', 'update:modelValue'],
  17. // #ifndef VUE3
  18. // #ifdef MP-WEIXIN
  19. behaviors: ['wx://form-field-group'],
  20. // #endif
  21. // #endif
  22. props: {
  23. name: {
  24. type: String,
  25. default: ''
  26. },
  27. // #ifdef VUE3
  28. modelValue: {
  29. type: String,
  30. default: ''
  31. },
  32. // #endif
  33. value: {
  34. type: String,
  35. default: ''
  36. }
  37. },
  38. data() {
  39. return {
  40. val: ''
  41. }
  42. },
  43. watch: {
  44. // #ifdef VUE3
  45. modelValue(val) {
  46. this.modelChange(val)
  47. },
  48. // #endif
  49. value(val) {
  50. this.modelChange(val)
  51. }
  52. },
  53. created() {
  54. this.childrens = []
  55. },
  56. methods: {
  57. radioChange(e) {
  58. this.$emit('change', e)
  59. this.$emit('input', e.detail.value)
  60. // #ifdef VUE3
  61. this.$emit("update:modelValue", e.detail.value);
  62. // #endif
  63. },
  64. changeValue(value, target) {
  65. this.val = value;
  66. this.childrens.forEach(item => {
  67. if (item !== target) {
  68. item.val = false;
  69. }
  70. })
  71. let e = {
  72. detail: {
  73. value: value
  74. }
  75. }
  76. this.radioChange(e)
  77. },
  78. modelChange(value) {
  79. this.childrens.forEach(item => {
  80. if (item.value === value) {
  81. item.val = true;
  82. } else {
  83. item.val = false;
  84. }
  85. })
  86. }
  87. }
  88. }
  89. </script>
  90. <style></style>