Skip to content

Commit

Permalink
fix: Disable row handler when clicking on a checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
Shawn Zhu committed Mar 28, 2023
1 parent 5137a9f commit 3ae4ed1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/components/TableModule/TableModule.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,8 @@ export const RowSelection: ComponentStory<typeof TableModule> = (args) => {
cell: {
content: (rowData: RowSelectionRow) => (
<Checkbox
label=" "
label=""
aria-label="select row"
checked={rowData.getIsSelected()}
disabled={!rowData.getCanSelect()}
onChange={rowData.getToggleSelectedHandler()}
Expand Down
36 changes: 36 additions & 0 deletions src/components/TableModule/TableModuleRow.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { renderWithTheme } from '../../testUtils/renderWithTheme';
import { TableModuleRow } from './TableModuleRow';
import * as React from 'react';
import { testIds } from './TableModule';
import { Checkbox } from '../Checkbox';

test('it renders a TableModuleRow', async () => {
const { findByTestId } = renderWithTheme(
Expand Down Expand Up @@ -126,3 +127,38 @@ test('it does not render row actions when the `rowActions` function returns null
const td = await findByRole('cell');
expect(td.innerHTML).toBe('');
});

test('it does not trigger row click when target is a checkbox input', async () => {
const mockFn = jest.fn();
const mockCheckboxClick = jest.fn();

const { findByLabelText } = renderWithTheme(
<table>
<tbody>
<TableModuleRow
data={{}}
row={{
id: 'row',
}}
cells={[
{
content: () => (
<Checkbox label="Select Row" onClick={mockCheckboxClick} />
),
},
]}
headingsLength={0}
onRowClick={mockFn}
/>
</tbody>
</table>
);

const checkbox = await findByLabelText('Select Row');

fireEvent.click(checkbox);

expect(mockCheckboxClick).toBeCalledTimes(1);
// does not trigger row click
expect(mockFn).toBeCalledTimes(0);
});
4 changes: 4 additions & 0 deletions src/components/TableModule/TableModuleRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ const TableModuleRow: React.FC<TableModuleRowProps> = React.memo(
return;
}

if (e.target?.tagName === 'INPUT' && e.target?.type === 'checkbox') {
return;
}

e?.currentTarget?.blur();

onRowClick?.(row);
Expand Down

0 comments on commit 3ae4ed1

Please sign in to comment.