dropdown-item.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 './dropdown-item-props';
  10. import menuProps from './props';
  11. import { equal, clone } 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.properties = Object.assign({}, props);
  18. this.data = {
  19. prefix,
  20. classPrefix: name,
  21. show: false,
  22. top: 0,
  23. maskHeight: 0,
  24. contentClasses: '',
  25. leafLevel: 0,
  26. treeOptions: [],
  27. initValue: null,
  28. hasChanged: false,
  29. duration: menuProps.duration.value,
  30. zIndex: menuProps.zIndex.value,
  31. overlay: menuProps.showOverlay.value,
  32. labelAlias: 'label',
  33. valueAlias: 'value',
  34. };
  35. this.parent = null;
  36. this.relations = {
  37. './dropdown-menu': {
  38. type: 'parent',
  39. linked(target) {
  40. const { zIndex, duration, showOverlay } = target.properties;
  41. this.parent = target;
  42. this.setData({
  43. zIndex,
  44. duration,
  45. showOverlay,
  46. });
  47. },
  48. },
  49. };
  50. this.controlledProps = [
  51. {
  52. key: 'value',
  53. event: 'change',
  54. },
  55. ];
  56. this.observers = {
  57. value(v) {
  58. if (this.data.multiple) {
  59. if (!Array.isArray(v))
  60. throw TypeError('应传入数组类型的 value');
  61. }
  62. if (this.data.optionsLayout === 'tree') {
  63. this.buildTreeOptions();
  64. }
  65. },
  66. 'initValue, value'(v1, v2) {
  67. this.setData({
  68. hasChanged: !equal(v1, v2),
  69. });
  70. },
  71. label() {
  72. var _a;
  73. (_a = this.parent) === null || _a === void 0 ? void 0 : _a.getAllItems();
  74. },
  75. keys(obj) {
  76. this.setData({
  77. labelAlias: obj.label || 'label',
  78. valueAlias: obj.value || 'value',
  79. });
  80. },
  81. show(visible) {
  82. if (visible) {
  83. this.getParentBottom(this.parent, () => {
  84. this.setData({ wrapperVisible: true });
  85. });
  86. }
  87. },
  88. };
  89. this.lifetimes = {
  90. attached() {
  91. const { multiple, optionsLayout, value, defaultValue } = this.data;
  92. const isTree = optionsLayout === 'tree';
  93. const contentClassesObj = {
  94. [`${prefix}-is-tree`]: isTree,
  95. [`${prefix}-is-single`]: !isTree && !multiple,
  96. [`${prefix}-is-multi`]: !isTree && multiple,
  97. };
  98. const contentClasses = Object.keys(contentClassesObj)
  99. .filter((e) => contentClassesObj[e] === true)
  100. .join(' ');
  101. this.setData({
  102. contentClasses,
  103. initValue: clone(value || defaultValue),
  104. });
  105. },
  106. };
  107. this.methods = {
  108. buildTreeOptions() {
  109. const { options, value, multiple } = this.data;
  110. const treeOptions = [];
  111. let level = -1;
  112. let node = { options };
  113. while (node && node.options) {
  114. level += 1;
  115. const list = node.options;
  116. const thisValue = value === null || value === void 0 ? void 0 : value[level];
  117. treeOptions.push([...list]);
  118. if (thisValue == null) {
  119. const [firstChild] = list;
  120. node = firstChild;
  121. }
  122. else {
  123. const child = list.find((child) => child.value === thisValue);
  124. node = child !== null && child !== void 0 ? child : list[0];
  125. }
  126. }
  127. const leafLevel = Math.max(0, level);
  128. if (multiple) {
  129. const finalValue = this.data.value || this.data.defaultValue;
  130. if (!Array.isArray(finalValue[leafLevel])) {
  131. throw TypeError('应传入数组类型的 value');
  132. }
  133. }
  134. this.setData({
  135. leafLevel,
  136. treeOptions,
  137. });
  138. },
  139. closeDropdown() {
  140. var _a;
  141. (_a = this.parent) === null || _a === void 0 ? void 0 : _a.setData({
  142. activeIdx: -1,
  143. });
  144. this.setData({
  145. show: false,
  146. });
  147. },
  148. getParentBottom(parent, cb) {
  149. const query = wx.createSelectorQuery().in(parent);
  150. query
  151. .select(`#${prefix}-bar`)
  152. .boundingClientRect((res) => {
  153. this.setData({
  154. top: res.bottom,
  155. maskHeight: res.top,
  156. }, cb);
  157. })
  158. .exec();
  159. },
  160. handleTreeClick(e) {
  161. const { level, value: itemValue } = e.currentTarget.dataset;
  162. const { value } = this.data;
  163. value[level] = itemValue;
  164. this._trigger('change', { value });
  165. },
  166. handleRadioChange(e) {
  167. let { value } = this.data;
  168. const { value: itemValue } = e.detail;
  169. const { level } = e.target.dataset;
  170. if (this.data.optionsLayout === 'tree') {
  171. value[level] = itemValue;
  172. }
  173. else {
  174. value = itemValue;
  175. }
  176. this._trigger('change', { value });
  177. if (!this.data.multiple && this.data.optionsLayout !== 'tree') {
  178. this.closeDropdown();
  179. }
  180. },
  181. handleMaskClick() {
  182. var _a;
  183. if ((_a = this.parent) === null || _a === void 0 ? void 0 : _a.properties.closeOnClickOverlay) {
  184. this.closeDropdown();
  185. }
  186. },
  187. handleReset() {
  188. this._trigger('change', { value: clone(this.data.initValue) });
  189. },
  190. handleConfirm() {
  191. this._trigger('confirm', { value: this.data.value });
  192. this.closeDropdown();
  193. },
  194. onLeaved() {
  195. this.setData({ wrapperVisible: false });
  196. },
  197. };
  198. }
  199. };
  200. DropdownMenuItem = __decorate([
  201. wxComponent()
  202. ], DropdownMenuItem);
  203. export default DropdownMenuItem;