| | |
| | | var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { |
| | | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; |
| | | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); |
| | | 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; |
| | | return c > 3 && r && Object.defineProperty(target, key, r), r; |
| | | }; |
| | | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| | | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| | | return new (P || (P = Promise))(function (resolve, reject) { |
| | | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| | | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| | | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| | | step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| | | }); |
| | | }; |
| | | import { SuperComponent, wxComponent } from '../common/src/index'; |
| | | import config from '../common/config'; |
| | | import props from './props'; |
| | | import { getRect } from '../common/utils'; |
| | | const { prefix } = config; |
| | | const name = `${prefix}-cascader`; |
| | | const defaultOptionLabel = '选择选项'; |
| | | let Cascader = class Cascader extends SuperComponent { |
| | | constructor() { |
| | | super(...arguments); |
| | | this.externalClasses = [`${prefix}-class`]; |
| | | this.options = { |
| | | multipleSlots: true, |
| | | }; |
| | | this.properties = props; |
| | | this.controlledProps = [ |
| | | { |
| | | key: 'value', |
| | | event: 'change', |
| | | }, |
| | | ]; |
| | | this.data = { |
| | | prefix, |
| | | name, |
| | | stepIndex: 0, |
| | | selectedIndexes: [], |
| | | selectedValue: [], |
| | | defaultOptionLabel, |
| | | scrollTopList: [], |
| | | steps: [defaultOptionLabel], |
| | | }; |
| | | this.observers = { |
| | | visible(v) { |
| | | if (v) { |
| | | const $tabs = this.selectComponent('#tabs'); |
| | | $tabs === null || $tabs === void 0 ? void 0 : $tabs.setTrack(); |
| | | this.updateScrollTop(); |
| | | this.initWithValue(); |
| | | } |
| | | }, |
| | | 'selectedIndexes, options'() { |
| | | var _a, _b, _c, _d; |
| | | const { options, selectedIndexes, keys } = this.data; |
| | | const selectedValue = []; |
| | | const steps = []; |
| | | const items = [options]; |
| | | if (options.length > 0) { |
| | | for (let i = 0, size = selectedIndexes.length; i < size; i += 1) { |
| | | const index = selectedIndexes[i]; |
| | | const next = items[i][index]; |
| | | selectedValue.push(next[(_a = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _a !== void 0 ? _a : 'value']); |
| | | steps.push(next[(_b = keys === null || keys === void 0 ? void 0 : keys.label) !== null && _b !== void 0 ? _b : 'label']); |
| | | if (next[(_c = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _c !== void 0 ? _c : 'children']) { |
| | | items.push(next[(_d = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _d !== void 0 ? _d : 'children']); |
| | | } |
| | | } |
| | | } |
| | | if (steps.length < items.length) { |
| | | steps.push(defaultOptionLabel); |
| | | } |
| | | this.setData({ |
| | | steps, |
| | | items, |
| | | selectedValue, |
| | | stepIndex: items.length - 1, |
| | | }); |
| | | }, |
| | | stepIndex() { |
| | | return __awaiter(this, void 0, void 0, function* () { |
| | | const { visible } = this.data; |
| | | if (visible) { |
| | | this.updateScrollTop(); |
| | | } |
| | | }); |
| | | }, |
| | | }; |
| | | this.methods = { |
| | | initWithValue() { |
| | | if (this.data.value != null && this.data.value !== '') { |
| | | const selectedIndexes = this.getIndexesByValue(this.data.options, this.data.value); |
| | | if (selectedIndexes) { |
| | | this.setData({ selectedIndexes }); |
| | | } |
| | | } |
| | | else { |
| | | this.setData({ selectedIndexes: [] }); |
| | | } |
| | | }, |
| | | getIndexesByValue(options, value) { |
| | | var _a, _b, _c; |
| | | const { keys } = this.data; |
| | | for (let i = 0, size = options.length; i < size; i += 1) { |
| | | const opt = options[i]; |
| | | if (opt[(_a = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _a !== void 0 ? _a : 'value'] === value) { |
| | | return [i]; |
| | | } |
| | | if (opt[(_b = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _b !== void 0 ? _b : 'children']) { |
| | | const res = this.getIndexesByValue(opt[(_c = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _c !== void 0 ? _c : 'children'], value); |
| | | if (res) { |
| | | return [i, ...res]; |
| | | } |
| | | } |
| | | } |
| | | }, |
| | | updateScrollTop() { |
| | | const { visible, items, selectedIndexes, stepIndex } = this.data; |
| | | if (visible) { |
| | | getRect(this, '.cascader-radio-group-0').then((rect) => { |
| | | var _a; |
| | | const eachRadioHeight = rect.height / ((_a = items[0]) === null || _a === void 0 ? void 0 : _a.length); |
| | | this.setData({ |
| | | [`scrollTopList[${stepIndex}]`]: eachRadioHeight * selectedIndexes[stepIndex], |
| | | }); |
| | | }); |
| | | } |
| | | }, |
| | | hide(trigger) { |
| | | this.setData({ visible: false }); |
| | | this.triggerEvent('close', { trigger: trigger }); |
| | | }, |
| | | onVisibleChange() { |
| | | this.hide('overlay'); |
| | | }, |
| | | onClose() { |
| | | this.hide('close-btn'); |
| | | }, |
| | | onStepClick(e) { |
| | | const { index } = e.currentTarget.dataset; |
| | | this.setData({ stepIndex: index }); |
| | | }, |
| | | onTabChange(e) { |
| | | const { value } = e.detail; |
| | | this.setData({ |
| | | stepIndex: value, |
| | | }); |
| | | }, |
| | | handleSelect(e) { |
| | | var _a, _b, _c; |
| | | const { level } = e.target.dataset; |
| | | const { value } = e.detail; |
| | | const { selectedIndexes, items, keys } = this.data; |
| | | 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; }); |
| | | const item = items[level][index]; |
| | | if (item.disabled) { |
| | | return; |
| | | } |
| | | selectedIndexes[level] = index; |
| | | selectedIndexes.length = level + 1; |
| | | this.triggerEvent('pick', { value: item[(_a = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _a !== void 0 ? _a : 'value'], index, level }); |
| | | if ((_c = item === null || item === void 0 ? void 0 : item[(_b = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _b !== void 0 ? _b : 'children']) === null || _c === void 0 ? void 0 : _c.length) { |
| | | this.setData({ selectedIndexes }); |
| | | } |
| | | else { |
| | | this.setData({ selectedIndexes }, () => { |
| | | var _a; |
| | | const { items } = this.data; |
| | | this._trigger('change', { |
| | | value: item[(_a = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _a !== void 0 ? _a : 'value'], |
| | | selectedOptions: items.map((item, index) => item[selectedIndexes[index]]), |
| | | }); |
| | | }); |
| | | this.hide('finish'); |
| | | } |
| | | }, |
| | | }; |
| | | } |
| | | }; |
| | | Cascader = __decorate([ |
| | | wxComponent() |
| | | ], Cascader); |
| | | export default Cascader; |
| | | import{__awaiter,__decorate}from"tslib";import{SuperComponent,wxComponent}from"../common/src/index";import config from"../common/config";import props from"./props";import{getRect}from"../common/utils";const{prefix:prefix}=config,name=`${prefix}-cascader`;function parseOptions(e,t){var s,i,l;const n=null!==(s=null==t?void 0:t.label)&&void 0!==s?s:"label",a=null!==(i=null==t?void 0:t.value)&&void 0!==i?i:"value",o=null!==(l=null==t?void 0:t.disabled)&&void 0!==l?l:"disabled";return e.map(e=>({[n]:e[n],[a]:e[a],[o]:e[o]}))}const defaultState={contentHeight:0,stepHeight:0,tabsHeight:0,subTitlesHeight:0,stepsInitHeight:0};let Cascader=class extends SuperComponent{constructor(){super(...arguments),this.externalClasses=[`${prefix}-class`],this.options={multipleSlots:!0,pureDataPattern:/^options$/},this.properties=props,this.controlledProps=[{key:"value",event:"change"}],this.state=Object.assign({},defaultState),this.data={prefix:prefix,name:name,stepIndex:0,selectedIndexes:[],selectedValue:[],scrollTopList:[],steps:[],_optionsHeight:0},this.observers={visible(e){if(e){const e=this.selectComponent("#tabs");null==e||e.setTrack(),null==e||e.getTabHeight().then(e=>{this.state.tabsHeight=e.height}),this.initOptionsHeight(this.data.steps.length),this.updateScrollTop(),this.initWithValue()}else this.state=Object.assign({},defaultState)},value(){this.initWithValue()},options(){const{selectedValue:e,steps:t,items:s}=this.genItems();this.setData({steps:t,items:s,selectedValue:e,stepIndex:s.length-1})},selectedIndexes(){const{visible:e,theme:t}=this.properties,{selectedValue:s,steps:i,items:l}=this.genItems(),n={steps:i,selectedValue:s,stepIndex:l.length-1};JSON.stringify(l)!==JSON.stringify(this.data.items)&&Object.assign(n,{items:l}),this.setData(n),e&&"step"===t&&this.updateOptionsHeight(i.length)},stepIndex(){return __awaiter(this,void 0,void 0,function*(){const{visible:e}=this.data;e&&this.updateScrollTop()})}},this.methods={updateOptionsHeight(e){const{contentHeight:t,stepsInitHeight:s,stepHeight:i,subTitlesHeight:l}=this.state;this.setData({_optionsHeight:t-s-l-(e-1)*i})},initOptionsHeight(e){return __awaiter(this,void 0,void 0,function*(){const{theme:t,subTitles:s}=this.properties,{height:i}=yield getRect(this,`.${name}__content`);if(this.state.contentHeight=i,"step"===t&&(yield Promise.all([getRect(this,`.${name}__steps`),getRect(this,`.${name}__step`)]).then(([t,s])=>{this.state.stepsInitHeight=t.height-(e-1)*s.height,this.state.stepHeight=s.height})),s.length>0){const{height:e}=yield getRect(this,`.${name}__options-title`);this.state.subTitlesHeight=e}const l=this.state.contentHeight-this.state.subTitlesHeight;this.setData({_optionsHeight:"step"===t?l-this.state.stepsInitHeight-(e-1)*this.state.stepHeight:l-this.state.tabsHeight})})},initWithValue(){if(null!=this.data.value&&""!==this.data.value){const e=this.getIndexesByValue(this.data.options,this.data.value);e&&this.setData({selectedIndexes:e})}else this.setData({selectedIndexes:[]})},getIndexesByValue(e,t){var s,i,l;const{keys:n}=this.data;for(let a=0,o=e.length;a<o;a+=1){const o=e[a];if(o[null!==(s=null==n?void 0:n.value)&&void 0!==s?s:"value"]===t)return[a];if(o[null!==(i=null==n?void 0:n.children)&&void 0!==i?i:"children"]){const e=this.getIndexesByValue(o[null!==(l=null==n?void 0:n.children)&&void 0!==l?l:"children"],t);if(e)return[a,...e]}}},updateScrollTop(){const{visible:e,items:t,selectedIndexes:s,stepIndex:i}=this.data;e&&getRect(this,".cascader-radio-group-0").then(e=>{var l;const n=e.height/(null===(l=t[0])||void 0===l?void 0:l.length);this.setData({[`scrollTopList[${i}]`]:n*s[i]})})},hide(e){this.setData({visible:!1}),this.triggerEvent("close",{trigger:e})},onVisibleChange(){this.hide("overlay")},onClose(){this.data.checkStrictly&&this.triggerChange(),this.hide("close-btn")},onStepClick(e){const{index:t}=e.currentTarget.dataset;this.setData({stepIndex:t})},onTabChange(e){const{value:t}=e.detail;this.setData({stepIndex:t})},genItems(){var e,t,s,i,l;const{options:n,selectedIndexes:a,keys:o,placeholder:h}=this.data,d=[],r=[],c=[parseOptions(n,o)];if(n.length>0){let h=n;for(let n=0,u=a.length;n<u;n+=1){const u=h[a[n]];h=u[null!==(e=null==o?void 0:o.children)&&void 0!==e?e:"children"],d.push(u[null!==(t=null==o?void 0:o.value)&&void 0!==t?t:"value"]),r.push(u[null!==(s=null==o?void 0:o.label)&&void 0!==s?s:"label"]),u[null!==(i=null==o?void 0:o.children)&&void 0!==i?i:"children"]&&c.push(parseOptions(u[null!==(l=null==o?void 0:o.children)&&void 0!==l?l:"children"],o))}}return r.length<c.length&&r.push(h),{selectedValue:d,steps:r,items:c}},handleSelect(e){var t,s,i,l,n,a;const{level:o}=e.target.dataset,{value:h}=e.detail,{checkStrictly:d}=this.properties,{selectedIndexes:r,items:c,keys:u,options:p,selectedValue:g}=this.data,v=c[o].findIndex(e=>{var t;return e[null!==(t=null==u?void 0:u.value)&&void 0!==t?t:"value"]===h});let m=r.slice(0,o).reduce((e,t,s)=>{var i;return 0===s?e[t]:e[null!==(i=null==u?void 0:u.children)&&void 0!==i?i:"children"][t]},p);if(m=0===o?m[v]:m[null!==(t=null==u?void 0:u.children)&&void 0!==t?t:"children"][v],m[null!==(s=null==u?void 0:u.disabled)&&void 0!==s?s:"disabled"])return;if(this.triggerEvent("pick",{value:m[null!==(i=null==u?void 0:u.value)&&void 0!==i?i:"value"],label:m[null!==(l=null==u?void 0:u.label)&&void 0!==l?l:"label"],index:v,level:o}),r[o]=v,d&&g.includes(String(h)))return r.length=o,void this.setData({selectedIndexes:r});r.length=o+1;const{items:f}=this.genItems();(null===(a=null==m?void 0:m[null!==(n=null==u?void 0:u.children)&&void 0!==n?n:"children"])||void 0===a?void 0:a.length)>=0?this.setData({selectedIndexes:r,[`items[${o+1}]`]:f[o+1]}):(this.setData({selectedIndexes:r},this.triggerChange),this.hide("finish"))},triggerChange(){var e;const{items:t,selectedValue:s,selectedIndexes:i}=this.data;this._trigger("change",{value:null!==(e=s[s.length-1])&&void 0!==e?e:"",selectedOptions:t.map((e,t)=>e[i[t]]).filter(Boolean)})}}}};Cascader=__decorate([wxComponent()],Cascader);export default Cascader; |