radio.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <label
  3. class="el-radio"
  4. :class="[
  5. border && radioSize ? 'el-radio--' + radioSize : '',
  6. { 'is-disabled': isDisabled },
  7. { 'is-focus': focus },
  8. { 'is-bordered': border },
  9. { 'is-checked': model === label }
  10. ]"
  11. role="radio"
  12. :aria-checked="model === label"
  13. :aria-disabled="isDisabled"
  14. :tabindex="tabIndex"
  15. @keydown.space.stop.prevent="model = isDisabled ? model : label"
  16. >
  17. <span class="el-radio__input"
  18. :class="{
  19. 'is-disabled': isDisabled,
  20. 'is-checked': model === label
  21. }"
  22. >
  23. <span class="el-radio__inner"></span>
  24. <input
  25. ref="radio"
  26. class="el-radio__original"
  27. :value="label"
  28. type="radio"
  29. aria-hidden="true"
  30. v-model="model"
  31. @focus="focus = true"
  32. @blur="focus = false"
  33. @change="handleChange"
  34. :name="name"
  35. :disabled="isDisabled"
  36. tabindex="-1"
  37. >
  38. </span>
  39. <span class="el-radio__label" @keydown.stop>
  40. <slot></slot>
  41. <template v-if="!$slots.default">{{label}}</template>
  42. </span>
  43. </label>
  44. </template>
  45. <script>
  46. import Emitter from 'element-ui/src/mixins/emitter';
  47. export default {
  48. name: 'ElRadio',
  49. mixins: [Emitter],
  50. inject: {
  51. elForm: {
  52. default: ''
  53. },
  54. elFormItem: {
  55. default: ''
  56. }
  57. },
  58. componentName: 'ElRadio',
  59. props: {
  60. value: {},
  61. label: {},
  62. disabled: Boolean,
  63. name: String,
  64. border: Boolean,
  65. size: String
  66. },
  67. data() {
  68. return {
  69. focus: false
  70. };
  71. },
  72. computed: {
  73. isGroup() {
  74. let parent = this.$parent;
  75. while (parent) {
  76. if (parent.$options.componentName !== 'ElRadioGroup') {
  77. parent = parent.$parent;
  78. } else {
  79. this._radioGroup = parent;
  80. return true;
  81. }
  82. }
  83. return false;
  84. },
  85. model: {
  86. get() {
  87. return this.isGroup ? this._radioGroup.value : this.value;
  88. },
  89. set(val) {
  90. if (this.isGroup) {
  91. this.dispatch('ElRadioGroup', 'input', [val]);
  92. } else {
  93. this.$emit('input', val);
  94. }
  95. this.$refs.radio && (this.$refs.radio.checked = this.model === this.label);
  96. }
  97. },
  98. _elFormItemSize() {
  99. return (this.elFormItem || {}).elFormItemSize;
  100. },
  101. radioSize() {
  102. const temRadioSize = this.size || this._elFormItemSize || (this.$ELEMENT || {}).size;
  103. return this.isGroup
  104. ? this._radioGroup.radioGroupSize || temRadioSize
  105. : temRadioSize;
  106. },
  107. isDisabled() {
  108. return this.isGroup
  109. ? this._radioGroup.disabled || this.disabled || (this.elForm || {}).disabled
  110. : this.disabled || (this.elForm || {}).disabled;
  111. },
  112. tabIndex() {
  113. return (this.isDisabled || (this.isGroup && this.model !== this.label)) ? -1 : 0;
  114. }
  115. },
  116. methods: {
  117. handleChange() {
  118. this.$nextTick(() => {
  119. this.$emit('change', this.model);
  120. this.isGroup && this.dispatch('ElRadioGroup', 'handleChange', this.model);
  121. });
  122. }
  123. }
  124. };
  125. </script>