| | |
| | | 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; |
| | | }; |
| | | import { SuperComponent, wxComponent } from '../common/src/index'; |
| | | import config from '../common/config'; |
| | | import props from './props'; |
| | | const { prefix } = config; |
| | | const name = `${prefix}-checkbox-group`; |
| | | let CheckBoxGroup = class CheckBoxGroup extends SuperComponent { |
| | | constructor() { |
| | | super(...arguments); |
| | | this.externalClasses = [`${prefix}-class`]; |
| | | this.relations = { |
| | | '../checkbox/checkbox': { |
| | | type: 'descendant', |
| | | }, |
| | | }; |
| | | this.data = { |
| | | prefix, |
| | | classPrefix: name, |
| | | checkboxOptions: [], |
| | | }; |
| | | this.properties = props; |
| | | this.observers = { |
| | | value() { |
| | | this.updateChildren(); |
| | | }, |
| | | options() { |
| | | this.initWithOptions(); |
| | | }, |
| | | disabled(v) { |
| | | var _a; |
| | | if ((_a = this.data.options) === null || _a === void 0 ? void 0 : _a.length) { |
| | | this.initWithOptions(); |
| | | return; |
| | | } |
| | | this.getChildren().forEach((item) => { |
| | | item.setDisabled(v); |
| | | }); |
| | | }, |
| | | }; |
| | | this.lifetimes = { |
| | | ready() { |
| | | this.setCheckall(); |
| | | }, |
| | | }; |
| | | this.controlledProps = [ |
| | | { |
| | | key: 'value', |
| | | event: 'change', |
| | | }, |
| | | ]; |
| | | this.$checkAll = null; |
| | | this.methods = { |
| | | getChildren() { |
| | | let items = this.$children; |
| | | if (!items.length) { |
| | | items = this.selectAllComponents(`.${prefix}-checkbox-option`); |
| | | } |
| | | return items || []; |
| | | }, |
| | | updateChildren() { |
| | | const items = this.getChildren(); |
| | | const { value } = this.data; |
| | | if (items.length > 0) { |
| | | items.forEach((item) => { |
| | | !item.data.checkAll && |
| | | item.setData({ |
| | | checked: value === null || value === void 0 ? void 0 : value.includes(item.data.value), |
| | | }); |
| | | }); |
| | | if (items.some((item) => item.data.checkAll)) { |
| | | this.setCheckall(); |
| | | } |
| | | } |
| | | }, |
| | | updateValue({ value, checked, checkAll, item, indeterminate }) { |
| | | let { value: newValue } = this.data; |
| | | const { max } = this.data; |
| | | const keySet = new Set(this.getChildren().map((item) => item.data.value)); |
| | | newValue = newValue.filter((value) => keySet.has(value)); |
| | | if (max && checked && newValue.length === max) |
| | | return; |
| | | if (checkAll) { |
| | | const items = this.getChildren(); |
| | | newValue = |
| | | !checked && indeterminate |
| | | ? items |
| | | .filter(({ data }) => !(data.disabled && !newValue.includes(data.value))) |
| | | .map((item) => item.data.value) |
| | | : items |
| | | .filter(({ data }) => { |
| | | if (data.disabled) { |
| | | return newValue.includes(data.value); |
| | | } |
| | | return checked && !data.checkAll; |
| | | }) |
| | | .map(({ data }) => data.value); |
| | | } |
| | | else if (checked) { |
| | | newValue = newValue.concat(value); |
| | | } |
| | | else { |
| | | const index = newValue.findIndex((v) => v === value); |
| | | newValue.splice(index, 1); |
| | | } |
| | | this._trigger('change', { value: newValue, context: item }); |
| | | }, |
| | | initWithOptions() { |
| | | const { options, value } = this.data; |
| | | if (!(options === null || options === void 0 ? void 0 : options.length) || !Array.isArray(options)) |
| | | return; |
| | | const checkboxOptions = options.map((item) => { |
| | | const isLabel = ['number', 'string'].includes(typeof item); |
| | | return isLabel |
| | | ? { |
| | | label: `${item}`, |
| | | value: item, |
| | | checked: value === null || value === void 0 ? void 0 : value.includes(item), |
| | | } |
| | | : Object.assign(Object.assign({}, item), { checked: value === null || value === void 0 ? void 0 : value.includes(item.value) }); |
| | | }); |
| | | this.setData({ |
| | | checkboxOptions, |
| | | }); |
| | | }, |
| | | handleInnerChildChange(e) { |
| | | var _a; |
| | | const { item } = e.target.dataset; |
| | | const { checked } = e.detail; |
| | | const rect = {}; |
| | | if (item.checkAll) { |
| | | rect.indeterminate = (_a = this.$checkAll) === null || _a === void 0 ? void 0 : _a.data.indeterminate; |
| | | } |
| | | this.updateValue(Object.assign(Object.assign(Object.assign({}, item), { checked, item }), rect)); |
| | | }, |
| | | setCheckall() { |
| | | const items = this.getChildren(); |
| | | if (!this.$checkAll) { |
| | | this.$checkAll = items.find((item) => item.data.checkAll); |
| | | } |
| | | if (!this.$checkAll) |
| | | return; |
| | | const { value } = this.data; |
| | | const valueSet = new Set(value === null || value === void 0 ? void 0 : value.filter((val) => val !== this.$checkAll.data.value)); |
| | | const isCheckall = items.every((item) => (item.data.checkAll ? true : valueSet.has(item.data.value))); |
| | | this.$checkAll.setData({ |
| | | checked: valueSet.size > 0, |
| | | indeterminate: !isCheckall, |
| | | }); |
| | | }, |
| | | }; |
| | | } |
| | | }; |
| | | CheckBoxGroup = __decorate([ |
| | | wxComponent() |
| | | ], CheckBoxGroup); |
| | | export default CheckBoxGroup; |
| | | import{__decorate}from"tslib";import{SuperComponent,wxComponent}from"../common/src/index";import config from"../common/config";import props from"./props";const{prefix:prefix}=config,name=`${prefix}-checkbox-group`;let CheckBoxGroup=class extends SuperComponent{constructor(){super(...arguments),this.externalClasses=[`${prefix}-class`],this.behaviors=["wx://form-field"],this.relations={"../checkbox/checkbox":{type:"descendant"}},this.data={prefix:prefix,classPrefix:name,checkboxOptions:[]},this.properties=props,this.observers={value(){this.updateChildren()},options(){this.initWithOptions()},disabled(e){var t;(null===(t=this.data.options)||void 0===t?void 0:t.length)?this.initWithOptions():this.getChildren().forEach(t=>{t.setDisabled(e)})}},this.lifetimes={ready(){this.setCheckall()}},this.controlledProps=[{key:"value",event:"change"}],this.$checkAll=null,this.methods={getChildren(){let e=this.$children;return e.length||(e=this.selectAllComponents(`.${prefix}-checkbox-option`)),e||[]},updateChildren(){const e=this.getChildren(),{value:t}=this.data;e.length>0&&(e.forEach(e=>{!e.data.checkAll&&e.setData({checked:null==t?void 0:t.includes(e.data.value)})}),e.some(e=>e.data.checkAll)&&this.setCheckall())},updateValue({value:e,checked:t,checkAll:i,item:l,indeterminate:a}){let{value:s}=this.data;const{max:n}=this.data,c=new Set(this.getChildren().map(e=>e.data.value));if(s=s.filter(e=>c.has(e)),!n||!t||s.length!==n){if(i){const e=this.getChildren();s=!t&&a?e.filter(({data:e})=>!(e.disabled&&!s.includes(e.value))).map(e=>e.data.value):e.filter(({data:e})=>e.disabled?s.includes(e.value):t&&!e.checkAll).map(({data:e})=>e.value)}else if(t)s=s.concat(e);else{const t=s.findIndex(t=>t===e);s.splice(t,1)}this._trigger("change",{value:s,context:l})}},initWithOptions(){const{options:e,value:t,keys:i}=this.data;if(!(null==e?void 0:e.length)||!Array.isArray(e))return;const l=e.map(e=>{var l,a,s;return["number","string"].includes(typeof e)?{label:`${e}`,value:e,checked:null==t?void 0:t.includes(e)}:Object.assign(Object.assign({},e),{label:e[null!==(l=null==i?void 0:i.label)&&void 0!==l?l:"label"],value:e[null!==(a=null==i?void 0:i.value)&&void 0!==a?a:"value"],checked:null==t?void 0:t.includes(e[null!==(s=null==i?void 0:i.value)&&void 0!==s?s:"value"])})});this.setData({checkboxOptions:l})},handleInnerChildChange(e){var t;const{item:i}=e.target.dataset,{checked:l}=e.detail,a={};i.checkAll&&(a.indeterminate=null===(t=this.$checkAll)||void 0===t?void 0:t.data.indeterminate),this.updateValue(Object.assign(Object.assign(Object.assign({},i),{checked:l,item:i}),a))},setCheckall(){const e=this.getChildren();if(this.$checkAll||(this.$checkAll=e.find(e=>e.data.checkAll)),!this.$checkAll)return;const{value:t}=this.data,i=new Set(null==t?void 0:t.filter(e=>e!==this.$checkAll.data.value)),l=e.every(e=>!!e.data.checkAll||i.has(e.data.value));this.$checkAll.setData({checked:i.size>0,indeterminate:!l})}}}};CheckBoxGroup=__decorate([wxComponent()],CheckBoxGroup);export default CheckBoxGroup; |