dropdown-menu.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 { SuperComponent, wxComponent } from '../common/src/index';
  8. import config from '../common/config';
  9. import props from './props';
  10. import { calcIcon } from '../common/utils';
  11. const { prefix } = config;
  12. const name = `${prefix}-dropdown-menu`;
  13. let DropdownMenu = class DropdownMenu extends SuperComponent {
  14. constructor() {
  15. super(...arguments);
  16. this.externalClasses = [`${prefix}-class`, `${prefix}-class-item`, `${prefix}-class-label`, `${prefix}-class-icon`];
  17. this.properties = props;
  18. this.nodes = null;
  19. this.data = {
  20. prefix,
  21. classPrefix: name,
  22. menus: null,
  23. activeIdx: -1,
  24. bottom: 0,
  25. _arrowIcon: { name: props.arrowIcon.value },
  26. };
  27. this.relations = {
  28. '../dropdown-item/dropdown-item': {
  29. type: 'child',
  30. },
  31. };
  32. this.lifetimes = {
  33. ready() {
  34. this.getAllItems();
  35. },
  36. };
  37. this.observers = {
  38. arrowIcon(v) {
  39. this.setData({
  40. _arrowIcon: calcIcon(v),
  41. });
  42. },
  43. activeIdx(v) {
  44. this.triggerEvent(v === -1 ? 'close' : 'open');
  45. },
  46. };
  47. this.methods = {
  48. toggle(index) {
  49. const { activeIdx, duration } = this.data;
  50. const prevItem = this.$children[activeIdx];
  51. const currItem = this.$children[index];
  52. if (currItem === null || currItem === void 0 ? void 0 : currItem.data.disabled)
  53. return;
  54. if (activeIdx !== -1) {
  55. prevItem.triggerEvent('close');
  56. prevItem.setData({
  57. show: false,
  58. }, () => {
  59. setTimeout(() => {
  60. prevItem.triggerEvent('closed');
  61. }, duration);
  62. });
  63. }
  64. if (index == null || activeIdx === index) {
  65. this.setData({
  66. activeIdx: -1,
  67. });
  68. }
  69. else {
  70. currItem.triggerEvent('open');
  71. this.setData({
  72. activeIdx: index,
  73. });
  74. currItem.setData({
  75. show: true,
  76. }, () => {
  77. setTimeout(() => {
  78. currItem.triggerEvent('opened');
  79. }, duration);
  80. });
  81. }
  82. },
  83. getAllItems() {
  84. const menus = this.$children.map(({ data }) => ({
  85. label: data.label || data.computedLabel,
  86. disabled: data.disabled,
  87. }));
  88. this.setData({
  89. menus,
  90. });
  91. },
  92. handleToggle(e) {
  93. const { index } = e.currentTarget.dataset;
  94. this.toggle(index);
  95. },
  96. noop() { },
  97. };
  98. }
  99. };
  100. DropdownMenu = __decorate([
  101. wxComponent()
  102. ], DropdownMenu);
  103. export default DropdownMenu;