index.js 3.6 KB

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