tab-bar-item.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 } 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.parent = null;
  26. this.relations = {
  27. '../tab-bar/tab-bar': {
  28. type: 'ancestor',
  29. linked(parent) {
  30. const { theme, split, shape } = parent.data;
  31. this.setData({
  32. theme,
  33. split,
  34. shape,
  35. currentName: this.properties.value ? this.properties.value : parent.initName(),
  36. });
  37. parent.updateChildren();
  38. },
  39. },
  40. };
  41. this.options = {
  42. multipleSlots: true,
  43. };
  44. this.data = {
  45. prefix,
  46. classPrefix,
  47. isSpread: false,
  48. isChecked: false,
  49. hasChildren: false,
  50. currentName: '',
  51. split: true,
  52. iconOnly: false,
  53. theme: '',
  54. crowded: false,
  55. shape: 'normal',
  56. };
  57. this.properties = props;
  58. this.observers = {
  59. subTabBar(value) {
  60. this.setData({
  61. hasChildren: value.length > 0,
  62. });
  63. },
  64. };
  65. this.lifetimes = {
  66. attached() {
  67. return __awaiter(this, void 0, void 0, function* () {
  68. const res = yield getRect(this, `.${classPrefix}__text`);
  69. this.setData({ iconOnly: res.height === 0 });
  70. });
  71. },
  72. };
  73. this.methods = {
  74. showSpread() {
  75. this.setData({
  76. isSpread: true,
  77. });
  78. },
  79. toggle() {
  80. const { currentName, hasChildren, isSpread } = this.data;
  81. if (hasChildren) {
  82. this.setData({
  83. isSpread: !isSpread,
  84. });
  85. }
  86. this.$parent.updateValue(currentName);
  87. this.$parent.changeOtherSpread(currentName);
  88. },
  89. selectChild(event) {
  90. const { value } = event.target.dataset;
  91. this.$parent.updateValue(value);
  92. this.setData({
  93. isSpread: false,
  94. });
  95. },
  96. checkActive(value) {
  97. const { currentName, subTabBar } = this.data;
  98. const isChecked = (subTabBar === null || subTabBar === void 0 ? void 0 : subTabBar.some((item) => item.value === value)) || currentName === value;
  99. this.setData({
  100. isChecked,
  101. });
  102. },
  103. closeSpread() {
  104. this.setData({
  105. isSpread: false,
  106. });
  107. },
  108. };
  109. }
  110. };
  111. TabBarItem = __decorate([
  112. wxComponent()
  113. ], TabBarItem);
  114. export default TabBarItem;