dropdown-item.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 menuProps from '../dropdown-menu/props';
  11. import { getRect } from '../common/utils';
  12. const { prefix } = config;
  13. const name = `${prefix}-dropdown-item`;
  14. let DropdownMenuItem = class DropdownMenuItem extends SuperComponent {
  15. constructor() {
  16. super(...arguments);
  17. this.externalClasses = [
  18. `${prefix}-class`,
  19. `${prefix}-class-content`,
  20. `${prefix}-class-column`,
  21. `${prefix}-class-column-item`,
  22. `${prefix}-class-column-item-label`,
  23. `${prefix}-class-footer`,
  24. ];
  25. this.properties = Object.assign({}, props);
  26. this.data = {
  27. prefix,
  28. classPrefix: name,
  29. show: false,
  30. top: 0,
  31. maskHeight: 0,
  32. initValue: null,
  33. hasChanged: false,
  34. duration: menuProps.duration.value,
  35. zIndex: menuProps.zIndex.value,
  36. overlay: menuProps.showOverlay.value,
  37. labelAlias: 'label',
  38. valueAlias: 'value',
  39. };
  40. this.parent = null;
  41. this.relations = {
  42. '../dropdown-menu/dropdown-menu': {
  43. type: 'parent',
  44. linked(target) {
  45. const { zIndex, duration, showOverlay } = target.properties;
  46. this.parent = target;
  47. this.setData({
  48. zIndex,
  49. duration,
  50. showOverlay,
  51. });
  52. },
  53. },
  54. };
  55. this.controlledProps = [
  56. {
  57. key: 'value',
  58. event: 'change',
  59. },
  60. ];
  61. this.observers = {
  62. keys(obj) {
  63. this.setData({
  64. labelAlias: obj.label || 'label',
  65. valueAlias: obj.value || 'value',
  66. });
  67. },
  68. value(v) {
  69. const { options, labelAlias, valueAlias } = this.data;
  70. if (this.data.multiple) {
  71. if (!Array.isArray(v))
  72. throw TypeError('应传入数组类型的 value');
  73. }
  74. const target = options.find((item) => item[valueAlias] === v);
  75. if (target) {
  76. this.setData({
  77. label: target[labelAlias],
  78. });
  79. }
  80. },
  81. label() {
  82. var _a;
  83. (_a = this.parent) === null || _a === void 0 ? void 0 : _a.getAllItems();
  84. },
  85. show(visible) {
  86. if (visible) {
  87. this.getParentBottom(this.parent, () => {
  88. this.setData({ wrapperVisible: true });
  89. });
  90. }
  91. },
  92. };
  93. this.methods = {
  94. closeDropdown() {
  95. var _a;
  96. (_a = this.parent) === null || _a === void 0 ? void 0 : _a.setData({
  97. activeIdx: -1,
  98. });
  99. this.setData({
  100. show: false,
  101. });
  102. },
  103. getParentBottom(parent, cb) {
  104. getRect(parent, `#${prefix}-bar`).then((rect) => {
  105. this.setData({
  106. top: rect.bottom,
  107. maskHeight: rect.top,
  108. }, cb);
  109. });
  110. },
  111. handleTreeClick(e) {
  112. const { level, value: itemValue } = e.currentTarget.dataset;
  113. const { value } = this.data;
  114. value[level] = itemValue;
  115. this._trigger('change', { value });
  116. },
  117. handleRadioChange(e) {
  118. const { value } = e.detail;
  119. this._trigger('change', { value });
  120. if (!this.data.multiple) {
  121. this.closeDropdown();
  122. }
  123. },
  124. handleMaskClick() {
  125. var _a;
  126. if ((_a = this.parent) === null || _a === void 0 ? void 0 : _a.properties.closeOnClickOverlay) {
  127. this.closeDropdown();
  128. }
  129. },
  130. handleReset() {
  131. this._trigger('change', { value: [] });
  132. },
  133. handleConfirm() {
  134. this._trigger('confirm', { value: this.data.value });
  135. this.closeDropdown();
  136. },
  137. onLeaved() {
  138. this.setData({ wrapperVisible: false });
  139. },
  140. };
  141. }
  142. };
  143. DropdownMenuItem = __decorate([
  144. wxComponent()
  145. ], DropdownMenuItem);
  146. export default DropdownMenuItem;