Skip to content

Commit cd5375a

Browse files
authored
fix(#670): missing HTMLAttributes types (#671)
1 parent 7dfa3f3 commit cd5375a

7 files changed

Lines changed: 82 additions & 22 deletions

File tree

‎src/components/Table.tsx‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import { forwardRef } from 'react';
2-
import { OmitProps } from '../types';
2+
import { TableProps } from '../types';
33
import { allowed } from '../utils/allowed';
44
import React from 'react';
55
import { HeaderProvider } from '../utils/tableContext';
66

7-
type TableProps = {
8-
className?: string;
9-
} & OmitProps;
10-
117
const Table = forwardRef<HTMLTableElement, TableProps>(
128
({ className, ...props }, ref) => {
139
const classes = `${className || ''} responsiveTable`;

‎src/components/Tbody.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React from 'react';
22
import allowed from '../utils/allowed';
3-
import { OmitProps } from '../types';
3+
import { TbodyProps } from '../types';
44

5-
function Tbody(props: OmitProps) {
5+
function Tbody(props: TbodyProps) {
66
return <tbody data-testid="tbody" {...allowed(props)} />;
77
}
88
export default Tbody;

‎src/components/Thead.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import React, { PropsWithChildren, ReactElement } from 'react';
1+
import React, { ReactElement } from 'react';
22

33
import allowed from '../utils/allowed';
4-
import { OmitProps } from '../types';
4+
import { TheadProps } from '../types';
55

6-
function Thead(props: PropsWithChildren<OmitProps>) {
6+
function Thead(props: TheadProps) {
77
const { children } = props;
88

99
return (

‎src/components/Tr.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React, { useContext } from 'react';
22
import TrInner from './TrInner';
33
import { HeaderContext } from '../utils/tableContext';
4-
import { OmitProps } from '../types';
4+
import { TrProps } from '../types';
55

6-
function Tr(props: OmitProps) {
6+
function Tr(props: TrProps) {
77
const context = useContext(HeaderContext);
88

99
if (!context) {

‎src/components/TrInner.tsx‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import React, {
2-
PropsWithChildren,
3-
ReactElement,
4-
useContext,
5-
useEffect,
6-
} from 'react';
1+
import React, { ReactElement, useContext, useEffect } from 'react';
72
import { HeaderContext } from '../utils/tableContext';
83

94
import allowed from '../utils/allowed';
10-
import { OmitProps } from '../types';
5+
import { TrProps } from '../types';
116

12-
type TrInnerProps = PropsWithChildren<OmitProps> & {
7+
type TrInnerProps = TrProps & {
138
headers: string[];
149
};
1510

‎src/types/index.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { Dispatch, ReactNode, SetStateAction } from 'react';
1+
import {
2+
Dispatch,
3+
ReactNode,
4+
SetStateAction,
5+
HTMLAttributes,
6+
TableHTMLAttributes,
7+
} from 'react';
28

39
export type OmitProps = {
410
inHeader?: boolean;
@@ -18,3 +24,9 @@ export type HeaderContextType = {
1824
export type TdProps = OmitProps & {
1925
columnKey?: number;
2026
};
27+
28+
// Component-specific types that combine OmitProps with appropriate HTML attributes
29+
export type TrProps = OmitProps & HTMLAttributes<HTMLTableRowElement>;
30+
export type TbodyProps = OmitProps & HTMLAttributes<HTMLTableSectionElement>;
31+
export type TableProps = OmitProps & TableHTMLAttributes<HTMLTableElement>;
32+
export type TheadProps = OmitProps & HTMLAttributes<HTMLTableSectionElement>;

‎test/index.test.tsx‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { render, screen } from '@testing-library/react';
2+
import { fireEvent, render, screen } from '@testing-library/react';
33
import '@testing-library/jest-dom';
44

55
import { Table, Thead, Tbody, Tr, Th, Td } from '../src';
@@ -382,4 +382,61 @@ describe('SuperResponsiveTable UniqueCase', () => {
382382
expect(cellWithRowSpan).toBeInTheDocument();
383383
expect(cellWithRowSpan).toHaveAttribute('rowSpan', '2');
384384
});
385+
386+
it('should handle events and pass React HTMLAttributes props to Tr, Th, and Td components', () => {
387+
const handleClick = jest.fn();
388+
const handleMouseEnter = jest.fn();
389+
const handleDoubleClick = jest.fn();
390+
const handleKeyDown = jest.fn();
391+
392+
const { getByTestId } = render(
393+
<Table>
394+
<Thead>
395+
<Tr>
396+
<Th
397+
data-testid="header"
398+
onMouseEnter={handleMouseEnter}
399+
onDoubleClick={handleDoubleClick}
400+
onKeyDown={handleKeyDown}
401+
>
402+
Header 1
403+
</Th>
404+
<Th>Header 2</Th>
405+
</Tr>
406+
</Thead>
407+
<Tbody>
408+
<Tr data-testid="clickable-row" onClick={handleClick}>
409+
<Td
410+
data-testid="cell"
411+
onMouseEnter={handleMouseEnter}
412+
onDoubleClick={handleDoubleClick}
413+
onKeyDown={handleKeyDown}
414+
>
415+
Cell 1
416+
</Td>
417+
<Td>Cell 2</Td>
418+
</Tr>
419+
</Tbody>
420+
</Table>
421+
);
422+
423+
const row = getByTestId('clickable-row');
424+
const header = getByTestId('header');
425+
const cell = getByTestId('cell');
426+
427+
fireEvent.click(row);
428+
expect(handleClick).toHaveBeenCalledTimes(1);
429+
430+
fireEvent.mouseEnter(header);
431+
fireEvent.mouseEnter(cell);
432+
expect(handleMouseEnter).toHaveBeenCalledTimes(2);
433+
434+
fireEvent.doubleClick(header);
435+
fireEvent.doubleClick(cell);
436+
expect(handleDoubleClick).toHaveBeenCalledTimes(2);
437+
438+
fireEvent.keyDown(header, { key: 'Enter', code: 'Enter' });
439+
fireEvent.keyDown(cell, { key: 'Enter', code: 'Enter' });
440+
expect(handleKeyDown).toHaveBeenCalledTimes(2);
441+
});
385442
});

0 commit comments

Comments
 (0)