dropdown-menu.js 3.3 KB

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