11import  Component  from  '@glimmer/component' ; 
2- import  {  isNone ,   isEmpty  }  from  '@ember/utils' ; 
2+ import  {  isNone  }  from  '@ember/utils' ; 
33import  {  action  }  from  '@ember/object' ; 
44import  {  tracked  }  from  '@glimmer/tracking' ; 
5- import  {  once ,  scheduleOnce  }  from  '@ember/runloop' ; 
5+ import  {  modifier  }  from  'ember-modifier' ; 
6+ import  {  runTask  }  from  'ember-lifeline' ; 
67
78export  const  DOWN_ARROW  =  40 ; 
89export  const  LEFT_ARROW  =  37 ; 
@@ -21,15 +22,17 @@ let instanceCount = 0;
2122 * @extends  Ember.Component 
2223 */ 
2324export  default  class  IvyTabsTabListComponent  extends  Component  { 
24-   _registerWithTabsContainer ( )  { 
25-     this . internalId  =  `ivy-tabs-list-${ instanceCount ++ }  ; 
25+   registerWithTabsContainer  =  modifier ( ( )  =>  { 
2626    this . args . tabsContainer . registerTabList ( this ) ; 
27-     once ( this ,  this . selectTab ) ; 
28-   } 
29- 
30-   _unregisterWithTabsContainer ( )  { 
31-     this . args . tabsContainer . unregisterTabList ( this ) ; 
32-   } 
27+     // if none of the tabs are selected, try to select one 
28+     let  selected  =  this . tabs . find ( ( tab )  =>  tab . isSelected ) ; 
29+     if  ( ! selected  &&  this . tabs . length  >  0 )  { 
30+       this . selectTab ( ) ; 
31+     } 
32+     return  ( )  =>  { 
33+       this . args . tabsContainer . unregisterTabList ( this ) ; 
34+     } ; 
35+   } ) ; 
3336
3437  /** 
3538   * Tells screenreaders that only one tab can be selected at a time. 
@@ -71,17 +74,20 @@ export default class IvyTabsTabListComponent extends Component {
7174   * 
7275   * @method  focusSelectedTab 
7376   */ 
77+   @action 
7478  focusSelectedTab ( )  { 
75-     this . selectedTab . focus ( ) ; 
79+     if  ( ! this . isDestroyed  &&  ! this . isDestroying )  { 
80+       this . selectedTab . focus ( ) ; 
81+     } 
7682  } 
7783
7884  constructor ( )  { 
7985    super ( ...arguments ) ; 
80-     this . _registerWithTabsContainer ( ) ; 
86+     this . internalId   =   `ivy-tabs-list- ${ instanceCount ++ } ` ; 
8187  } 
8288
8389  get  isEmpty ( )  { 
84-     return  isEmpty ( this . tabs ) ; 
90+     return  this . tabs . length   ===   0 ; 
8591  } 
8692
8793  /** 
@@ -108,7 +114,7 @@ export default class IvyTabsTabListComponent extends Component {
108114    } 
109115
110116    event . preventDefault ( ) ; 
111-     scheduleOnce ( 'afterRender' ,   this ,  this . focusSelectedTab ) ; 
117+     runTask ( this ,  this . focusSelectedTab ) ; 
112118  } 
113119
114120  /** 
@@ -117,16 +123,20 @@ export default class IvyTabsTabListComponent extends Component {
117123   * @method  registerTab 
118124   * @param  {IvyTabs.IvyTabComponent } tab 
119125   */ 
126+   @action 
120127  registerTab ( tab )  { 
121-     this . tabs  =  this . tabs . concat ( tab ) ; 
122-     once ( this ,  this . selectTab ) ; 
128+     runTask ( this ,  ( )  =>  { 
129+       this . tabs  =  this . tabs . concat ( tab ) ; 
130+       this . selectTab ( ) ; 
131+     } ) ; 
123132  } 
124133
125134  /** 
126135   * Selects the next tab in the list, if any. 
127136   * 
128137   * @method  selectNextTab 
129138   */ 
139+   @action 
130140  selectNextTab ( )  { 
131141    const  selectedTab  =  this . selectedTab ; 
132142    const  tabs  =  this . tabs ; 
@@ -155,6 +165,7 @@ export default class IvyTabsTabListComponent extends Component {
155165   * 
156166   * @method  selectPreviousTab 
157167   */ 
168+   @action 
158169  selectPreviousTab ( )  { 
159170    const  selectedTab  =  this . selectedTab ; 
160171    const  tabs  =  this . tabs ; 
@@ -194,6 +205,7 @@ export default class IvyTabsTabListComponent extends Component {
194205    return  this . args [ 'aria-label' ] ; 
195206  } 
196207
208+   @action 
197209  selectTab ( )  { 
198210    const  selection  =  this . selection ; 
199211
@@ -213,18 +225,20 @@ export default class IvyTabsTabListComponent extends Component {
213225  selectTabByIndex ( index )  { 
214226    const  tab  =  this . tabs [ index ] ; 
215227
216-     if  ( tab )  { 
217-       tab . select ( ) ; 
228+     if  ( tab  &&  tab . isSelected  ===  false )  { 
229+       runTask ( tab ,  ( )  =>  { 
230+         tab . select ( ) ; 
231+       } ) ; 
218232    } 
219233  } 
220234
221235  selectTabByModel ( model )  { 
222-     const  tab  =  this . tabs . find ( ( element )  =>  { 
223-       return  element . model  ===  model ; 
224-     } ) ; 
236+     const  tab  =  this . tabs . find ( ( element )  =>  element . model  ===  model ) ; 
225237
226238    if  ( tab )  { 
227-       tab . select ( ) ; 
239+       runTask ( tab ,  ( )  =>  { 
240+         tab . select ( ) ; 
241+       } ) ; 
228242    } 
229243  } 
230244
@@ -253,8 +267,7 @@ export default class IvyTabsTabListComponent extends Component {
253267   */ 
254268  unregisterTab ( tab )  { 
255269    const  index  =  tab . index ; 
256- 
257-     if  ( tab . isSelected )  { 
270+     if  ( tab . isSelected  &&  ! this . isDestroying  &&  ! this . isDestroyed )  { 
258271      if  ( index  ===  0 )  { 
259272        this . selectNextTab ( ) ; 
260273      }  else  { 
@@ -266,9 +279,4 @@ export default class IvyTabsTabListComponent extends Component {
266279      return  element  !==  tab ; 
267280    } ) ; 
268281  } 
269- 
270-   willDestroy ( )  { 
271-     super . willDestroy ( ...arguments ) ; 
272-     this . _unregisterWithTabsContainer ( ) ; 
273-   } 
274282} 
0 commit comments