project init & fix ui

This commit is contained in:
2023-01-06 14:24:11 +08:00
parent abc4f65a8e
commit ac9b479805
866 changed files with 39916 additions and 53 deletions

View File

@@ -0,0 +1,59 @@
---
title: checkbox-group
description: 组合多选框
spline: form
isComponent: true
---
### 特性及兼容性
## 引入
### 引入组件
`app.json``page.json` 中引入组件:
```json
"usingComponents": {
"t-checkbox": "tdesign-miniprogram/checkbox/checkbox",
"t-checkbox-group": "tdesign-miniprogram/checkbox-group/checkbox-group"
}
```
## 用法
### 组件方式
```html
<!-- page.wxml -->
<t-checkbox-group defaultValue="checkbox1" bind:change="onChange">
<t-checkbox title="单行标题" value="checkbox1" />
<t-checkbox title="单行标题" label="辅助信息" value="checkbox2" />
</t-checkbox-group>
```
<t-checkbox title="单行标题" value="checkbox1" defaultChecked="{{true}}"/>
## API
### `<t-checkbox-group>` 组件
组件路径:`tdesign-miniprogram/checkbox-group/checkbox-group`
#### Props
| 属性 | 值类型 | 默认值 | 必传 | 说明 |
| -------- | --------- | ------ | ---- | ---------------------- |
| value | `Array` | `[]` | N | 当前选中项的标识符 |
| name | `String` | - | N | 在表单内提交时的标识符 |
### Slots
| 名称 | 说明 |
| ---- | ----------------- |
| 默认 | `t-checkbox` 组件 |
#### Events
| 事件 | event.detail | 说明 |
| ----------- | -------------------------- | ------------------------ |
| bind:change | {names:当前选中项的标识符} | 当绑定值变化时触发的事件 |

View File

@@ -0,0 +1,69 @@
import { SuperComponent, RelationsOptions } from '../common/src/index';
export default class CheckBoxGroup extends SuperComponent {
externalClasses: string[];
relations: RelationsOptions;
data: {
prefix: string;
classPrefix: string;
checkboxOptions: any[];
};
properties: {
borderless: {
type: BooleanConstructor;
value: boolean;
};
customStyle?: {
type: StringConstructor;
value?: string;
};
disabled?: {
type: BooleanConstructor;
value?: boolean;
};
max?: {
type: NumberConstructor;
value?: number;
};
name?: {
type: StringConstructor;
value?: string;
};
options?: {
type: ArrayConstructor;
value?: import("../checkbox/type").CheckboxOption[];
};
value?: {
type: ArrayConstructor;
value?: import("../checkbox/type").CheckboxGroupValue;
};
defaultValue?: {
type: ArrayConstructor;
value?: import("../checkbox/type").CheckboxGroupValue;
};
};
observers: {
value(): void;
};
lifetimes: {
attached(): void;
ready(): void;
};
controlledProps: {
key: string;
event: string;
}[];
$checkAll: any;
methods: {
getChilds(): any;
updateChildren(): void;
updateValue({ value, checked, checkAll, indeterminate }: {
value: any;
checked: any;
checkAll: any;
indeterminate: any;
}): void;
initWithOptions(): void;
handleInnerChildChange(e: any): void;
setCheckall(): void;
};
}

View File

@@ -0,0 +1,151 @@
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 '../checkbox/checkbox-group-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 = Object.assign(Object.assign({}, props), { borderless: {
type: Boolean,
value: false,
} });
this.observers = {
value() {
this.updateChildren();
},
};
this.lifetimes = {
attached() {
this.initWithOptions();
},
ready() {
this.setCheckall();
},
};
this.controlledProps = [
{
key: 'value',
event: 'change',
},
];
this.$checkAll = null;
this.methods = {
getChilds() {
let items = this.$children;
if (!items.length) {
items = this.selectAllComponents(`.${prefix}-checkbox-option`);
}
return items || [];
},
updateChildren() {
const items = this.getChilds();
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, indeterminate }) {
let { value: newValue } = this.data;
const { max } = this.data;
const keySet = new Set(this.getChilds().map((item) => item.data.value));
newValue = newValue.filter((value) => keySet.has(value));
if (max && checked && newValue.length === max)
return;
if (checkAll) {
const items = this.getChilds();
newValue =
!checked && indeterminate
? items.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 });
},
initWithOptions() {
const { options } = 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,
}
: Object.assign({}, item);
});
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 }), rect));
},
setCheckall() {
const items = this.getChilds();
if (!this.$checkAll) {
this.$checkAll = items.find((item) => item.data.checkAll);
}
if (!this.$checkAll)
return;
const { value } = this.data;
const valueSet = new Set(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;

View File

@@ -0,0 +1,6 @@
{
"component": true,
"usingComponents": {
"t-checkbox": "../checkbox/checkbox"
}
}

View File

@@ -0,0 +1,15 @@
<view class="{{ classPrefix }} {{prefix}}-class" style="{{customStyle}}">
<slot />
<block wx:for="{{checkboxOptions}}" wx:key="value">
<t-checkbox
class="{{prefix}}-checkbox-option"
label="{{item.label || item.text || ''}}"
value="{{item.value || ''}}"
content="{{item.content || ''}}"
check-all="{{item.checkAll}}"
disabled="{{item.disabled}}"
data-item="{{item}}"
bind:change="handleInnerChildChange"
></t-checkbox>
</block>
</view>