合伙人运营小程序
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

cascader.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  8. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  9. return new (P || (P = Promise))(function (resolve, reject) {
  10. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  11. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  12. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  13. step((generator = generator.apply(thisArg, _arguments || [])).next());
  14. });
  15. };
  16. import { SuperComponent, wxComponent } from '../common/src/index';
  17. import config from '../common/config';
  18. import props from './props';
  19. import { getRect } from '../common/utils';
  20. const { prefix } = config;
  21. const name = `${prefix}-cascader`;
  22. function parseOptions(options, keys) {
  23. var _a, _b;
  24. const label = (_a = keys === null || keys === void 0 ? void 0 : keys.label) !== null && _a !== void 0 ? _a : 'label';
  25. const value = (_b = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _b !== void 0 ? _b : 'value';
  26. return options.map((item) => {
  27. return {
  28. [label]: item[label],
  29. [value]: item[value],
  30. };
  31. });
  32. }
  33. let Cascader = class Cascader extends SuperComponent {
  34. constructor() {
  35. super(...arguments);
  36. this.externalClasses = [`${prefix}-class`];
  37. this.options = {
  38. multipleSlots: true,
  39. pureDataPattern: /^options$/,
  40. };
  41. this.properties = props;
  42. this.controlledProps = [
  43. {
  44. key: 'value',
  45. event: 'change',
  46. },
  47. ];
  48. this.data = {
  49. prefix,
  50. name,
  51. stepIndex: 0,
  52. selectedIndexes: [],
  53. selectedValue: [],
  54. scrollTopList: [],
  55. steps: [],
  56. };
  57. this.observers = {
  58. visible(v) {
  59. if (v) {
  60. const $tabs = this.selectComponent('#tabs');
  61. $tabs === null || $tabs === void 0 ? void 0 : $tabs.setTrack();
  62. this.updateScrollTop();
  63. this.initWithValue();
  64. }
  65. },
  66. value() {
  67. this.initWithValue();
  68. },
  69. options() {
  70. const { selectedValue, steps, items } = this.genItems();
  71. this.setData({
  72. steps,
  73. items,
  74. selectedValue,
  75. stepIndex: items.length - 1,
  76. });
  77. },
  78. selectedIndexes() {
  79. const { selectedValue, steps, items } = this.genItems();
  80. this.setData({
  81. steps,
  82. selectedValue,
  83. stepIndex: items.length - 1,
  84. });
  85. },
  86. stepIndex() {
  87. return __awaiter(this, void 0, void 0, function* () {
  88. const { visible } = this.data;
  89. if (visible) {
  90. this.updateScrollTop();
  91. }
  92. });
  93. },
  94. };
  95. this.methods = {
  96. initWithValue() {
  97. if (this.data.value != null && this.data.value !== '') {
  98. const selectedIndexes = this.getIndexesByValue(this.data.options, this.data.value);
  99. if (selectedIndexes) {
  100. this.setData({ selectedIndexes });
  101. }
  102. }
  103. else {
  104. this.setData({ selectedIndexes: [] });
  105. }
  106. },
  107. getIndexesByValue(options, value) {
  108. var _a, _b, _c;
  109. const { keys } = this.data;
  110. for (let i = 0, size = options.length; i < size; i += 1) {
  111. const opt = options[i];
  112. if (opt[(_a = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _a !== void 0 ? _a : 'value'] === value) {
  113. return [i];
  114. }
  115. if (opt[(_b = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _b !== void 0 ? _b : 'children']) {
  116. const res = this.getIndexesByValue(opt[(_c = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _c !== void 0 ? _c : 'children'], value);
  117. if (res) {
  118. return [i, ...res];
  119. }
  120. }
  121. }
  122. },
  123. updateScrollTop() {
  124. const { visible, items, selectedIndexes, stepIndex } = this.data;
  125. if (visible) {
  126. getRect(this, '.cascader-radio-group-0').then((rect) => {
  127. var _a;
  128. const eachRadioHeight = rect.height / ((_a = items[0]) === null || _a === void 0 ? void 0 : _a.length);
  129. this.setData({
  130. [`scrollTopList[${stepIndex}]`]: eachRadioHeight * selectedIndexes[stepIndex],
  131. });
  132. });
  133. }
  134. },
  135. hide(trigger) {
  136. this.setData({ visible: false });
  137. this.triggerEvent('close', { trigger: trigger });
  138. },
  139. onVisibleChange() {
  140. this.hide('overlay');
  141. },
  142. onClose() {
  143. this.hide('close-btn');
  144. },
  145. onStepClick(e) {
  146. const { index } = e.currentTarget.dataset;
  147. this.setData({ stepIndex: index });
  148. },
  149. onTabChange(e) {
  150. const { value } = e.detail;
  151. this.setData({
  152. stepIndex: value,
  153. });
  154. },
  155. genItems() {
  156. var _a, _b, _c, _d, _e;
  157. const { options, selectedIndexes, keys, placeholder } = this.data;
  158. const selectedValue = [];
  159. const steps = [];
  160. const items = [parseOptions(options, keys)];
  161. if (options.length > 0) {
  162. let current = options;
  163. for (let i = 0, size = selectedIndexes.length; i < size; i += 1) {
  164. const index = selectedIndexes[i];
  165. const next = current[index];
  166. current = next[(_a = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _a !== void 0 ? _a : 'children'];
  167. selectedValue.push(next[(_b = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _b !== void 0 ? _b : 'value']);
  168. steps.push(next[(_c = keys === null || keys === void 0 ? void 0 : keys.label) !== null && _c !== void 0 ? _c : 'label']);
  169. if (next[(_d = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _d !== void 0 ? _d : 'children']) {
  170. items.push(parseOptions(next[(_e = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _e !== void 0 ? _e : 'children'], keys));
  171. }
  172. }
  173. }
  174. if (steps.length < items.length) {
  175. steps.push(placeholder);
  176. }
  177. return {
  178. selectedValue,
  179. steps,
  180. items,
  181. };
  182. },
  183. handleSelect(e) {
  184. var _a, _b, _c, _d;
  185. const { level } = e.target.dataset;
  186. const { value } = e.detail;
  187. const { selectedIndexes, items, keys, options } = this.data;
  188. const index = items[level].findIndex((item) => { var _a; return item[(_a = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _a !== void 0 ? _a : 'value'] === value; });
  189. let item = selectedIndexes.slice(0, level).reduce((acc, item, index) => {
  190. var _a;
  191. if (index === 0) {
  192. return acc[item];
  193. }
  194. return acc[(_a = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _a !== void 0 ? _a : 'children'][item];
  195. }, options);
  196. if (level === 0) {
  197. item = item[index];
  198. }
  199. else {
  200. item = item[(_a = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _a !== void 0 ? _a : 'children'][index];
  201. }
  202. if (item.disabled) {
  203. return;
  204. }
  205. selectedIndexes[level] = index;
  206. selectedIndexes.length = level + 1;
  207. this.triggerEvent('pick', { value: item[(_b = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _b !== void 0 ? _b : 'value'], index, level });
  208. const { items: newItems } = this.genItems();
  209. if ((_d = item === null || item === void 0 ? void 0 : item[(_c = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _c !== void 0 ? _c : 'children']) === null || _d === void 0 ? void 0 : _d.length) {
  210. this.setData({
  211. selectedIndexes,
  212. [`items[${level + 1}]`]: newItems[level + 1],
  213. });
  214. }
  215. else {
  216. this.setData({
  217. selectedIndexes,
  218. }, () => {
  219. var _a;
  220. const { items } = this.data;
  221. this._trigger('change', {
  222. value: item[(_a = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _a !== void 0 ? _a : 'value'],
  223. selectedOptions: items.map((item, index) => item[selectedIndexes[index]]),
  224. });
  225. });
  226. this.hide('finish');
  227. }
  228. },
  229. };
  230. }
  231. };
  232. Cascader = __decorate([
  233. wxComponent()
  234. ], Cascader);
  235. export default Cascader;