Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/components/Table.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { forwardRef } from 'react';
import { OmitProps } from '../types';
import { TableProps } from '../types';
import { allowed } from '../utils/allowed';
import React from 'react';
import { HeaderProvider } from '../utils/tableContext';

type TableProps = {
className?: string;
} & OmitProps;

const Table = forwardRef<HTMLTableElement, TableProps>(
({ className, ...props }, ref) => {
const classes = `${className || ''} responsiveTable`;
Expand Down
4 changes: 2 additions & 2 deletions src/components/Tbody.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import allowed from '../utils/allowed';
import { OmitProps } from '../types';
import { TbodyProps } from '../types';

function Tbody(props: OmitProps) {
function Tbody(props: TbodyProps) {
return <tbody data-testid="tbody" {...allowed(props)} />;
}
export default Tbody;
6 changes: 3 additions & 3 deletions src/components/Thead.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, { PropsWithChildren, ReactElement } from 'react';
import React, { ReactElement } from 'react';

import allowed from '../utils/allowed';
import { OmitProps } from '../types';
import { TheadProps } from '../types';

function Thead(props: PropsWithChildren<OmitProps>) {
function Thead(props: TheadProps) {
const { children } = props;

return (
Expand Down
4 changes: 2 additions & 2 deletions src/components/Tr.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, { useContext } from 'react';
import TrInner from './TrInner';
import { HeaderContext } from '../utils/tableContext';
import { OmitProps } from '../types';
import { TrProps } from '../types';

function Tr(props: OmitProps) {
function Tr(props: TrProps) {
const context = useContext(HeaderContext);

if (!context) {
Expand Down
11 changes: 3 additions & 8 deletions src/components/TrInner.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import React, {
PropsWithChildren,
ReactElement,
useContext,
useEffect,
} from 'react';
import React, { ReactElement, useContext, useEffect } from 'react';
import { HeaderContext } from '../utils/tableContext';

import allowed from '../utils/allowed';
import { OmitProps } from '../types';
import { TrProps } from '../types';

type TrInnerProps = PropsWithChildren<OmitProps> & {
type TrInnerProps = TrProps & {
headers: string[];
};

Expand Down
14 changes: 13 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Dispatch, ReactNode, SetStateAction } from 'react';
import {
Dispatch,
ReactNode,
SetStateAction,
HTMLAttributes,
TableHTMLAttributes,
} from 'react';

export type OmitProps = {
inHeader?: boolean;
Expand All @@ -18,3 +24,9 @@ export type HeaderContextType = {
export type TdProps = OmitProps & {
columnKey?: number;
};

// Component-specific types that combine OmitProps with appropriate HTML attributes
export type TrProps = OmitProps & HTMLAttributes<HTMLTableRowElement>;
export type TbodyProps = OmitProps & HTMLAttributes<HTMLTableSectionElement>;
export type TableProps = OmitProps & TableHTMLAttributes<HTMLTableElement>;
export type TheadProps = OmitProps & HTMLAttributes<HTMLTableSectionElement>;
59 changes: 58 additions & 1 deletion test/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { fireEvent, render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';

import { Table, Thead, Tbody, Tr, Th, Td } from '../src';
Expand Down Expand Up @@ -382,4 +382,61 @@ describe('SuperResponsiveTable UniqueCase', () => {
expect(cellWithRowSpan).toBeInTheDocument();
expect(cellWithRowSpan).toHaveAttribute('rowSpan', '2');
});

it('should handle events and pass React HTMLAttributes props to Tr, Th, and Td components', () => {
const handleClick = jest.fn();
const handleMouseEnter = jest.fn();
const handleDoubleClick = jest.fn();
const handleKeyDown = jest.fn();

const { getByTestId } = render(
<Table>
<Thead>
<Tr>
<Th
data-testid="header"
onMouseEnter={handleMouseEnter}
onDoubleClick={handleDoubleClick}
onKeyDown={handleKeyDown}
>
Header 1
</Th>
<Th>Header 2</Th>
</Tr>
</Thead>
<Tbody>
<Tr data-testid="clickable-row" onClick={handleClick}>
<Td
data-testid="cell"
onMouseEnter={handleMouseEnter}
onDoubleClick={handleDoubleClick}
onKeyDown={handleKeyDown}
>
Cell 1
</Td>
<Td>Cell 2</Td>
</Tr>
</Tbody>
</Table>
);

const row = getByTestId('clickable-row');
const header = getByTestId('header');
const cell = getByTestId('cell');

fireEvent.click(row);
expect(handleClick).toHaveBeenCalledTimes(1);

fireEvent.mouseEnter(header);
fireEvent.mouseEnter(cell);
expect(handleMouseEnter).toHaveBeenCalledTimes(2);

fireEvent.doubleClick(header);
fireEvent.doubleClick(cell);
expect(handleDoubleClick).toHaveBeenCalledTimes(2);

fireEvent.keyDown(header, { key: 'Enter', code: 'Enter' });
fireEvent.keyDown(cell, { key: 'Enter', code: 'Enter' });
expect(handleKeyDown).toHaveBeenCalledTimes(2);
});
});