Skip to content

Commit

Permalink
Fix bug related to <Table> cells and rows calling preventDefault() wh…
Browse files Browse the repository at this point in the history
…en pressing enter key
  • Loading branch information
calebjacob committed Dec 12, 2024
1 parent 14c933c commit 9672a70
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@near-pagoda/ui",
"version": "3.0.0",
"version": "3.0.1",
"description": "A React component library that implements the official NEAR design system.",
"license": "MIT",
"repository": {
Expand Down
22 changes: 14 additions & 8 deletions src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
import { createContext, forwardRef, useContext, useEffect, useRef } from 'react';

import { usePagodaUi } from '../../context/PagodaUi';
import { mergeRefs } from '../../utils';
import { Flex } from '../Flex';
import { Placeholder } from '../Placeholder';
import { SvgIcon } from '../SvgIcon';
Expand Down Expand Up @@ -140,14 +141,16 @@ export const Row = forwardRef<HTMLTableRowElement, RowProps>(({ className = '',
const clickable = !!props.onClick;
const role = clickable ? 'button' : undefined;
const tabIndex = clickable ? 0 : undefined;
const rowRef = useRef<HTMLTableRowElement | null>(null);

const onKeyDown: KeyboardEventHandler<HTMLTableRowElement> = (event) => {
if (event.key === 'Enter') {
if (props.onKeyDown) props.onKeyDown(event);

if (event.key === 'Enter' && rowRef.current === event.target) {
event.preventDefault();
event.stopPropagation();
event.target.dispatchEvent(new Event('click', { bubbles: true, cancelable: true }));
}
if (props.onKeyDown) props.onKeyDown(event);
};

return (
Expand All @@ -156,7 +159,7 @@ export const Row = forwardRef<HTMLTableRowElement, RowProps>(({ className = '',
data-clickable={clickable}
role={role}
tabIndex={tabIndex}
ref={ref}
ref={mergeRefs([ref, rowRef])}
{...props}
onKeyDown={onKeyDown}
/>
Expand Down Expand Up @@ -187,11 +190,12 @@ export const HeadCell = forwardRef<HTMLTableCellElement, HeadCellProps>(
const role = clickable ? 'button' : undefined;
const tabIndex = clickable ? 0 : undefined;
const columnHasActiveSort = sort?.column === column;
const cellRef = useRef<HTMLTableCellElement | null>(null);

const onKeyDown: KeyboardEventHandler<HTMLTableCellElement> = (event) => {
if (props.onKeyDown) props.onKeyDown(event);

if (event.key === 'Enter') {
if (event.key === 'Enter' && cellRef.current === event.target) {
event.preventDefault();
event.stopPropagation();
event.target.dispatchEvent(new Event('click', { bubbles: true, cancelable: true }));
Expand Down Expand Up @@ -225,7 +229,7 @@ export const HeadCell = forwardRef<HTMLTableCellElement, HeadCellProps>(
data-clickable={clickable}
role={role}
tabIndex={tabIndex}
ref={ref}
ref={mergeRefs([ref, cellRef])}
{...props}
onKeyDown={onKeyDown}
onClick={onClick}
Expand Down Expand Up @@ -266,14 +270,16 @@ export const Cell = forwardRef<HTMLTableCellElement, CellProps>(
const role = isButton ? 'button' : undefined;
const tabIndex = isButton ? (disabled ? -1 : 0) : undefined;
const { Link } = usePagodaUi();
const cellRef = useRef<HTMLTableCellElement | null>(null);

const onKeyDown: KeyboardEventHandler<HTMLTableCellElement> = (event) => {
if (event.key === 'Enter') {
if (props.onKeyDown) props.onKeyDown(event);

if (event.key === 'Enter' && cellRef.current === event.target) {
event.preventDefault();
event.stopPropagation();
event.target.dispatchEvent(new Event('click', { bubbles: true, cancelable: true }));
}
if (props.onKeyDown) props.onKeyDown(event);
};

return (
Expand All @@ -283,7 +289,7 @@ export const Cell = forwardRef<HTMLTableCellElement, CellProps>(
data-clickable={clickable}
role={role}
tabIndex={tabIndex}
ref={ref}
ref={mergeRefs([ref, cellRef])}
{...props}
colSpan={spanAllColumns ? 10_000 : props.colSpan}
onKeyDown={onKeyDown}
Expand Down

0 comments on commit 9672a70

Please sign in to comment.