File tree Expand file tree Collapse file tree 3 files changed +22
-9
lines changed
packages/components/src/components Expand file tree Collapse file tree 3 files changed +22
-9
lines changed Original file line number Diff line number Diff line change 11import {
22 onMount ,
3+ onUnMount ,
34 onUpdate ,
45 Show ,
56 useDefaultProps ,
@@ -28,6 +29,10 @@ useDefaultProps<DBTabItemProps>({});
2829export default function DBTabItem ( props : DBTabItemProps ) {
2930 const _ref = useRef < HTMLInputElement | any > ( null ) ;
3031
32+ function setSelectedOnChange ( event : any ) {
33+ state . _selected = event . target === _ref ;
34+ }
35+
3136 // jscpd:ignore-start
3237 const state = useStore < DBTabItemState > ( {
3338 _selected : false ,
@@ -65,20 +70,18 @@ export default function DBTabItem(props: DBTabItemProps) {
6570
6671 onUpdate ( ( ) => {
6772 if ( state . initialized && _ref ) {
68- if ( props . active ) {
69- _ref . click ( ) ;
70- }
71-
7273 useTarget ( { react : ( ) => state . handleNameAttribute ( ) } ) ;
7374 state . initialized = false ;
7475
7576 // deselect this tab when another tab in tablist is selected
7677 _ref . closest ( '[role=tablist]' ) ?. addEventListener (
7778 'change' ,
78- ( event : any ) => {
79- state . _selected = event . target === _ref ;
80- }
79+ setSelectedOnChange
8180 ) ;
81+
82+ if ( props . active ) {
83+ _ref . click ( ) ;
84+ }
8285 }
8386 } , [ _ref , state . initialized ] ) ;
8487
@@ -88,6 +91,13 @@ export default function DBTabItem(props: DBTabItemProps) {
8891 }
8992 } , [ props . name ] ) ;
9093
94+ onUnMount ( ( ) => {
95+ _ref . closest ( '[role=tablist]' ) ?. removeEventListener (
96+ 'change' ,
97+ setSelectedOnChange
98+ ) ;
99+ } ) ;
100+
91101 return (
92102 < li class = { cls ( 'db-tab-item' , props . className ) } role = "none" >
93103 < label
Original file line number Diff line number Diff line change @@ -170,11 +170,11 @@ export default function DBTabs(props: DBTabsProps) {
170170 if ( tabItem ) {
171171 const list = tabItem . parentElement ;
172172 if ( list ) {
173- const indices = Array . from ( list . childNodes ) . indexOf (
173+ const tabIndex = Array . from ( list . children ) . indexOf (
174174 tabItem
175175 ) ;
176176 if ( props . onIndexChange ) {
177- props . onIndexChange ( indices ) ;
177+ props . onIndexChange ( tabIndex ) ;
178178 }
179179
180180 if ( props . onTabSelect ) {
Original file line number Diff line number Diff line change @@ -42,6 +42,9 @@ const testActions = () => {
4242 test ( 'should be clickable' , async ( { mount } ) => {
4343 expect ( tabIndex ) . toBe ( undefined ) ;
4444
45+ // Beware: the comments below actually change the selector for vue
46+ // this is necessary because vue will not trigger a check on an list element but requires the actual
47+ // radio button element, which has the role=tab
4548 const component = await mount ( comp ) ;
4649 await component
4750 . getByTestId ( 'test2' )
You can’t perform that action at this time.
0 commit comments