index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import scrollCenter from '../behaviors/scrollCenter';
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. behaviors: [scrollCenter],
  7. externalClasses: [
  8. 'l-class',
  9. 'l-header-class',
  10. 'l-class-active',
  11. 'l-active-class',
  12. 'l-class-inactive',
  13. 'l-inactive-class',
  14. 'l-class-tabimage',
  15. 'l-tab-image-class',
  16. 'l-class-header-line',
  17. 'l-header-line-class',
  18. 'l-class-line',
  19. 'l-line-class',
  20. 'l-class-icon',
  21. 'l-icon-class',
  22. 'l-class-badge',
  23. 'l-badge-class'
  24. ],
  25. options: {
  26. multipleSlots: true // 在组件定义时的选项中启用多slot支持
  27. },
  28. relations: {
  29. '../segment-item/index': {
  30. type: 'child',
  31. linked() {
  32. // 每次有子节点被插入时执行,target是该节点实例对象,触发在该节点attached生命周期之后
  33. this.initTabs();
  34. }
  35. },
  36. },
  37. properties: {
  38. activeKey: {
  39. type: String,
  40. value: '',
  41. observer: 'changeCurrent'
  42. },
  43. placement: {
  44. type: String,
  45. value: 'top',
  46. },
  47. scrollable: Boolean,
  48. hasLine: {
  49. type: Boolean,
  50. value: true
  51. },
  52. animatedForLine: Boolean,
  53. activeColor: {
  54. type: String,
  55. },
  56. inactiveColor: {
  57. type: String,
  58. },
  59. equalWidth: {
  60. type: Boolean,
  61. value: true
  62. },
  63. even: {
  64. type: Boolean,
  65. value: true
  66. },
  67. width: Number,
  68. height: Number,
  69. itemHeight: Number,
  70. itemWidth: Number
  71. },
  72. observers: {
  73. 'activeKey': function (newKey) {
  74. if(!newKey) return;
  75. const index = this.data.tabList.findIndex(tab=>tab.key===newKey);
  76. this.setData({
  77. currentIndex:index
  78. },() => {
  79. if (this.data.scrollable) {
  80. this.queryMultipleNodes();
  81. }
  82. });
  83. }
  84. },
  85. /**
  86. * 组件的初始数据
  87. */
  88. data: {
  89. tabList: [],
  90. currentIndex: 0
  91. },
  92. /**
  93. * 组件的方法列表
  94. */
  95. methods: {
  96. initTabs(val = this.data.activeKey) {
  97. let items = this.getRelationNodes('../segment-item/index');
  98. if (items.length > 0) {
  99. if (items.length === this.data.tabList.length) return;
  100. let activeKey = val,
  101. currentIndex = this.data.currentIndex;
  102. const tab = items.map((item, index) => {
  103. activeKey = !val && index == 0 ? item.data.key : activeKey;
  104. currentIndex = item.data.key === activeKey ? index : currentIndex;
  105. return {
  106. ...item.data
  107. };
  108. });
  109. this.setData({
  110. tabList: tab,
  111. activeKey,
  112. currentIndex,
  113. }, () => {
  114. if (this.data.scrollable) {
  115. this.queryMultipleNodes();
  116. }
  117. });
  118. }
  119. },
  120. handleChange(e) {
  121. const activeKey = e.currentTarget.dataset.key;
  122. const currentIndex = e.currentTarget.dataset.index;
  123. this._setChangeData({
  124. activeKey,
  125. currentIndex
  126. });
  127. },
  128. _setChangeData({
  129. activeKey,
  130. currentIndex
  131. }) {
  132. this.setData({
  133. activeKey,
  134. currentIndex
  135. }, () => {
  136. if (this.data.scrollable) {
  137. this.queryMultipleNodes();
  138. }
  139. });
  140. this.triggerEvent('linchange', {
  141. activeKey,
  142. currentIndex
  143. });
  144. }
  145. }
  146. });