File tree 4 files changed +56
-32
lines changed
demo/pages/IndexBarControl
demo/pages/IndexBarControl
4 files changed +56
-32
lines changed Original file line number Diff line number Diff line change @@ -31,14 +31,19 @@ Page({
31
31
data : {
32
32
nameMap,
33
33
items : [ ] ,
34
- current : 'S ' ,
34
+ current : '' ,
35
35
} ,
36
36
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 ) ;
42
47
} ,
43
48
onChange ( item , index ) {
44
49
console . log ( item , index ) ;
Original file line number Diff line number Diff line change
1
+ import equal from 'fast-deep-equal' ;
1
2
import { IndexBarDefaultProps } from './props' ;
2
3
3
4
Component ( {
@@ -14,19 +15,13 @@ Component({
14
15
hasDefaultSlot : true ,
15
16
} ,
16
17
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 ( ) ;
27
19
} ,
28
20
didUpdate ( _prop ) {
29
21
const { current, items } = this . props ;
22
+ if ( ! equal ( _prop . items , this . props . items ) ) {
23
+ this . init ( ) ;
24
+ }
30
25
if ( _prop . current !== current ) {
31
26
const _index = items . findIndex ( ( u ) => current === u . label ) ;
32
27
this . setData ( {
@@ -41,6 +36,18 @@ Component({
41
36
}
42
37
} ,
43
38
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
+ } ,
44
51
isControlled ( valueKey = 'current' ) {
45
52
if ( 'controlled' in this . props ) {
46
53
return this . props . controlled ;
Original file line number Diff line number Diff line change @@ -31,14 +31,19 @@ Page({
31
31
data : {
32
32
nameMap,
33
33
items : [ ] ,
34
- current : 'S ' ,
34
+ current : '' ,
35
35
} ,
36
36
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 ) ;
42
47
} ,
43
48
onChange ( item , index ) {
44
49
console . log ( item , index ) ;
Original file line number Diff line number Diff line change
1
+ import equal from 'fast-deep-equal' ;
1
2
import { IndexBarDefaultProps } from './props' ;
2
3
3
4
Component ( {
@@ -14,19 +15,13 @@ Component({
14
15
hasDefaultSlot : true ,
15
16
} ,
16
17
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 ( ) ;
27
19
} ,
28
20
didUpdate ( _prop ) {
29
21
const { current, items } = this . props ;
22
+ if ( ! equal ( _prop . items , this . props . items ) ) {
23
+ this . init ( ) ;
24
+ }
30
25
if ( _prop . current !== current ) {
31
26
const _index = items . findIndex ( ( u ) => current === u . label ) ;
32
27
this . setData ( {
@@ -41,6 +36,18 @@ Component({
41
36
}
42
37
} ,
43
38
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
+ } ,
44
51
isControlled ( valueKey = 'current' ) {
45
52
if ( 'controlled' in this . props ) {
46
53
return this . props . controlled ;
You can’t perform that action at this time.
0 commit comments