tabs.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import dom from '../behaviors/dom';
  8. import touch from '../behaviors/touch';
  9. import { SuperComponent, wxComponent } from '../common/src/index';
  10. import props from './props';
  11. import config from '../common/config';
  12. const { prefix } = config;
  13. const name = `${prefix}-tabs`;
  14. var Position;
  15. (function (Position) {
  16. Position["top"] = "top";
  17. Position["right"] = "right";
  18. Position["bottom"] = "bottom";
  19. Position["left"] = "left";
  20. })(Position || (Position = {}));
  21. const trackLineWidth = 30;
  22. let Tabs = class Tabs extends SuperComponent {
  23. constructor() {
  24. super(...arguments);
  25. this.behaviors = [dom, touch];
  26. this.externalClasses = [
  27. `${prefix}-class`,
  28. `${prefix}-class-item`,
  29. `${prefix}-class-active`,
  30. `${prefix}-class-track`,
  31. ];
  32. this.relations = {
  33. './tab-panel': {
  34. type: 'descendant',
  35. linked(target) {
  36. this.children.push(target);
  37. target.index = this.children.length - 1;
  38. this.updateTabs();
  39. },
  40. unlinked() {
  41. this.children = this.children.map((child, index) => {
  42. child.index = index;
  43. return child;
  44. });
  45. this.updateTabs();
  46. },
  47. },
  48. };
  49. this.properties = props;
  50. this.controlledProps = [
  51. {
  52. key: 'value',
  53. event: 'change',
  54. },
  55. ];
  56. this.observers = {
  57. value(name) {
  58. if (name !== this.getCurrentName()) {
  59. this.setCurrentIndexByName(name);
  60. }
  61. },
  62. animation(v) {
  63. this.setData({ animate: v });
  64. },
  65. };
  66. this.data = {
  67. prefix,
  68. classPrefix: name,
  69. tabs: [],
  70. currentIndex: -1,
  71. trackStyle: '',
  72. isScrollX: true,
  73. isScrollY: false,
  74. direction: 'X',
  75. animate: { duration: 0 },
  76. };
  77. }
  78. created() {
  79. this.children = this.children || [];
  80. }
  81. attached() {
  82. wx.nextTick(() => {
  83. this.setTrack();
  84. });
  85. // 根据placement判断scroll-view滚动方向
  86. const { placement } = this.properties;
  87. let isScrollX = false;
  88. let isScrollY = false;
  89. if (placement === Position.top || placement === Position.bottom) {
  90. isScrollX = true;
  91. }
  92. else {
  93. isScrollY = true;
  94. }
  95. this.setData({
  96. isScrollX,
  97. isScrollY,
  98. direction: isScrollX ? 'X' : 'Y',
  99. });
  100. }
  101. updateTabs() {
  102. const { children } = this;
  103. this.setData({
  104. tabs: children.map((child) => child.data),
  105. });
  106. this.setCurrentIndexByName(this.properties.value);
  107. }
  108. setCurrentIndexByName(name) {
  109. const { children } = this;
  110. const index = children.findIndex((child) => child.getComputedName() === `${name}`);
  111. if (index > -1) {
  112. this.setCurrentIndex(index);
  113. }
  114. }
  115. setCurrentIndex(index) {
  116. if (index <= -1 || index >= this.children.length)
  117. return;
  118. this.children.forEach((child, idx) => {
  119. const isActive = index === idx;
  120. if (isActive !== child.data.active) {
  121. child.render(isActive, this);
  122. }
  123. });
  124. if (this.data.currentIndex === index)
  125. return;
  126. this.setData({
  127. currentIndex: index,
  128. });
  129. this.setTrack();
  130. }
  131. getCurrentName() {
  132. if (this.children) {
  133. const activeTab = this.children[this.data.currentIndex];
  134. if (activeTab) {
  135. return activeTab.getComputedName();
  136. }
  137. }
  138. }
  139. setTrack(color = '#0052d9') {
  140. if (!this.properties.showBottomLine)
  141. return;
  142. const { children } = this;
  143. if (!children)
  144. return;
  145. const { currentIndex, isScrollX, direction } = this.data;
  146. if (currentIndex <= -1)
  147. return;
  148. this.gettingBoundingClientRect(`.${prefix}-tabs__item`, true)
  149. .then((res) => {
  150. const rect = res[currentIndex];
  151. if (!rect)
  152. return;
  153. let count = 0;
  154. let distance = 0;
  155. // eslint-disable-next-line no-restricted-syntax
  156. for (const item of res) {
  157. if (count < currentIndex) {
  158. distance += isScrollX ? item.width : item.height;
  159. count += 1;
  160. }
  161. }
  162. if (isScrollX) {
  163. distance += (rect.width - trackLineWidth) / 2;
  164. }
  165. let trackStyle = `background-color: ${color};
  166. -webkit-transform: translate${direction}(${distance}px);
  167. transform: translate${direction}(${distance}px);
  168. -webkit-transition-duration: 0.3s;
  169. transition-duration: 0.3s;
  170. `;
  171. trackStyle += isScrollX ? `width: ${trackLineWidth}px;` : `height: ${rect.height}px;`;
  172. this.setData({
  173. trackStyle,
  174. });
  175. })
  176. .catch((err) => {
  177. this.triggerEvent('error', err);
  178. });
  179. }
  180. onTabTap(event) {
  181. const { index } = event.currentTarget.dataset;
  182. this.changeIndex(index);
  183. }
  184. onTouchStart(event) {
  185. this.touchStart(event);
  186. }
  187. onTouchMove(event) {
  188. this.touchMove(event);
  189. }
  190. onTouchEnd() {
  191. const { direction, deltaX, offsetX } = this;
  192. const minSwipeDistance = 50;
  193. if (direction === 'horizontal' && offsetX >= minSwipeDistance) {
  194. const index = this.getAvailableTabIndex(deltaX);
  195. this.changeIndex(index);
  196. }
  197. }
  198. changeIndex(index) {
  199. const currentTab = this.data.tabs[index];
  200. if (!(currentTab === null || currentTab === void 0 ? void 0 : currentTab.disabled) && index !== this.data.currentIndex) {
  201. this._trigger('change', { value: currentTab.value });
  202. }
  203. }
  204. getAvailableTabIndex(deltaX) {
  205. const step = deltaX > 0 ? -1 : 1;
  206. const { currentIndex, tabs } = this.data;
  207. const len = tabs.length;
  208. for (let i = step; currentIndex + step >= 0 && currentIndex + step < len; i += step) {
  209. const newIndex = currentIndex + i;
  210. if (newIndex >= 0 && newIndex < len && tabs[newIndex] && !tabs[newIndex].disabled) {
  211. return newIndex;
  212. }
  213. }
  214. return -1;
  215. }
  216. };
  217. Tabs = __decorate([
  218. wxComponent()
  219. ], Tabs);
  220. export default Tabs;
  221. //# sourceMappingURL=tabs.js.map