Skip to content

Commit 341b44b

Browse files
committed
fix(ItemButton): serialization
1 parent a1298fe commit 341b44b

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

.changeset/wild-parents-matter.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cube-dev/ui-kit": patch
3+
---
4+
5+
Don't pass onPress prop to the element in ItemBase.

src/components/actions/ItemButton/ItemButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const ItemButton = forwardRef(function ItemButton(
2424
allProps: CubeItemButtonProps,
2525
ref: FocusableRef<HTMLElement>,
2626
) {
27-
const { mods, to, htmlType, as, type, theme, ...rest } =
27+
const { mods, to, htmlType, as, type, theme, onPress, ...rest } =
2828
allProps as CubeItemButtonProps & {
2929
as?: 'a' | 'button' | 'div' | 'span';
3030
};

src/components/actions/use-action.test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,26 @@ describe('performClickHandler', () => {
277277
...modifiers,
278278
});
279279

280+
it('passes a serializable event to onPress (target preserved, non-enumerable)', () => {
281+
const evt = createMockEvent();
282+
283+
performClickHandler(evt, {
284+
navigate: mockNavigate,
285+
resolvedHref: undefined,
286+
to: undefined,
287+
onPress: mockOnPress,
288+
tracking: mockTracking,
289+
navigationOptions: {},
290+
});
291+
292+
expect(mockOnPress).toHaveBeenCalled();
293+
const arg = mockOnPress.mock.calls[0][0];
294+
expect(() => JSON.stringify(arg)).not.toThrow();
295+
expect(arg.target).toBe(evt.target);
296+
expect(arg.shiftKey).toBe(false);
297+
expect(arg.metaKey).toBe(false);
298+
});
299+
280300
it('should handle history navigation', () => {
281301
const evt = createMockEvent();
282302

src/components/actions/use-action.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,27 @@ export function parseTo(to: NavigateArg | undefined): {
140140
};
141141
}
142142

143+
function sanitizePressEvent(evt: PressEvent): PressEvent {
144+
const safeEvt: any = {
145+
type: (evt as any)?.type,
146+
pointerType: (evt as any)?.pointerType,
147+
shiftKey: !!(evt as any)?.shiftKey,
148+
metaKey: !!(evt as any)?.metaKey,
149+
ctrlKey: !!(evt as any)?.ctrlKey,
150+
altKey: !!(evt as any)?.altKey,
151+
};
152+
try {
153+
Object.defineProperty(safeEvt, 'target', {
154+
value: (evt as any)?.target,
155+
enumerable: false,
156+
configurable: true,
157+
});
158+
} catch (e) {
159+
// do nothing
160+
}
161+
return safeEvt;
162+
}
163+
143164
export function performClickHandler(
144165
evt,
145166
{ navigate, resolvedHref, to, onPress, tracking, navigationOptions },
@@ -156,7 +177,7 @@ export function performClickHandler(
156177
const element = evt.target;
157178
const qa = element?.getAttribute('data-qa');
158179

159-
onPress?.(evt);
180+
onPress?.(sanitizePressEvent(evt));
160181

161182
if (to == null) {
162183
// Allow 0 as valid navigation (go to current page)

0 commit comments

Comments
 (0)