explorer: fix click propagating from action buttons

When the tree item actions buttons were clicked, they were also clicking
the underlying tree item.
This commit is contained in:
David Lechner
2022-04-08 11:04:39 -05:00
parent fad7b0114a
commit 2536e17865
2 changed files with 19 additions and 1 deletions
+9
View File
@@ -115,6 +115,9 @@ describe('tree item', () => {
userEvent.click(button);
expect(dispatch).toHaveBeenCalledWith(explorerRenameFile('test.file'));
// should not propagate to treeitem
expect(dispatch).toHaveBeenCalledTimes(1);
});
it('should dispatch action when key is pressed', async () => {
@@ -144,6 +147,9 @@ describe('tree item', () => {
userEvent.click(button);
expect(dispatch).toHaveBeenCalledWith(explorerExportFile('test.file'));
// should not propagate to treeitem
expect(dispatch).toHaveBeenCalledTimes(1);
});
it('should dispatch export action when key is pressed', async () => {
@@ -173,6 +179,9 @@ describe('tree item', () => {
userEvent.click(button);
expect(dispatch).toHaveBeenCalledWith(explorerDeleteFile('test.file'));
// should not propagate to treeitem
expect(dispatch).toHaveBeenCalledTimes(1);
});
it('should dispatch delete action when key is pressed', async () => {
+10 -1
View File
@@ -61,6 +61,15 @@ const ActionButton: React.VoidFunctionComponent<ActionButtonProps> = ({
focusable,
onClick,
}) => {
const handleClick = useCallback<React.MouseEventHandler>(
(e) => {
// prevent click on treeitem too
e.stopPropagation();
onClick();
},
[onClick],
);
return (
<Button
icon={icon}
@@ -68,7 +77,7 @@ const ActionButton: React.VoidFunctionComponent<ActionButtonProps> = ({
disabled={disabled}
tabIndex={focusable === false ? -1 : undefined}
onFocus={focusable === false ? (e) => e.preventDefault() : undefined}
onClick={onClick}
onClick={handleClick}
/>
);
};