dropdown-menu.js 3.2 KB

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