bug fix and performance improvements

This commit is contained in:
quantulr
2023-08-18 17:32:18 +08:00
parent 3c6ab550cf
commit e750b9be9c
923 changed files with 46650 additions and 9 deletions

View File

@ -0,0 +1,40 @@
:: BASE_DOC ::
## API
### Cascader Props
name | type | default | description | required
-- | -- | -- | -- | --
close-btn | Boolean / Slot | true | \- | N
keys | Object | - | Typescript`KeysType` | N
options | Array | [] | Typescript`Array<CascaderOption>` | N
sub-titles | Array | [] | Typescript`Array<string>` | N
theme | String | step | optionsstep/tab | N
title | String / Slot | - | \- | N
value | String / Number | null | \- | N
default-value | String / Number | undefined | uncontrolled property | N
visible | Boolean | false | \- | N
### Cascader Events
name | params | description
-- | -- | --
change | `(value: string \| number, selectedOptions: string[])` | `1.0.1`
close | `(trigger: TriggerSource)` | `1.0.1`。[see more ts definition](https://github.com/Tencent/tdesign-miniprogram/tree/develop/src/cascader/type.ts)。<br/>`type TriggerSource = 'overlay' \| 'close-btn' \| 'finish'`<br/>
pick | `(value: string \| number, index: number)` | `1.0.1`
### CSS Variables
The component provides the following CSS variables, which can be used to customize styles.
Name | Default Value | Description
-- | -- | --
--td-cascader-active-color | @brand-color | -
--td-cascader-border-color | @border-color | -
--td-cascader-disabled-color | @font-gray-4 | -
--td-cascader-options-height | 640rpx | -
--td-cascader-options-title-color | @font-gray-3 | -
--td-cascader-step-arrow-color | @font-gray-3 | -
--td-cascader-step-dot-size | 16rpx | -
--td-cascader-step-height | 88rpx | -
--td-cascader-title-color | @font-gray-1 | -
--td-cascder-title-font-size | 36rpx | -

View File

@ -0,0 +1,86 @@
---
title: Cascader 级联选择器
description: 级联选择器适用于有清晰层级结构的数据集合,用户可以通过逐级查看并选择。
spline: form
isComponent: true
---
<div style="background: #ecf2fe; display: flex; align-items: center; line-height: 20px; padding: 14px 24px; border-radius: 3px; color: #555a65">
<svg fill="none" viewBox="0 0 16 16" width="16px" height="16px" style="margin-right: 5px">
<path fill="#0052d9" d="M8 15A7 7 0 108 1a7 7 0 000 14zM7.4 4h1.2v1.2H7.4V4zm.1 2.5h1V12h-1V6.5z" fillOpacity="0.9"></path>
</svg>
该组件于 0.23.0 版本上线,请留意版本。
</div>
## 引入
全局引入,在 miniprogram 根目录下的`app.json`中配置,局部引入,在需要引入的页面或组件的`index.json`中配置。
```json
"usingComponents": {
"t-cascader": "tdesign-miniprogram/cascader/cascader"
}
```
## 代码演示
### 基础用法
{{ base }}
### 选项卡风格
{{ theme-tab }}
### 进阶
#### 带初始值
{{ with-value }}
#### 自定义 keys
{{ keys }}
#### 使用次级标题
{{ with-title }}
## API
### Cascader Props
名称 | 类型 | 默认值 | 说明 | 必传
-- | -- | -- | -- | --
close-btn | Boolean / Slot | true | 关闭按钮 | N
keys | Object | - | 用来定义 value / label 在 `options` 中对应的字段别名。TS 类型:`KeysType` | N
options | Array | [] | 可选项数据源。TS 类型:`Array<CascaderOption>` | N
sub-titles | Array | [] | 每级展示的次标题。TS 类型:`Array<string>` | N
theme | String | step | 展示风格。可选项step/tab | N
title | String / Slot | - | 标题 | N
value | String / Number | null | 选项值 | N
default-value | String / Number | undefined | 选项值。非受控属性 | N
visible | Boolean | false | 是否展示 | N
### Cascader Events
名称 | 参数 | 描述
-- | -- | --
change | `(value: string \| number, selectedOptions: string[])` | `1.0.1`。值发生变更时触发
close | `(trigger: TriggerSource)` | `1.0.1`。关闭时触发。[详细类型定义](https://github.com/Tencent/tdesign-miniprogram/tree/develop/src/cascader/type.ts)。<br/>`type TriggerSource = 'overlay' \| 'close-btn' \| 'finish'`<br/>
pick | `(value: string \| number, index: number)` | `1.0.1`。选择后触发
### CSS 变量
组件提供了下列 CSS 变量,可用于自定义样式。
名称 | 默认值 | 描述
-- | -- | --
--td-cascader-active-color | @brand-color | -
--td-cascader-border-color | @border-color | -
--td-cascader-disabled-color | @font-gray-4 | -
--td-cascader-options-height | 640rpx | -
--td-cascader-options-title-color | @font-gray-3 | -
--td-cascader-step-arrow-color | @font-gray-3 | -
--td-cascader-step-dot-size | 16rpx | -
--td-cascader-step-height | 88rpx | -
--td-cascader-title-color | @font-gray-1 | -
--td-cascder-title-font-size | 36rpx | -

View File

@ -0,0 +1,40 @@
/// <reference types="miniprogram-api-typings" />
import { SuperComponent } from '../common/src/index';
import { TdCascaderProps } from './type';
export interface CascaderProps extends TdCascaderProps {
}
export default class Cascader extends SuperComponent {
externalClasses: string[];
options: WechatMiniprogram.Component.ComponentOptions;
properties: TdCascaderProps<import("../common/common").TreeOptionData>;
controlledProps: {
key: string;
event: string;
}[];
data: {
prefix: string;
name: string;
stepIndex: number;
selectedIndexes: any[];
selectedValue: any[];
defaultOptionLabel: string;
scrollTopList: any[];
steps: string[];
};
observers: {
visible(v: any): void;
'selectedIndexes, options'(): void;
stepIndex(): Promise<void>;
};
methods: {
initWithValue(): void;
getIndexesByValue(options: import("../common/common").TreeOptionData[], value: any): any[];
updateScrollTop(): void;
hide(trigger: any): void;
onVisibleChange(): void;
onClose(): void;
onStepClick(e: any): void;
onTabChange(e: any): void;
handleSelect(e: any): void;
};
}

View File

@ -0,0 +1,186 @@
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;

View File

@ -0,0 +1,10 @@
{
"component": true,
"usingComponents": {
"t-icon": "../icon/icon",
"t-popup": "../popup/popup",
"t-tabs": "../tabs/tabs",
"t-tab-panel": "../tab-panel/tab-panel",
"t-radio-group": "../radio-group/radio-group"
}
}

View File

@ -0,0 +1,64 @@
<wxs src="../common/utils.wxs" module="_" />
<t-popup class="class" visible="{{visible}}" placement="bottom" bind:visible-change="onVisibleChange">
<view style="{{_._style([style, customStyle])}}" class="{{name}}">
<view class="{{name}}__title">
<slot name="title" />
{{title}}
</view>
<view class="{{name}}__close-btn" bind:tap="onClose">
<slot name="close-btn" />
<t-icon wx:if="{{closeBtn}}" size="24" name="close" />
</view>
<view class="{{name}}__content">
<block wx:if="{{steps && steps.length}}">
<view wx:if="{{theme == 'step'}}" class="{{name}}__steps">
<view wx:for="{{steps}}" wx:key="index" class="{{name}}__step" bind:tap="onStepClick" data-index="{{index}}">
<view
class="{{name}}__step-dot {{name}}__step-dot--{{item !== defaultOptionLabel ? 'active' : ''}} {{name}}__step-dot--{{index === steps.length - 1 ? 'last' : ''}}"
></view>
<view class="{{name}}__step-label {{name}}__step-label--{{index === stepIndex ? 'active' : ''}}">
{{ item }}
</view>
<t-icon name="chevron-right" size="22" t-class="{{name}}__step-arrow" />
</view>
</view>
<block wx:if="{{theme == 'tab'}}">
<t-tabs id="tabs" value="{{stepIndex}}" bind:change="onTabChange" space-evenly="{{false}}">
<t-tab-panel wx:for="{{steps}}" wx:key="index" value="{{index}}" label="{{item}}" />
</t-tabs>
</block>
</block>
<view wx:if="{{ subTitles && subTitles[stepIndex] }}" class="{{name}}__options-title"
>{{subTitles[stepIndex]}}</view
>
<view
class="{{name}}__options-container"
style="width: {{items.length + 1}}00vw; transform: translateX(-{{stepIndex}}00vw)"
>
<scroll-view
wx:for="{{items}}"
wx:for-item="options"
wx:key="index"
class="{{name}}__options"
scroll-y
scroll-top="{{scrollTopList[index]}}"
>
<view class="cascader-radio-group-{{index}}">
<t-radio-group
value="{{selectedValue[index]}}"
keys="{{keys}}"
options="{{options}}"
bind:change="handleSelect"
data-level="{{index}}"
placement="right"
icon="line"
borderless
>
</t-radio-group>
</view>
</scroll-view>
</view>
</view>
</view>
</t-popup>

View File

@ -0,0 +1,136 @@
.t-float-left {
float: left;
}
.t-float-right {
float: right;
}
@keyframes tdesign-fade-out {
from {
opacity: 1;
}
to {
opacity: 0;
}
}
.hotspot-expanded.relative {
position: relative;
}
.hotspot-expanded::after {
content: '';
display: block;
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
transform: scale(1.5);
}
.t-cascader {
display: flex;
flex-direction: column;
background-color: #fff;
color: var(--td-cascader-title-color, var(--td-font-gray-1, rgba(0, 0, 0, 0.9)));
border-radius: 24rpx 24rpx 0 0;
--td-radio-icon-checked-color: var(--td-cascader-active-color, var(--td-brand-color, var(--td-primary-color-7, #0052d9)));
--td-tab-item-active-color: var(--td-cascader-active-color, var(--td-brand-color, var(--td-primary-color-7, #0052d9)));
--td-tab-track-color: var(--td-cascader-active-color, var(--td-brand-color, var(--td-primary-color-7, #0052d9)));
}
.t-cascader__close-btn {
right: 16px;
top: 12px;
position: absolute;
}
.t-cascader__title {
position: relative;
font-weight: 700;
text-align: center;
line-height: 48px;
font-size: var(--td-cascder-title-font-size, 36rpx);
}
.t-cascader__content {
width: 100%;
flex: 1;
display: flex;
flex-direction: column;
}
.t-cascader__options {
width: 100vw;
height: var(--td-cascader-options-height, 640rpx);
}
.t-cascader__options-title {
margin-top: 40rpx;
color: var(--td-cascader-options-title-color, var(--td-font-gray-3, rgba(0, 0, 0, 0.4)));
font-size: 28rpx;
line-height: 44rpx;
padding-left: 16px;
}
.t-cascader__options-content {
flex: 1;
height: 100%;
overflow: auto;
padding-left: 16px;
}
.t-cascader__options-container {
display: flex;
transition: all ease 0.3s;
}
.t-cascader__step {
display: flex;
align-items: center;
height: var(--td-cascader-step-height, 88rpx);
}
.t-cascader__steps {
padding: 0 32rpx 10rpx;
position: relative;
}
.t-cascader__steps::after {
content: '';
display: block;
position: absolute;
top: unset;
bottom: 0;
left: unset;
right: unset;
background-color: var(--td-cascader-border-color, var(--td-border-color, var(--td-gray-color-3, #e7e7e7)));
}
.t-cascader__steps::after {
height: 1px;
left: 0;
right: 0;
transform: scaleY(0.5);
}
.t-cascader__step-dot {
position: relative;
width: var(--td-cascader-step-dot-size, 16rpx);
height: var(--td-cascader-step-dot-size, 16rpx);
border-radius: 50%;
border: 1px solid var(--td-cascader-active-color, var(--td-brand-color, var(--td-primary-color-7, #0052d9)));
box-sizing: border-box;
}
.t-cascader__step-dot:not(.t-cascader__step-dot--last)::after {
content: '';
display: block;
position: absolute;
left: 50%;
top: calc(var(--td-cascader-step-dot-size, 16rpx) + 14rpx);
height: 36rpx;
width: 1px;
background: var(--td-cascader-active-color, var(--td-brand-color, var(--td-primary-color-7, #0052d9)));
transform: translateX(-50%);
}
.t-cascader__step-dot--active {
background: var(--td-cascader-active-color, var(--td-brand-color, var(--td-primary-color-7, #0052d9)));
border-color: var(--td-cascader-active-color, var(--td-brand-color, var(--td-primary-color-7, #0052d9)));
}
.t-cascader__step-label {
padding-left: 16px;
font-size: 16px;
}
.t-cascader__step-label--active {
color: var(--td-cascader-active-color, var(--td-brand-color, var(--td-primary-color-7, #0052d9)));
font-weight: 600;
}
.t-cascader__step-arrow {
color: var(--td-cascader-step-arrow-color, var(--td-font-gray-3, rgba(0, 0, 0, 0.4)));
margin-left: auto;
}

View File

@ -0,0 +1,3 @@
import { TdCascaderProps } from './type';
declare const props: TdCascaderProps;
export default props;

View File

@ -0,0 +1,37 @@
const props = {
closeBtn: {
type: Boolean,
value: true,
},
keys: {
type: Object,
},
options: {
type: Array,
value: [],
},
subTitles: {
type: Array,
value: [],
},
theme: {
type: String,
value: 'step',
},
title: {
type: String,
},
value: {
type: null,
value: null,
},
defaultValue: {
type: null,
value: null,
},
visible: {
type: Boolean,
value: false,
},
};
export default props;

View File

@ -0,0 +1,39 @@
import { TreeOptionData, KeysType } from '../common/common';
export interface TdCascaderProps<CascaderOption extends TreeOptionData = TreeOptionData> {
closeBtn?: {
type: BooleanConstructor;
value?: boolean;
};
keys?: {
type: ObjectConstructor;
value?: KeysType;
};
options?: {
type: ArrayConstructor;
value?: Array<CascaderOption>;
};
subTitles?: {
type: ArrayConstructor;
value?: Array<string>;
};
theme?: {
type: StringConstructor;
value?: 'step' | 'tab';
};
title?: {
type: StringConstructor;
value?: string;
};
value?: {
type: null;
value?: string | number;
};
defaultValue?: {
type: null;
value?: string | number;
};
visible?: {
type: BooleanConstructor;
value?: boolean;
};
}

View File

@ -0,0 +1 @@
export {};