tab-bar-item.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  8. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  9. return new (P || (P = Promise))(function (resolve, reject) {
  10. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  11. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  12. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  13. step((generator = generator.apply(thisArg, _arguments || [])).next());
  14. });
  15. };
  16. import { wxComponent, SuperComponent } from '../common/src/index';
  17. import config from '../common/config';
  18. import props from './props';
  19. import { getRect, calcIcon } from '../common/utils';
  20. const { prefix } = config;
  21. const classPrefix = `${prefix}-tab-bar-item`;
  22. let TabBarItem = class TabBarItem extends SuperComponent {
  23. constructor() {
  24. super(...arguments);
  25. this.externalClasses = [`${prefix}-class`];
  26. this.parent = null;
  27. this.relations = {
  28. '../tab-bar/tab-bar': {
  29. type: 'ancestor',
  30. linked(parent) {
  31. const { theme, split, shape } = parent.data;
  32. this.setData({
  33. theme,
  34. split,
  35. shape,
  36. currentName: this.properties.value ? this.properties.value : parent.initName(),
  37. });
  38. parent.updateChildren();
  39. },
  40. },
  41. };
  42. this.options = {
  43. multipleSlots: true,
  44. };
  45. this.data = {
  46. prefix,
  47. classPrefix,
  48. isSpread: false,
  49. isChecked: false,
  50. hasChildren: false,
  51. currentName: '',
  52. split: true,
  53. iconOnly: false,
  54. theme: '',
  55. crowded: false,
  56. shape: 'normal',
  57. };
  58. this.properties = props;
  59. this.observers = {
  60. subTabBar(value) {
  61. this.setData({
  62. hasChildren: value.length > 0,
  63. });
  64. },
  65. icon(v) {
  66. this.setData({
  67. _icon: calcIcon(v),
  68. });
  69. },
  70. };
  71. this.lifetimes = {
  72. attached() {
  73. return __awaiter(this, void 0, void 0, function* () {
  74. const res = yield getRect(this, `.${classPrefix}__text`);
  75. this.setData({ iconOnly: res.height === 0 });
  76. });
  77. },
  78. };
  79. this.methods = {
  80. showSpread() {
  81. this.setData({
  82. isSpread: true,
  83. });
  84. },
  85. toggle() {
  86. const { currentName, hasChildren, isSpread } = this.data;
  87. if (hasChildren) {
  88. this.setData({
  89. isSpread: !isSpread,
  90. });
  91. }
  92. this.$parent.updateValue(currentName);
  93. this.$parent.changeOtherSpread(currentName);
  94. },
  95. selectChild(event) {
  96. const { value } = event.target.dataset;
  97. this.$parent.updateValue(value);
  98. this.setData({
  99. isSpread: false,
  100. });
  101. },
  102. checkActive(value) {
  103. const { currentName, subTabBar } = this.data;
  104. const isChecked = (subTabBar === null || subTabBar === void 0 ? void 0 : subTabBar.some((item) => item.value === value)) || currentName === value;
  105. this.setData({
  106. isChecked,
  107. });
  108. },
  109. closeSpread() {
  110. this.setData({
  111. isSpread: false,
  112. });
  113. },
  114. };
  115. }
  116. };
  117. TabBarItem = __decorate([
  118. wxComponent()
  119. ], TabBarItem);
  120. export default TabBarItem;