component.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <div
  3. class="el-switch"
  4. :class="{ 'is-disabled': switchDisabled, 'is-checked': checked }"
  5. role="switch"
  6. :aria-checked="checked"
  7. :aria-disabled="switchDisabled"
  8. @click.prevent="switchValue"
  9. >
  10. <input
  11. class="el-switch__input"
  12. type="checkbox"
  13. @change="handleChange"
  14. ref="input"
  15. :id="id"
  16. :name="name"
  17. :true-value="activeValue"
  18. :false-value="inactiveValue"
  19. :disabled="switchDisabled"
  20. @keydown.enter="switchValue"
  21. >
  22. <span
  23. :class="['el-switch__label', 'el-switch__label--left', !checked ? 'is-active' : '']"
  24. v-if="inactiveIconClass || inactiveText">
  25. <i :class="[inactiveIconClass]" v-if="inactiveIconClass"></i>
  26. <span v-if="!inactiveIconClass && inactiveText" :aria-hidden="checked">{{ inactiveText }}</span>
  27. </span>
  28. <span class="el-switch__core" ref="core" :style="{ 'width': coreWidth + 'px' }">
  29. </span>
  30. <span
  31. :class="['el-switch__label', 'el-switch__label--right', checked ? 'is-active' : '']"
  32. v-if="activeIconClass || activeText">
  33. <i :class="[activeIconClass]" v-if="activeIconClass"></i>
  34. <span v-if="!activeIconClass && activeText" :aria-hidden="!checked">{{ activeText }}</span>
  35. </span>
  36. </div>
  37. </template>
  38. <script>
  39. import emitter from 'element-ui/src/mixins/emitter';
  40. import Focus from 'element-ui/src/mixins/focus';
  41. import Migrating from 'element-ui/src/mixins/migrating';
  42. export default {
  43. name: 'ElSwitch',
  44. mixins: [Focus('input'), Migrating, emitter],
  45. inject: {
  46. elForm: {
  47. default: ''
  48. }
  49. },
  50. props: {
  51. value: {
  52. type: [Boolean, String, Number],
  53. default: false
  54. },
  55. disabled: {
  56. type: Boolean,
  57. default: false
  58. },
  59. width: {
  60. type: Number,
  61. default: 40
  62. },
  63. activeIconClass: {
  64. type: String,
  65. default: ''
  66. },
  67. inactiveIconClass: {
  68. type: String,
  69. default: ''
  70. },
  71. activeText: String,
  72. inactiveText: String,
  73. activeColor: {
  74. type: String,
  75. default: ''
  76. },
  77. inactiveColor: {
  78. type: String,
  79. default: ''
  80. },
  81. activeValue: {
  82. type: [Boolean, String, Number],
  83. default: true
  84. },
  85. inactiveValue: {
  86. type: [Boolean, String, Number],
  87. default: false
  88. },
  89. name: {
  90. type: String,
  91. default: ''
  92. },
  93. validateEvent: {
  94. type: Boolean,
  95. default: true
  96. },
  97. id: String
  98. },
  99. data() {
  100. return {
  101. coreWidth: this.width
  102. };
  103. },
  104. created() {
  105. if (!~[this.activeValue, this.inactiveValue].indexOf(this.value)) {
  106. this.$emit('input', this.inactiveValue);
  107. }
  108. },
  109. computed: {
  110. checked() {
  111. return this.value === this.activeValue;
  112. },
  113. switchDisabled() {
  114. return this.disabled || (this.elForm || {}).disabled;
  115. }
  116. },
  117. watch: {
  118. checked() {
  119. this.$refs.input.checked = this.checked;
  120. if (this.activeColor || this.inactiveColor) {
  121. this.setBackgroundColor();
  122. }
  123. if (this.validateEvent) {
  124. this.dispatch('ElFormItem', 'el.form.change', [this.value]);
  125. }
  126. }
  127. },
  128. methods: {
  129. handleChange(event) {
  130. const val = this.checked ? this.inactiveValue : this.activeValue;
  131. this.$emit('input', val);
  132. this.$emit('change', val);
  133. this.$nextTick(() => {
  134. // set input's checked property
  135. // in case parent refuses to change component's value
  136. this.$refs.input.checked = this.checked;
  137. });
  138. },
  139. setBackgroundColor() {
  140. let newColor = this.checked ? this.activeColor : this.inactiveColor;
  141. this.$refs.core.style.borderColor = newColor;
  142. this.$refs.core.style.backgroundColor = newColor;
  143. },
  144. switchValue() {
  145. !this.switchDisabled && this.handleChange();
  146. },
  147. getMigratingConfig() {
  148. return {
  149. props: {
  150. 'on-color': 'on-color is renamed to active-color.',
  151. 'off-color': 'off-color is renamed to inactive-color.',
  152. 'on-text': 'on-text is renamed to active-text.',
  153. 'off-text': 'off-text is renamed to inactive-text.',
  154. 'on-value': 'on-value is renamed to active-value.',
  155. 'off-value': 'off-value is renamed to inactive-value.',
  156. 'on-icon-class': 'on-icon-class is renamed to active-icon-class.',
  157. 'off-icon-class': 'off-icon-class is renamed to inactive-icon-class.'
  158. }
  159. };
  160. }
  161. },
  162. mounted() {
  163. /* istanbul ignore if */
  164. this.coreWidth = this.width || 40;
  165. if (this.activeColor || this.inactiveColor) {
  166. this.setBackgroundColor();
  167. }
  168. this.$refs.input.checked = this.checked;
  169. }
  170. };
  171. </script>