Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(picker): support keys property #1759

Merged
merged 2 commits into from
Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/picker-item/picker-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ export default class PickerItem extends SuperComponent {
relations: RelationsOptions = {
'../picker/picker': {
type: 'parent',
linked(parent) {
if ('keys' in parent.data) {
const { keys } = parent.data;

this.setData({
labelAlias: keys?.label || 'label',
valueAlias: keys?.value || 'value',
});
}
},
},
};

Expand All @@ -41,6 +51,8 @@ export default class PickerItem extends SuperComponent {
duration: 0, // 滚动动画延迟
value: '',
curIndex: 0,
labelAlias: 'label',
valueAlias: 'value',
};

methods = {
Expand All @@ -64,7 +76,7 @@ export default class PickerItem extends SuperComponent {
},

onTouchEnd() {
const { offset } = this.data;
const { offset, labelAlias, valueAlias } = this.data;
const { options } = this.properties;

if (offset === this.StartOffset) {
Expand All @@ -83,8 +95,8 @@ export default class PickerItem extends SuperComponent {

wx.nextTick(() => {
this._selectedIndex = index;
this._selectedValue = options[index]?.value;
this._selectedLabel = options[index]?.label;
this._selectedValue = options[index]?.[valueAlias];
this._selectedLabel = options[index]?.[labelAlias];
this.$parent?.triggerColumnChange({
index,
column: this.columnIndex || 0,
Expand All @@ -94,9 +106,9 @@ export default class PickerItem extends SuperComponent {

// 刷新选中状态
update() {
const { options, value } = this.data;
const { options, value, labelAlias, valueAlias } = this.data;

const index = options.findIndex((item) => item.value === value);
const index = options.findIndex((item) => item[valueAlias] === value);
const selectedIndex = index > 0 ? index : 0;

this.setData({
Expand All @@ -105,8 +117,8 @@ export default class PickerItem extends SuperComponent {
});

this._selectedIndex = selectedIndex;
this._selectedValue = options[selectedIndex]?.value;
this._selectedLabel = options[selectedIndex]?.label;
this._selectedValue = options[selectedIndex]?.[valueAlias];
this._selectedLabel = options[selectedIndex]?.[labelAlias];
},

resetOrigin() {
Expand Down
2 changes: 1 addition & 1 deletion src/picker-item/picker-item.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
wx:for-item="option"
data-index="{{ index }}"
>
{{option.label}}
{{option[labelAlias]}}
</view>
</view>
</view>
5 changes: 2 additions & 3 deletions src/picker/README.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ name | type | default | description | required
-- | -- | -- | -- | --
auto-close | Boolean | true | \- | N
cancel-btn | String / Boolean / Object | true | Typescript:`boolean \| string \| ButtonProps` | N
columns | Array / Function | [] | required。Typescript:`Array<PickerColumn> \| ((item: Array<PickerValue>) => Array<PickerColumn>)` `type PickerColumn = PickerColumnItem[]` `interface PickerColumnItem { label: string,value: string}`。[see more ts definition](https://github.com/Tencent/tdesign-miniprogram/tree/develop/src/picker/type.ts) | Y
confirm-btn | String / Boolean / Object | true | Typescript:`boolean \| string \| ButtonProps`,[Button API Documents](./button?tab=api)。[see more ts definition](https://github.com/Tencent/tdesign-miniprogram/tree/develop/src/picker/type.ts) | N
footer | Slot | - | \- | N
footer | Slot | - | `deprecated` | N
header | Boolean / Slot | true | \- | N
render-label | String / Function | - | Typescript:`(item: PickerColumnItem) => string` | N
keys | Object | - | Typescript:`KeysType` | N
title | String | '' | \- | N
value | Array | - | Typescript:`Array<PickerValue>` `type PickerValue = string \| number`。[see more ts definition](https://github.com/Tencent/tdesign-miniprogram/tree/develop/src/picker/type.ts) | N
default-value | Array | undefined | uncontrolled property。Typescript:`Array<PickerValue>` `type PickerValue = string \| number`。[see more ts definition](https://github.com/Tencent/tdesign-miniprogram/tree/develop/src/picker/type.ts) | N
Expand Down
3 changes: 1 addition & 2 deletions src/picker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ isComponent: true
-- | -- | -- | -- | --
auto-close | Boolean | true | 自动关闭;在确认、取消、点击遮罩层自动关闭,不需要手动设置 visible | N
cancel-btn | String / Boolean / Object | true | 取消按钮文字。TS 类型:`boolean \| string \| ButtonProps` | N
columns | Array / Function | [] | 必需。配置每一列的选项。TS 类型:`Array<PickerColumn> \| ((item: Array<PickerValue>) => Array<PickerColumn>)` `type PickerColumn = PickerColumnItem[]` `interface PickerColumnItem { label: string,value: string}`。[详细类型定义](https://github.com/Tencent/tdesign-miniprogram/tree/develop/src/picker/type.ts) | Y
confirm-btn | String / Boolean / Object | true | 确定按钮文字。TS 类型:`boolean \| string \| ButtonProps`,[Button API Documents](./button?tab=api)。[详细类型定义](https://github.com/Tencent/tdesign-miniprogram/tree/develop/src/picker/type.ts) | N
footer | Slot | - | 已废弃。底部内容 | N
header | Boolean / Slot | true | 头部内容。值为 true 显示空白头部,值为 false 不显示任何内容,值类型为 TNode 表示自定义头部内容 | N
render-label | String / Function | - | 自定义label。TS 类型:`(item: PickerColumnItem) => string` | N
keys | Object | - | 用来定义 value / label 在 `options` 中对应的字段别名。TS 类型:`KeysType` | N
title | String | '' | 标题 | N
value | Array | - | 选中值。TS 类型:`Array<PickerValue>` `type PickerValue = string \| number`。[详细类型定义](https://github.com/Tencent/tdesign-miniprogram/tree/develop/src/picker/type.ts) | N
default-value | Array | undefined | 选中值。非受控属性。TS 类型:`Array<PickerValue>` `type PickerValue = string \| number`。[详细类型定义](https://github.com/Tencent/tdesign-miniprogram/tree/develop/src/picker/type.ts) | N
Expand Down
2 changes: 1 addition & 1 deletion src/picker/_example/base/index.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
bindpick="onColumnChange"
bindcancel="onPickerCancel"
>
<t-picker-item options="{{citys}}"></t-picker-item>
<t-picker-item options="{{citys}}" />
</t-picker>

<t-picker
Expand Down
9 changes: 8 additions & 1 deletion src/picker/picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,19 @@ export default class Picker extends SuperComponent {
value() {
this.updateChildren();
},
keys(obj) {
this.setData({
labelAlias: obj.label || 'label',
valueAlias: obj.value || 'value',
});
},
};

data = {
prefix,
classPrefix: name,
labelAlias: 'label',
valueAlias: 'value',
};

methods = {
Expand All @@ -42,7 +50,6 @@ export default class Picker extends SuperComponent {
this.$children.forEach((child, index) => {
child.setData({
value: value?.[index] || '',
siblingCount: this.$children.length,
});
child.update();
});
Expand Down
11 changes: 3 additions & 8 deletions src/picker/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ const props: TdPickerProps = {
type: null,
value: true,
},
/** 配置每一列的选项 */
columns: {
type: null,
value: [],
},
/** 确定按钮文字 */
confirmBtn: {
type: null,
Expand All @@ -31,9 +26,9 @@ const props: TdPickerProps = {
type: Boolean,
value: true,
},
/** 自定义label */
renderLabel: {
type: null,
/** 用来定义 value / label 在 `options` 中对应的字段别名 */
keys: {
type: Object,
},
/** 标题 */
title: {
Expand Down
17 changes: 5 additions & 12 deletions src/picker/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* */

import { ButtonProps } from '../button/index';
import { KeysType } from '../common/common';

export interface TdPickerProps {
/**
Expand All @@ -23,14 +24,6 @@ export interface TdPickerProps {
type: null;
value?: boolean | string | ButtonProps;
};
/**
* 配置每一列的选项
* @default []
*/
columns: {
type: ArrayConstructor;
value?: Array<PickerColumn> | ((item: Array<PickerValue>) => Array<PickerColumn>);
};
/**
* 确定按钮文字
* @default true
Expand All @@ -48,11 +41,11 @@ export interface TdPickerProps {
value?: boolean;
};
/**
* 自定义label
* 用来定义 value / label 在 `options` 中对应的字段别名
*/
renderLabel?: {
type: StringConstructor;
value?: (item: PickerColumnItem) => string;
keys?: {
type: ObjectConstructor;
value?: KeysType;
};
/**
* 标题
Expand Down