collapse-item.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <div class="el-collapse-item"
  3. :class="{'is-active': isActive, 'is-disabled': disabled }">
  4. <div
  5. role="tab"
  6. :aria-expanded="isActive"
  7. :aria-controls="`el-collapse-content-${id}`"
  8. :aria-describedby ="`el-collapse-content-${id}`"
  9. >
  10. <div
  11. class="el-collapse-item__header"
  12. @click="handleHeaderClick"
  13. role="button"
  14. :id="`el-collapse-head-${id}`"
  15. :tabindex="disabled ? undefined : 0"
  16. @keyup.space.enter.stop="handleEnterClick"
  17. :class="{
  18. 'focusing': focusing,
  19. 'is-active': isActive
  20. }"
  21. @focus="handleFocus"
  22. @blur="focusing = false"
  23. >
  24. <slot name="title">{{title}}</slot>
  25. <i
  26. class="el-collapse-item__arrow el-icon-arrow-right"
  27. :class="{'is-active': isActive}">
  28. </i>
  29. </div>
  30. </div>
  31. <el-collapse-transition>
  32. <div
  33. class="el-collapse-item__wrap"
  34. v-show="isActive"
  35. role="tabpanel"
  36. :aria-hidden="!isActive"
  37. :aria-labelledby="`el-collapse-head-${id}`"
  38. :id="`el-collapse-content-${id}`"
  39. >
  40. <div class="el-collapse-item__content">
  41. <slot></slot>
  42. </div>
  43. </div>
  44. </el-collapse-transition>
  45. </div>
  46. </template>
  47. <script>
  48. import ElCollapseTransition from 'element-ui/src/transitions/collapse-transition';
  49. import Emitter from 'element-ui/src/mixins/emitter';
  50. import { generateId } from 'element-ui/src/utils/util';
  51. export default {
  52. name: 'ElCollapseItem',
  53. componentName: 'ElCollapseItem',
  54. mixins: [Emitter],
  55. components: { ElCollapseTransition },
  56. data() {
  57. return {
  58. contentWrapStyle: {
  59. height: 'auto',
  60. display: 'block'
  61. },
  62. contentHeight: 0,
  63. focusing: false,
  64. isClick: false,
  65. id: generateId()
  66. };
  67. },
  68. inject: ['collapse'],
  69. props: {
  70. title: String,
  71. name: {
  72. type: [String, Number],
  73. default() {
  74. return this._uid;
  75. }
  76. },
  77. disabled: Boolean
  78. },
  79. computed: {
  80. isActive() {
  81. return this.collapse.activeNames.indexOf(this.name) > -1;
  82. }
  83. },
  84. methods: {
  85. handleFocus() {
  86. setTimeout(() => {
  87. if (!this.isClick) {
  88. this.focusing = true;
  89. } else {
  90. this.isClick = false;
  91. }
  92. }, 50);
  93. },
  94. handleHeaderClick() {
  95. if (this.disabled) return;
  96. this.dispatch('ElCollapse', 'item-click', this);
  97. this.focusing = false;
  98. this.isClick = true;
  99. },
  100. handleEnterClick() {
  101. this.dispatch('ElCollapse', 'item-click', this);
  102. }
  103. }
  104. };
  105. </script>