cascader.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. const defaultState = {
  34. contentHeight: 0,
  35. stepHeight: 0,
  36. tabsHeight: 0,
  37. subTitlesHeight: 0,
  38. stepsInitHeight: 0,
  39. };
  40. let Cascader = class Cascader extends SuperComponent {
  41. constructor() {
  42. super(...arguments);
  43. this.externalClasses = [`${prefix}-class`];
  44. this.options = {
  45. multipleSlots: true,
  46. pureDataPattern: /^options$/,
  47. };
  48. this.properties = props;
  49. this.controlledProps = [
  50. {
  51. key: 'value',
  52. event: 'change',
  53. },
  54. ];
  55. this.state = Object.assign({}, defaultState);
  56. this.data = {
  57. prefix,
  58. name,
  59. stepIndex: 0,
  60. selectedIndexes: [],
  61. selectedValue: [],
  62. scrollTopList: [],
  63. steps: [],
  64. _optionsHeight: 0,
  65. };
  66. this.observers = {
  67. visible(v) {
  68. if (v) {
  69. const $tabs = this.selectComponent('#tabs');
  70. $tabs === null || $tabs === void 0 ? void 0 : $tabs.setTrack();
  71. $tabs === null || $tabs === void 0 ? void 0 : $tabs.getTabHeight().then((res) => {
  72. this.state.tabsHeight = res.height;
  73. });
  74. this.initOptionsHeight(this.data.steps.length);
  75. this.updateScrollTop();
  76. this.initWithValue();
  77. }
  78. else {
  79. this.state = Object.assign({}, defaultState);
  80. }
  81. },
  82. value() {
  83. this.initWithValue();
  84. },
  85. options() {
  86. const { selectedValue, steps, items } = this.genItems();
  87. this.setData({
  88. steps,
  89. items,
  90. selectedValue,
  91. stepIndex: items.length - 1,
  92. });
  93. },
  94. selectedIndexes() {
  95. const { visible, theme } = this.properties;
  96. const { selectedValue, steps, items } = this.genItems();
  97. const setData = {
  98. steps,
  99. selectedValue,
  100. stepIndex: items.length - 1,
  101. };
  102. if (JSON.stringify(items) !== JSON.stringify(this.data.items)) {
  103. Object.assign(setData, { items });
  104. }
  105. this.setData(setData);
  106. if (visible && theme === 'step') {
  107. this.updateOptionsHeight(steps.length);
  108. }
  109. },
  110. stepIndex() {
  111. return __awaiter(this, void 0, void 0, function* () {
  112. const { visible } = this.data;
  113. if (visible) {
  114. this.updateScrollTop();
  115. }
  116. });
  117. },
  118. };
  119. this.methods = {
  120. updateOptionsHeight(steps) {
  121. const { contentHeight, stepsInitHeight, stepHeight, subTitlesHeight } = this.state;
  122. this.setData({
  123. _optionsHeight: contentHeight - stepsInitHeight - subTitlesHeight - (steps - 1) * stepHeight,
  124. });
  125. },
  126. initOptionsHeight(steps) {
  127. return __awaiter(this, void 0, void 0, function* () {
  128. const { theme, subTitles } = this.properties;
  129. const { height } = yield getRect(this, `.${name}__content`);
  130. this.state.contentHeight = height;
  131. if (theme === 'step') {
  132. yield Promise.all([getRect(this, `.${name}__steps`), getRect(this, `.${name}__step`)]).then(([stepsRect, stepRect]) => {
  133. this.state.stepsInitHeight = stepsRect.height - (steps - 1) * stepRect.height;
  134. this.state.stepHeight = stepRect.height;
  135. });
  136. }
  137. if (subTitles.length > 0) {
  138. const { height } = yield getRect(this, `.${name}__options-title`);
  139. this.state.subTitlesHeight = height;
  140. }
  141. const optionsInitHeight = this.state.contentHeight - this.state.subTitlesHeight;
  142. this.setData({
  143. _optionsHeight: theme === 'step'
  144. ? optionsInitHeight - this.state.stepsInitHeight - (steps - 1) * this.state.stepHeight
  145. : optionsInitHeight - this.state.tabsHeight,
  146. });
  147. });
  148. },
  149. initWithValue() {
  150. if (this.data.value != null && this.data.value !== '') {
  151. const selectedIndexes = this.getIndexesByValue(this.data.options, this.data.value);
  152. if (selectedIndexes) {
  153. this.setData({ selectedIndexes });
  154. }
  155. }
  156. else {
  157. this.setData({ selectedIndexes: [] });
  158. }
  159. },
  160. getIndexesByValue(options, value) {
  161. var _a, _b, _c;
  162. const { keys } = this.data;
  163. for (let i = 0, size = options.length; i < size; i += 1) {
  164. const opt = options[i];
  165. if (opt[(_a = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _a !== void 0 ? _a : 'value'] === value) {
  166. return [i];
  167. }
  168. if (opt[(_b = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _b !== void 0 ? _b : 'children']) {
  169. const res = this.getIndexesByValue(opt[(_c = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _c !== void 0 ? _c : 'children'], value);
  170. if (res) {
  171. return [i, ...res];
  172. }
  173. }
  174. }
  175. },
  176. updateScrollTop() {
  177. const { visible, items, selectedIndexes, stepIndex } = this.data;
  178. if (visible) {
  179. getRect(this, '.cascader-radio-group-0').then((rect) => {
  180. var _a;
  181. const eachRadioHeight = rect.height / ((_a = items[0]) === null || _a === void 0 ? void 0 : _a.length);
  182. this.setData({
  183. [`scrollTopList[${stepIndex}]`]: eachRadioHeight * selectedIndexes[stepIndex],
  184. });
  185. });
  186. }
  187. },
  188. hide(trigger) {
  189. this.setData({ visible: false });
  190. this.triggerEvent('close', { trigger: trigger });
  191. },
  192. onVisibleChange() {
  193. this.hide('overlay');
  194. },
  195. onClose() {
  196. if (this.data.checkStrictly) {
  197. this.triggerChange();
  198. }
  199. this.hide('close-btn');
  200. },
  201. onStepClick(e) {
  202. const { index } = e.currentTarget.dataset;
  203. this.setData({ stepIndex: index });
  204. },
  205. onTabChange(e) {
  206. const { value } = e.detail;
  207. this.setData({
  208. stepIndex: value,
  209. });
  210. },
  211. genItems() {
  212. var _a, _b, _c, _d, _e;
  213. const { options, selectedIndexes, keys, placeholder } = this.data;
  214. const selectedValue = [];
  215. const steps = [];
  216. const items = [parseOptions(options, keys)];
  217. if (options.length > 0) {
  218. let current = options;
  219. for (let i = 0, size = selectedIndexes.length; i < size; i += 1) {
  220. const index = selectedIndexes[i];
  221. const next = current[index];
  222. current = next[(_a = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _a !== void 0 ? _a : 'children'];
  223. selectedValue.push(next[(_b = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _b !== void 0 ? _b : 'value']);
  224. steps.push(next[(_c = keys === null || keys === void 0 ? void 0 : keys.label) !== null && _c !== void 0 ? _c : 'label']);
  225. if (next[(_d = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _d !== void 0 ? _d : 'children']) {
  226. items.push(parseOptions(next[(_e = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _e !== void 0 ? _e : 'children'], keys));
  227. }
  228. }
  229. }
  230. if (steps.length < items.length) {
  231. steps.push(placeholder);
  232. }
  233. return {
  234. selectedValue,
  235. steps,
  236. items,
  237. };
  238. },
  239. handleSelect(e) {
  240. var _a, _b, _c, _d, _e;
  241. const { level } = e.target.dataset;
  242. const { value } = e.detail;
  243. const { checkStrictly } = this.properties;
  244. const { selectedIndexes, items, keys, options, selectedValue } = this.data;
  245. 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; });
  246. let item = selectedIndexes.slice(0, level).reduce((acc, item, index) => {
  247. var _a;
  248. if (index === 0) {
  249. return acc[item];
  250. }
  251. return acc[(_a = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _a !== void 0 ? _a : 'children'][item];
  252. }, options);
  253. if (level === 0) {
  254. item = item[index];
  255. }
  256. else {
  257. item = item[(_a = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _a !== void 0 ? _a : 'children'][index];
  258. }
  259. if (item.disabled) {
  260. return;
  261. }
  262. this.triggerEvent('pick', {
  263. value: item[(_b = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _b !== void 0 ? _b : 'value'],
  264. label: item[(_c = keys === null || keys === void 0 ? void 0 : keys.label) !== null && _c !== void 0 ? _c : 'label'],
  265. index,
  266. level,
  267. });
  268. selectedIndexes[level] = index;
  269. if (checkStrictly && selectedValue.includes(String(value))) {
  270. selectedIndexes.length = level;
  271. this.setData({ selectedIndexes });
  272. return;
  273. }
  274. selectedIndexes.length = level + 1;
  275. const { items: newItems } = this.genItems();
  276. if ((_e = item === null || item === void 0 ? void 0 : item[(_d = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _d !== void 0 ? _d : 'children']) === null || _e === void 0 ? void 0 : _e.length) {
  277. this.setData({
  278. selectedIndexes,
  279. [`items[${level + 1}]`]: newItems[level + 1],
  280. });
  281. }
  282. else {
  283. this.setData({
  284. selectedIndexes,
  285. }, this.triggerChange);
  286. this.hide('finish');
  287. }
  288. },
  289. triggerChange() {
  290. var _a;
  291. const { items, selectedValue, selectedIndexes } = this.data;
  292. this._trigger('change', {
  293. value: (_a = selectedValue[selectedValue.length - 1]) !== null && _a !== void 0 ? _a : '',
  294. selectedOptions: items.map((item, index) => item[selectedIndexes[index]]).filter(Boolean),
  295. });
  296. },
  297. };
  298. }
  299. };
  300. Cascader = __decorate([
  301. wxComponent()
  302. ], Cascader);
  303. export default Cascader;