@@ -13,6 +13,13 @@ export default class Indexes extends SuperComponent {
1313
1414 properties = props ;
1515
16+ controlledProps = [
17+ {
18+ key : 'current' ,
19+ event : 'change' ,
20+ } ,
21+ ] ;
22+
1623 data = {
1724 prefix,
1825 classPrefix : name ,
@@ -37,8 +44,6 @@ export default class Indexes extends SuperComponent {
3744
3845 sidebar = null ;
3946
40- currentTouchAnchor = null ;
41-
4247 observers = {
4348 indexList ( v ) {
4449 this . setIndexList ( v ) ;
@@ -47,6 +52,12 @@ export default class Indexes extends SuperComponent {
4752 height ( v ) {
4853 this . setHeight ( v ) ;
4954 } ,
55+
56+ current ( current : string | number ) {
57+ if ( current && this . data . activeAnchor && current !== this . data . activeAnchor ) {
58+ this . setAnchorByCurrent ( current , 'update' ) ;
59+ }
60+ } ,
5061 } ;
5162
5263 lifetimes = {
@@ -100,7 +111,9 @@ export default class Indexes extends SuperComponent {
100111 const next = this . groupTop [ index + 1 ] ;
101112 item . totalHeight = ( next ?. top || Infinity ) - item . top ;
102113 } ) ;
103- this . setAnchorOnScroll ( 0 ) ;
114+
115+ const current = this . data . current || this . data . _indexList [ 0 ] ;
116+ this . setAnchorByCurrent ( current , 'init' ) ;
104117 } ) ;
105118 this . getSidebarRect ( ) ;
106119 } ,
@@ -147,31 +160,36 @@ export default class Indexes extends SuperComponent {
147160 }
148161 } ,
149162
150- setAnchorByIndex ( index ) {
151- const { _indexList, stickyOffset } = this . data ;
152- const activeAnchor = _indexList [ index ] ;
163+ setAnchorByCurrent ( current : string | number , source : 'init' | 'click' | 'touch' | 'update' ) {
164+ const { stickyOffset } = this . data ;
153165
154- if ( this . data . activeAnchor !== null && this . data . activeAnchor === activeAnchor ) return ;
166+ if ( this . data . activeAnchor !== null && this . data . activeAnchor === current ) return ;
155167
156- const target = this . groupTop . find ( ( item ) => item . anchor === activeAnchor ) ;
168+ const target = this . groupTop . find ( ( item ) => item . anchor === current ) ;
157169
158170 if ( target ) {
159- this . currentTouchAnchor = activeAnchor ;
160171 const scrollTop = target . top - stickyOffset ;
161- wx . pageScrollTo ( {
162- scrollTop,
163- duration : 0 ,
164- } ) ;
165- this . toggleTips ( true ) ;
166- this . triggerEvent ( 'select' , { index : activeAnchor } ) ;
167- this . setData ( { activeAnchor } ) ;
172+
173+ if ( scrollTop === 0 && source === 'init' ) {
174+ this . setAnchorOnScroll ( scrollTop ) ;
175+ } else {
176+ wx . pageScrollTo ( {
177+ scrollTop,
178+ duration : 0 ,
179+ } ) ;
180+ }
181+
182+ if ( [ 'click' , 'touch' ] . includes ( source ) ) {
183+ this . toggleTips ( true ) ;
184+ this . triggerEvent ( 'select' , { index : current } ) ;
185+ }
168186 }
169187 } ,
170188
171189 onClick ( e ) {
172- const { index } = e . currentTarget . dataset ;
190+ const { current } = e . currentTarget . dataset ;
173191
174- this . setAnchorByIndex ( index ) ;
192+ this . setAnchorByCurrent ( current , 'click' ) ;
175193 } ,
176194
177195 onTouchMove ( e ) {
@@ -203,15 +221,15 @@ export default class Indexes extends SuperComponent {
203221 } ;
204222 const index = getAnchorIndex ( e . changedTouches [ 0 ] . clientY ) ;
205223
206- this . setAnchorByIndex ( index ) ;
224+ this . setAnchorByCurrent ( this . data . _indexList [ index ] , 'touch' ) ;
207225 } , 1000 / 30 ) , // 30 frame
208226
209227 setAnchorOnScroll ( scrollTop : number ) {
210228 if ( ! this . groupTop ) {
211229 return ;
212230 }
213231
214- const { sticky, stickyOffset, activeAnchor } = this . data ;
232+ const { sticky, stickyOffset } = this . data ;
215233
216234 scrollTop += stickyOffset ;
217235
@@ -222,13 +240,10 @@ export default class Indexes extends SuperComponent {
222240 if ( curIndex === - 1 ) return ;
223241
224242 const curGroup = this . groupTop [ curIndex ] ;
225- if ( this . currentTouchAnchor !== null ) {
226- this . triggerEvent ( 'change' , { index : curGroup . anchor } ) ;
227- this . currentTouchAnchor = null ;
228- } else if ( activeAnchor !== curGroup . anchor ) {
229- this . triggerEvent ( 'change' , { index : curGroup . anchor } ) ;
230- this . setData ( { activeAnchor : curGroup . anchor } ) ;
231- }
243+
244+ this . setData ( { activeAnchor : curGroup . anchor } , ( ) => {
245+ this . _trigger ( 'change' , { index : curGroup . anchor , current : curGroup . anchor } ) ;
246+ } ) ;
232247
233248 if ( sticky ) {
234249 const offset = curGroup . top - scrollTop ;
0 commit comments