Skip to content

Commit f95b0bb

Browse files
authored
fix(Picker): fixed the problem that dynamic changes of keys are invalid (#4084)
1 parent d8cba42 commit f95b0bb

File tree

3 files changed

+19
-35
lines changed

3 files changed

+19
-35
lines changed

packages/components/picker-item/picker-item.ts

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,6 @@ export default class PickerItem extends SuperComponent {
4242
relations: RelationsOptions = {
4343
'../picker/picker': {
4444
type: 'parent',
45-
linked(parent) {
46-
if ('keys' in parent.data) {
47-
const { keys } = parent.data;
48-
49-
if (keys === null || JSON.stringify(this.data.pickerKeys) === JSON.stringify(keys)) return;
50-
51-
this.setData({
52-
pickerKeys: { ...this.data.pickerKeys, ...keys },
53-
});
54-
}
55-
},
5645
},
5746
};
5847

@@ -65,7 +54,7 @@ export default class PickerItem extends SuperComponent {
6554
properties = props;
6655

6756
observers = {
68-
'options, pickerKeys'() {
57+
'options, keys'() {
6958
this.update();
7059
},
7160
};
@@ -78,7 +67,7 @@ export default class PickerItem extends SuperComponent {
7867
value: '',
7968
curIndex: 0,
8069
columnIndex: 0,
81-
pickerKeys: { value: 'value', label: 'label', icon: 'icon' },
70+
keys: {},
8271
formatOptions: props.options.value,
8372
// 虚拟滚动相关
8473
enableVirtualScroll: false, // 是否启用虚拟滚动
@@ -283,10 +272,10 @@ export default class PickerItem extends SuperComponent {
283272
},
284273

285274
updateSelected(index: number, trigger: boolean) {
286-
const { columnIndex, pickerKeys, formatOptions } = this.data;
275+
const { columnIndex, keys, formatOptions } = this.data;
287276
this._selectedIndex = index;
288-
this._selectedValue = formatOptions[index]?.[pickerKeys?.value];
289-
this._selectedLabel = formatOptions[index]?.[pickerKeys?.label];
277+
this._selectedValue = formatOptions[index]?.[keys?.value];
278+
this._selectedLabel = formatOptions[index]?.[keys?.label];
290279

291280
if (trigger) {
292281
this.$parent?.triggerColumnChange({
@@ -298,7 +287,7 @@ export default class PickerItem extends SuperComponent {
298287

299288
// 刷新选中状态
300289
update() {
301-
const { options, value, pickerKeys, format, columnIndex, itemHeight, visibleItemCount } = this.data;
290+
const { options, value, keys, format, columnIndex, itemHeight, visibleItemCount } = this.data;
302291

303292
const formatOptions = this.formatOption(options, columnIndex, format);
304293
const optionsCount = formatOptions.length;
@@ -310,10 +299,10 @@ export default class PickerItem extends SuperComponent {
310299
let index: number = -1;
311300
if (optionsCount > 500) {
312301
// 构建临时 Map(只在查找时构建,不缓存)
313-
const valueMap = new Map<any, number>(formatOptions.map((item, idx) => [item[pickerKeys?.value], idx]));
302+
const valueMap = new Map<any, number>(formatOptions.map((item, idx) => [item[keys?.value], idx]));
314303
index = valueMap.get(value) ?? -1;
315304
} else {
316-
index = formatOptions.findIndex((item: PickerItemOption) => item[pickerKeys?.value] === value);
305+
index = formatOptions.findIndex((item: PickerItemOption) => item[keys?.value] === value);
317306
}
318307
const selectedIndex = index > 0 ? index : 0;
319308

@@ -410,13 +399,13 @@ export default class PickerItem extends SuperComponent {
410399
},
411400

412401
getCurrentSelected() {
413-
const { offset, itemHeight, formatOptions, pickerKeys } = this.data;
402+
const { offset, itemHeight, formatOptions, keys } = this.data;
414403
const currentIndex = Math.max(0, Math.min(Math.round(-offset / itemHeight), this.getCount() - 1));
415404

416405
return {
417406
index: currentIndex,
418-
value: formatOptions[currentIndex]?.[pickerKeys?.value],
419-
label: formatOptions[currentIndex]?.[pickerKeys?.label],
407+
value: formatOptions[currentIndex]?.[keys?.value],
408+
label: formatOptions[currentIndex]?.[keys?.label],
420409
};
421410
},
422411
};

packages/components/picker-item/picker-item.wxml

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@
2525
data-index="{{ virtualStartIndex + index }}"
2626
bind:tap="onClickItem"
2727
>
28-
<t-icon
29-
wx:if="{{option[pickerKeys.icon]}}"
30-
class="{{classPrefix}}__item-icon"
31-
name="{{option[pickerKeys.icon]}}"
32-
/>
33-
<text class="{{classPrefix}}__item-label">{{option[pickerKeys.label]}}</text>
28+
<t-icon wx:if="{{option[keys.icon]}}" class="{{classPrefix}}__item-icon" name="{{option[keys.icon]}}" />
29+
<text class="{{classPrefix}}__item-label">{{option[keys.label]}}</text>
3430
<slot name="label-suffix--{{virtualStartIndex + index}}"></slot>
3531
</view>
3632
</view>
@@ -47,12 +43,8 @@
4743
data-index="{{ index }}"
4844
bind:tap="onClickItem"
4945
>
50-
<t-icon
51-
wx:if="{{option[pickerKeys.icon]}}"
52-
class="{{classPrefix}}__item-icon"
53-
name="{{option[pickerKeys.icon]}}"
54-
/>
55-
<text class="{{classPrefix}}__item-label">{{option[pickerKeys.label]}}</text>
46+
<t-icon wx:if="{{option[keys.icon]}}" class="{{classPrefix}}__item-icon" name="{{option[keys.icon]}}" />
47+
<text class="{{classPrefix}}__item-label">{{option[keys.label]}}</text>
5648
<slot name="label-suffix--{{index}}"></slot>
5749
</view>
5850
</block>

packages/components/picker/picker.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import useCustomNavbar from '../mixins/using-custom-navbar';
66
const { prefix } = config;
77
const name = `${prefix}-picker`;
88

9+
const DEFAULT_KEYS = { value: 'value', label: 'label', icon: 'icon' };
10+
911
@wxComponent()
1012
export default class Picker extends SuperComponent {
1113
behaviors = [useCustomNavbar];
@@ -50,14 +52,15 @@ export default class Picker extends SuperComponent {
5052

5153
methods = {
5254
updateChildren() {
53-
const { value, defaultValue, itemHeight, visibleItemCount } = this.properties;
55+
const { value, defaultValue, itemHeight, visibleItemCount, keys } = this.properties;
5456

5557
this.$children.forEach((child, index) => {
5658
child.setData({
5759
value: value?.[index] ?? defaultValue?.[index] ?? '',
5860
columnIndex: index,
5961
itemHeight,
6062
visibleItemCount,
63+
keys: { ...DEFAULT_KEYS, ...(keys || {}) },
6164
});
6265
child.update();
6366
});

0 commit comments

Comments
 (0)