Skip to content

Commit 82cd676

Browse files
committed
fix(states): fixed usedPressStates to pass onClick like other state hooks
1 parent 6884a3a commit 82cd676

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

packages/button/src/Button.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ const Button = forwardRef<HTMLButtonElement, ButtonProps>(function Button(
9595
? propEnablePressedAndRipple
9696
: themeType === "contained";
9797
const propHandlers = {
98+
onClick,
9899
onKeyUp,
99100
onKeyDown,
100101
onMouseUp,
@@ -132,7 +133,6 @@ const Button = forwardRef<HTMLButtonElement, ButtonProps>(function Button(
132133
aria-disabled={isDisabledTheme || undefined}
133134
{...props}
134135
{...(isDisabledTheme ? undefined : handlers)}
135-
onClick={isDisabledTheme ? undefined : onClick}
136136
ref={ref}
137137
type={type}
138138
className={className}

packages/states/src/__tests__/usePressedStates.tsx

+29-11
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,15 @@ import { renderHook } from "@testing-library/react-hooks";
33
import { fireEvent, render } from "@testing-library/react";
44

55
import usePressedStates from "../usePressedStates";
6+
import { MergableRippleHandlerNames } from "../ripples/types";
67

78
interface Props
8-
extends Pick<
9-
HTMLAttributes<HTMLButtonElement>,
10-
| "onKeyDown"
11-
| "onKeyUp"
12-
| "onMouseDown"
13-
| "onMouseUp"
14-
| "onMouseLeave"
15-
| "onTouchStart"
16-
| "onTouchMove"
17-
| "onTouchEnd"
18-
> {
9+
extends Pick<HTMLAttributes<HTMLButtonElement>, MergableRippleHandlerNames> {
1910
pressedRef?: { current: boolean | null };
2011
disableSpacebarClick?: boolean;
2112
}
2213
const Test: FC<Props> = ({
14+
onClick,
2315
onKeyDown,
2416
onKeyUp,
2517
onMouseDown,
@@ -33,6 +25,7 @@ const Test: FC<Props> = ({
3325
}) => {
3426
const { pressed, handlers } = usePressedStates({
3527
handlers: {
28+
onClick,
3629
onKeyDown,
3730
onKeyUp,
3831
onMouseDown,
@@ -74,7 +67,29 @@ describe("usePressedStates", () => {
7467
});
7568
});
7669

70+
it("should include the `onClick` in the handlers result if it was passed as an option even though the functionality is not updated", () => {
71+
const onClick = jest.fn();
72+
const { result } = renderHook(() =>
73+
usePressedStates({ handlers: { onClick } })
74+
);
75+
expect(result.current).toEqual({
76+
pressed: false,
77+
handlers: {
78+
onClick,
79+
onTouchStart: expect.any(Function),
80+
onTouchMove: expect.any(Function),
81+
onTouchEnd: expect.any(Function),
82+
onMouseDown: expect.any(Function),
83+
onMouseUp: expect.any(Function),
84+
onMouseLeave: expect.any(Function),
85+
onKeyDown: expect.any(Function),
86+
onKeyUp: expect.any(Function),
87+
},
88+
});
89+
});
90+
7791
it("should trigger the provided event handlers as normal", () => {
92+
const onClick = jest.fn();
7893
const onKeyDown = jest.fn();
7994
const onKeyUp = jest.fn();
8095
const onMouseDown = jest.fn();
@@ -86,6 +101,7 @@ describe("usePressedStates", () => {
86101

87102
const { getByText } = render(
88103
<Test
104+
onClick={onClick}
89105
onKeyDown={onKeyDown}
90106
onKeyUp={onKeyUp}
91107
onMouseDown={onMouseDown}
@@ -102,6 +118,7 @@ describe("usePressedStates", () => {
102118
fireEvent.keyUp(button);
103119
fireEvent.mouseDown(button);
104120
fireEvent.mouseUp(button);
121+
fireEvent.click(button);
105122
fireEvent.mouseLeave(button);
106123
fireEvent.touchStart(button);
107124
fireEvent.touchMove(button);
@@ -111,6 +128,7 @@ describe("usePressedStates", () => {
111128
expect(onKeyUp).toBeCalledTimes(1);
112129
expect(onMouseDown).toBeCalledTimes(1);
113130
expect(onMouseUp).toBeCalledTimes(1);
131+
expect(onClick).toBeCalledTimes(1);
114132
expect(onMouseLeave).toBeCalledTimes(1);
115133
expect(onTouchStart).toBeCalledTimes(1);
116134
expect(onTouchMove).toBeCalledTimes(1);

packages/states/src/ripples/types.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ export interface RippleState {
2626
}
2727
export type RipplesState = RippleState[];
2828

29-
export type MergableRippleHandlers<E extends HTMLElement = HTMLElement> = Pick<
30-
HTMLAttributes<E>,
29+
export type MergableRippleHandlerNames =
3130
| "onKeyDown"
3231
| "onKeyUp"
3332
| "onMouseDown"
@@ -36,7 +35,10 @@ export type MergableRippleHandlers<E extends HTMLElement = HTMLElement> = Pick<
3635
| "onClick"
3736
| "onTouchStart"
3837
| "onTouchMove"
39-
| "onTouchEnd"
38+
| "onTouchEnd";
39+
export type MergableRippleHandlers<E extends HTMLElement = HTMLElement> = Pick<
40+
HTMLAttributes<E>,
41+
MergableRippleHandlerNames
4042
>;
4143

4244
export interface RipplesOptions<E extends HTMLElement = HTMLElement> {

packages/states/src/usePressedStates.ts

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export default function usePressedStates<E extends HTMLElement = HTMLElement>({
147147
return {
148148
pressed,
149149
handlers: {
150+
onClick: handlers.onClick,
150151
onKeyDown: handleKeyDown,
151152
onKeyUp: handleKeyUp,
152153
onMouseDown: handleMouseDown,

0 commit comments

Comments
 (0)