Skip to content

Commit 3e191df

Browse files
authored
fix(react): onDoubleClick fires on components (#27611)
Issue number: Resolves #21320 --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> `onDoubleClick` bindings on Ionic components do not fire when the element is double clicked. ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - `onDoubleClick` fires on Ionic components - Fixed the unit testing set-up for the react test apps ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this introduces a breaking change, please describe the impact and migration path for existing applications below. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. -->
1 parent 1d4ab4e commit 3e191df

File tree

7 files changed

+43
-16
lines changed

7 files changed

+43
-16
lines changed

core/package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"@rollup/plugin-node-resolve": "^8.4.0",
4545
"@rollup/plugin-virtual": "^2.0.3",
4646
"@stencil/angular-output-target": "^0.7.1",
47-
"@stencil/react-output-target": "^0.5.1",
47+
"@stencil/react-output-target": "^0.5.3",
4848
"@stencil/sass": "^3.0.4",
4949
"@stencil/vue-output-target": "^0.8.6",
5050
"@types/jest": "^27.5.2",

packages/react/src/components/react-component-lib/utils/attachProps.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ export const getClassName = (classList: DOMTokenList, newProps: any, oldProps: a
6262
return finalClassNames.join(' ');
6363
};
6464

65+
/**
66+
* Transforms a React event name to a browser event name.
67+
*/
68+
export const transformReactEventName = (eventNameSuffix: string) => {
69+
switch (eventNameSuffix) {
70+
case 'doubleclick':
71+
return 'dblclick';
72+
}
73+
return eventNameSuffix;
74+
};
75+
6576
/**
6677
* Checks if an event is supported in the current execution environment.
6778
* @license Modernizr 3.0.0pre (Custom Build) | MIT
@@ -70,7 +81,7 @@ export const isCoveredByReact = (eventNameSuffix: string) => {
7081
if (typeof document === 'undefined') {
7182
return true;
7283
} else {
73-
const eventName = 'on' + eventNameSuffix;
84+
const eventName = 'on' + transformReactEventName(eventNameSuffix);
7485
let isSupported = eventName in document;
7586

7687
if (!isSupported) {

packages/react/test/apps/react17/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"scripts": {
2121
"start": "react-scripts start",
2222
"build": "react-scripts build",
23-
"test": "react-scripts test",
23+
"test": "react-scripts test --env=jsdom --transformIgnorePatterns 'node_modules/(?!(@ionic/react|@ionic/react-router|@ionic/core|@stencil/core|ionicons)/)'",
2424
"eject": "react-scripts eject",
2525
"sync": "sh ./scripts/sync.sh",
2626
"cypress": "cypress run --headless --browser chrome",

packages/react/test/apps/react18/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"scripts": {
2121
"start": "react-scripts start",
2222
"build": "react-scripts build",
23-
"test": "react-scripts test",
23+
"test": "react-scripts test --env=jsdom --transformIgnorePatterns 'node_modules/(?!(@ionic/react|@ionic/react-router|@ionic/core|@stencil/core|ionicons)/)'",
2424
"eject": "react-scripts eject",
2525
"sync": "sh ./scripts/sync.sh",
2626
"cypress": "cypress run --headless --browser chrome",
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { IonButton } from '@ionic/react';
2+
import { fireEvent, render, screen } from '@testing-library/react';
3+
import React from 'react';
4+
5+
test('should support onDoubleClick bindings', () => {
6+
const mockFn = jest.fn();
7+
8+
render(<IonButton onDoubleClick={mockFn}>Click me</IonButton>);
9+
10+
// Simulate a double click on the button
11+
fireEvent.dblClick(screen.getByText('Click me'));
12+
13+
expect(mockFn).toBeCalled();
14+
});

packages/react/test/base/src/setupTests.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
// allows you to do things like:
33
// expect(element).toHaveTextContent(/react/i)
44
// learn more: https://github.com/testing-library/jest-dom
5-
import '@testing-library/jest-dom/extend-expect';
5+
import { setupIonicReact } from '@ionic/react';
6+
7+
setupIonicReact();
68

79
// Mock matchmedia
8-
window.matchMedia = window.matchMedia || function() {
10+
window.matchMedia = window.matchMedia || function () {
911
return {
10-
matches: false,
11-
addListener: function() {},
12-
removeListener: function() {}
12+
matches: false,
13+
addListener: function () { },
14+
removeListener: function () { }
1315
};
1416
};

0 commit comments

Comments
 (0)