@@ -11,6 +11,7 @@ import type {
11
11
import { createContext , forwardRef , useContext , useEffect , useRef } from 'react' ;
12
12
13
13
import { usePagodaUi } from '../../context/PagodaUi' ;
14
+ import { mergeRefs } from '../../utils' ;
14
15
import { Flex } from '../Flex' ;
15
16
import { Placeholder } from '../Placeholder' ;
16
17
import { SvgIcon } from '../SvgIcon' ;
@@ -140,14 +141,16 @@ export const Row = forwardRef<HTMLTableRowElement, RowProps>(({ className = '',
140
141
const clickable = ! ! props . onClick ;
141
142
const role = clickable ? 'button' : undefined ;
142
143
const tabIndex = clickable ? 0 : undefined ;
144
+ const rowRef = useRef < HTMLTableRowElement | null > ( null ) ;
143
145
144
146
const onKeyDown : KeyboardEventHandler < HTMLTableRowElement > = ( event ) => {
145
- if ( event . key === 'Enter' ) {
147
+ if ( props . onKeyDown ) props . onKeyDown ( event ) ;
148
+
149
+ if ( event . key === 'Enter' && rowRef . current === event . target ) {
146
150
event . preventDefault ( ) ;
147
151
event . stopPropagation ( ) ;
148
152
event . target . dispatchEvent ( new Event ( 'click' , { bubbles : true , cancelable : true } ) ) ;
149
153
}
150
- if ( props . onKeyDown ) props . onKeyDown ( event ) ;
151
154
} ;
152
155
153
156
return (
@@ -156,7 +159,7 @@ export const Row = forwardRef<HTMLTableRowElement, RowProps>(({ className = '',
156
159
data-clickable = { clickable }
157
160
role = { role }
158
161
tabIndex = { tabIndex }
159
- ref = { ref }
162
+ ref = { mergeRefs ( [ ref , rowRef ] ) }
160
163
{ ...props }
161
164
onKeyDown = { onKeyDown }
162
165
/>
@@ -187,11 +190,12 @@ export const HeadCell = forwardRef<HTMLTableCellElement, HeadCellProps>(
187
190
const role = clickable ? 'button' : undefined ;
188
191
const tabIndex = clickable ? 0 : undefined ;
189
192
const columnHasActiveSort = sort ?. column === column ;
193
+ const cellRef = useRef < HTMLTableCellElement | null > ( null ) ;
190
194
191
195
const onKeyDown : KeyboardEventHandler < HTMLTableCellElement > = ( event ) => {
192
196
if ( props . onKeyDown ) props . onKeyDown ( event ) ;
193
197
194
- if ( event . key === 'Enter' ) {
198
+ if ( event . key === 'Enter' && cellRef . current === event . target ) {
195
199
event . preventDefault ( ) ;
196
200
event . stopPropagation ( ) ;
197
201
event . target . dispatchEvent ( new Event ( 'click' , { bubbles : true , cancelable : true } ) ) ;
@@ -225,7 +229,7 @@ export const HeadCell = forwardRef<HTMLTableCellElement, HeadCellProps>(
225
229
data-clickable = { clickable }
226
230
role = { role }
227
231
tabIndex = { tabIndex }
228
- ref = { ref }
232
+ ref = { mergeRefs ( [ ref , cellRef ] ) }
229
233
{ ...props }
230
234
onKeyDown = { onKeyDown }
231
235
onClick = { onClick }
@@ -266,14 +270,16 @@ export const Cell = forwardRef<HTMLTableCellElement, CellProps>(
266
270
const role = isButton ? 'button' : undefined ;
267
271
const tabIndex = isButton ? ( disabled ? - 1 : 0 ) : undefined ;
268
272
const { Link } = usePagodaUi ( ) ;
273
+ const cellRef = useRef < HTMLTableCellElement | null > ( null ) ;
269
274
270
275
const onKeyDown : KeyboardEventHandler < HTMLTableCellElement > = ( event ) => {
271
- if ( event . key === 'Enter' ) {
276
+ if ( props . onKeyDown ) props . onKeyDown ( event ) ;
277
+
278
+ if ( event . key === 'Enter' && cellRef . current === event . target ) {
272
279
event . preventDefault ( ) ;
273
280
event . stopPropagation ( ) ;
274
281
event . target . dispatchEvent ( new Event ( 'click' , { bubbles : true , cancelable : true } ) ) ;
275
282
}
276
- if ( props . onKeyDown ) props . onKeyDown ( event ) ;
277
283
} ;
278
284
279
285
return (
@@ -283,7 +289,7 @@ export const Cell = forwardRef<HTMLTableCellElement, CellProps>(
283
289
data-clickable = { clickable }
284
290
role = { role }
285
291
tabIndex = { tabIndex }
286
- ref = { ref }
292
+ ref = { mergeRefs ( [ ref , cellRef ] ) }
287
293
{ ...props }
288
294
colSpan = { spanAllColumns ? 10_000 : props . colSpan }
289
295
onKeyDown = { onKeyDown }
0 commit comments