checkbox.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <label class="el-checkbox">
  3. <span class="el-checkbox__input">
  4. <span class="el-checkbox__inner"
  5. :class="{
  6. 'is-disabled': disabled,
  7. 'is-checked': isChecked,
  8. 'is-indeterminate': indeterminate,
  9. 'is-focus': focus
  10. }"
  11. ></span>
  12. <input
  13. v-if="trueLabel || falseLabel"
  14. class="el-checkbox__original"
  15. type="checkbox"
  16. :name="name"
  17. :disabled="disabled"
  18. :true-value="trueLabel"
  19. :false-value="falseLabel"
  20. v-model="model"
  21. @change="$emit('change', $event)"
  22. @focus="focus = true"
  23. @blur="focus = false">
  24. <input
  25. v-else
  26. class="el-checkbox__original"
  27. type="checkbox"
  28. :disabled="disabled"
  29. :value="label"
  30. :name="name"
  31. v-model="model"
  32. @change="$emit('change', $event)"
  33. @focus="focus = true"
  34. @blur="focus = false">
  35. </span>
  36. <span class="el-checkbox__label" v-if="$slots.default || label">
  37. <slot></slot>
  38. <template v-if="!$slots.default">{{label}}</template>
  39. </span>
  40. </label>
  41. </template>
  42. <script>
  43. import Emitter from 'element-ui/src/mixins/emitter';
  44. export default {
  45. name: 'ElCheckbox',
  46. mixins: [Emitter],
  47. componentName: 'ElCheckbox',
  48. computed: {
  49. model: {
  50. get() {
  51. return this.isGroup ? this.store : this.value;
  52. },
  53. set(val) {
  54. if (this.isGroup) {
  55. this.dispatch('ElCheckboxGroup', 'input', [val]);
  56. } else {
  57. this.$emit('input', val);
  58. }
  59. }
  60. },
  61. isChecked() {
  62. if ({}.toString.call(this.model) === '[object Boolean]') {
  63. return this.model;
  64. } else if (Array.isArray(this.model)) {
  65. return this.model.indexOf(this.label) > -1;
  66. } else if (this.model !== null && this.model !== undefined) {
  67. return this.model === this.trueLabel;
  68. }
  69. },
  70. isGroup() {
  71. let parent = this.$parent;
  72. while (parent) {
  73. if (parent.$options.componentName !== 'ElCheckboxGroup') {
  74. parent = parent.$parent;
  75. } else {
  76. this._checkboxGroup = parent;
  77. return true;
  78. }
  79. }
  80. return false;
  81. },
  82. store() {
  83. return this._checkboxGroup.value;
  84. }
  85. },
  86. props: {
  87. value: {},
  88. label: {},
  89. indeterminate: Boolean,
  90. disabled: Boolean,
  91. checked: Boolean,
  92. name: String,
  93. trueLabel: [String, Number],
  94. falseLabel: [String, Number]
  95. },
  96. data() {
  97. return {
  98. isGroup: false
  99. };
  100. },
  101. methods: {
  102. addToStore() {
  103. if (Array.isArray(this.model)) {
  104. this.model.indexOf(this.label) === -1 && this.model.push(this.label);
  105. } else {
  106. this.model = this.trueLabel || true;
  107. }
  108. }
  109. },
  110. created() {
  111. this.checked && this.addToStore();
  112. }
  113. };
  114. </script>