Skip to content

Commit da3d6a9

Browse files
authored
feat: indexbar异步加载数据时,异常处理 (#1260)
1 parent 2e1a556 commit da3d6a9

File tree

4 files changed

+56
-32
lines changed

4 files changed

+56
-32
lines changed

compiled/alipay/demo/pages/IndexBarControl/index.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,19 @@ Page({
3131
data: {
3232
nameMap,
3333
items: [],
34-
current: 'S',
34+
current: '',
3535
},
3636
onLoad() {
37-
this.setData({
38-
items: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('').map((u) => {
39-
return { label: u, disablePreview: true };
40-
}),
41-
});
37+
// 异步获取数据
38+
setTimeout(() => {
39+
this.setData({
40+
nameMap,
41+
items: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('').map((u) => {
42+
return { label: u, disablePreview: true };
43+
}),
44+
current: 'S',
45+
});
46+
}, 1000);
4247
},
4348
onChange(item, index) {
4449
console.log(item, index);

compiled/alipay/src/IndexBar/index.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import equal from 'fast-deep-equal';
12
import { IndexBarDefaultProps } from './props';
23

34
Component({
@@ -14,19 +15,13 @@ Component({
1415
hasDefaultSlot: true,
1516
},
1617
didMount() {
17-
const { defaultCurrent, current, items } = this.props;
18-
this.initItemHeight();
19-
this.initTopRange();
20-
const initCurrent = this.isControlled() ? current : defaultCurrent;
21-
const _index = items.findIndex((u) => initCurrent === u.label);
22-
this.setData({
23-
currentKey: _index,
24-
touchKeyIndex: _index,
25-
touchKey: initCurrent,
26-
});
18+
this.init();
2719
},
2820
didUpdate(_prop) {
2921
const { current, items } = this.props;
22+
if (!equal(_prop.items, this.props.items)) {
23+
this.init();
24+
}
3025
if (_prop.current !== current) {
3126
const _index = items.findIndex((u) => current === u.label);
3227
this.setData({
@@ -41,6 +36,18 @@ Component({
4136
}
4237
},
4338
methods: {
39+
init() {
40+
const { defaultCurrent, current, items } = this.props;
41+
this.initItemHeight();
42+
this.initTopRange();
43+
const initCurrent = this.isControlled() ? current : defaultCurrent;
44+
const _index = items.findIndex((u) => initCurrent === u.label);
45+
this.setData({
46+
currentKey: _index,
47+
touchKeyIndex: _index,
48+
touchKey: initCurrent,
49+
});
50+
},
4451
isControlled(valueKey = 'current') {
4552
if ('controlled' in this.props) {
4653
return this.props.controlled;

demo/pages/IndexBarControl/index.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,19 @@ Page({
3131
data: {
3232
nameMap,
3333
items: [],
34-
current: 'S',
34+
current: '',
3535
},
3636
onLoad() {
37-
this.setData({
38-
items: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('').map((u) => {
39-
return { label: u, disablePreview: true };
40-
}),
41-
});
37+
// 异步获取数据
38+
setTimeout(() => {
39+
this.setData({
40+
nameMap,
41+
items: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('').map((u) => {
42+
return { label: u, disablePreview: true };
43+
}),
44+
current: 'S',
45+
});
46+
}, 1000);
4247
},
4348
onChange(item, index) {
4449
console.log(item, index);

src/IndexBar/index.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import equal from 'fast-deep-equal';
12
import { IndexBarDefaultProps } from './props';
23

34
Component({
@@ -14,19 +15,13 @@ Component({
1415
hasDefaultSlot: true,
1516
},
1617
didMount() {
17-
const { defaultCurrent, current, items } = this.props;
18-
this.initItemHeight();
19-
this.initTopRange();
20-
const initCurrent = this.isControlled() ? current : defaultCurrent;
21-
const _index = items.findIndex((u) => initCurrent === u.label);
22-
this.setData({
23-
currentKey: _index,
24-
touchKeyIndex: _index,
25-
touchKey: initCurrent,
26-
});
18+
this.init();
2719
},
2820
didUpdate(_prop) {
2921
const { current, items } = this.props;
22+
if (!equal(_prop.items, this.props.items)) {
23+
this.init();
24+
}
3025
if (_prop.current !== current) {
3126
const _index = items.findIndex((u) => current === u.label);
3227
this.setData({
@@ -41,6 +36,18 @@ Component({
4136
}
4237
},
4338
methods: {
39+
init() {
40+
const { defaultCurrent, current, items } = this.props;
41+
this.initItemHeight();
42+
this.initTopRange();
43+
const initCurrent = this.isControlled() ? current : defaultCurrent;
44+
const _index = items.findIndex((u) => initCurrent === u.label);
45+
this.setData({
46+
currentKey: _index,
47+
touchKeyIndex: _index,
48+
touchKey: initCurrent,
49+
});
50+
},
4451
isControlled(valueKey = 'current') {
4552
if ('controlled' in this.props) {
4653
return this.props.controlled;

0 commit comments

Comments
 (0)