radio-button.vue 2.7 KB

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