Skip to content

Commit 631e826

Browse files
chore: pass gesture responder event in FAB.Group onLongPress (#3891)
1 parent 957997b commit 631e826

File tree

6 files changed

+124
-7
lines changed

6 files changed

+124
-7
lines changed

src/components/FAB/AnimatedFAB.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export type Props = $Omit<$RemoveChildren<typeof Surface>, 'mode'> & {
7878
/**
7979
* Function to execute on long press.
8080
*/
81-
onLongPress?: () => void;
81+
onLongPress?: (e: GestureResponderEvent) => void;
8282
/**
8383
* The number of milliseconds a user must touch the element before executing `onLongPress`.
8484
*/

src/components/FAB/FAB.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export type Props = $Omit<$RemoveChildren<typeof Surface>, 'mode'> & {
9696
/**
9797
* Function to execute on long press.
9898
*/
99-
onLongPress?: () => void;
99+
onLongPress?: (e: GestureResponderEvent) => void;
100100
/**
101101
* The number of milliseconds a user must touch the element before executing `onLongPress`.
102102
*/

src/components/FAB/FABGroup.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export type Props = {
7777
/**
7878
* Function to execute on long pressing the `FAB`.
7979
*/
80-
onLongPress?: () => void;
80+
onLongPress?: (e: GestureResponderEvent) => void;
8181
/**
8282
* Makes actions stack appear on long press instead of on press.
8383
*/
@@ -442,9 +442,9 @@ const FABGroup = ({
442442
toggle();
443443
}
444444
}}
445-
onLongPress={() => {
445+
onLongPress={(e) => {
446446
if (!open || enableLongPressWhenStackOpened) {
447-
onLongPress?.();
447+
onLongPress?.(e);
448448
if (toggleStackOnLongPress) {
449449
toggle();
450450
}

src/components/__tests__/AnimatedFAB.test.tsx

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import * as React from 'react';
44
import { Animated, StyleSheet } from 'react-native';
55

6-
import { render } from '@testing-library/react-native';
6+
import { fireEvent, render } from '@testing-library/react-native';
7+
import { act } from 'react-test-renderer';
78

89
import { MD3Colors } from '../../styles/themes/v3/tokens';
910
import AnimatedFAB from '../FAB/AnimatedFAB';
@@ -144,3 +145,36 @@ it('renders correct elevation value for shadow views', () => {
144145
elevation: 3,
145146
});
146147
});
148+
149+
describe('AnimatedFAB events', () => {
150+
it('onPress passes event', () => {
151+
const onPress = jest.fn();
152+
const { getByTestId } = render(
153+
<AnimatedFAB extended icon="plus" onPress={onPress} label="Add items" />
154+
);
155+
156+
act(() => {
157+
fireEvent(getByTestId('animated-fab'), 'onPress', { key: 'value' });
158+
});
159+
160+
expect(onPress).toHaveBeenCalledWith({ key: 'value' });
161+
});
162+
163+
it('onLongPress passes event', () => {
164+
const onLongPress = jest.fn();
165+
const { getByTestId } = render(
166+
<AnimatedFAB
167+
extended
168+
icon="plus"
169+
onLongPress={onLongPress}
170+
label="Add items"
171+
/>
172+
);
173+
174+
act(() => {
175+
fireEvent(getByTestId('animated-fab'), 'onLongPress', { key: 'value' });
176+
});
177+
178+
expect(onLongPress).toHaveBeenCalledWith({ key: 'value' });
179+
});
180+
});

src/components/__tests__/FAB.test.tsx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as React from 'react';
22
import { Animated, StyleSheet } from 'react-native';
33

4-
import { render } from '@testing-library/react-native';
4+
import { fireEvent, render } from '@testing-library/react-native';
55
import color from 'color';
6+
import { act } from 'react-test-renderer';
67

78
import { getTheme } from '../../core/theming';
89
import { black, white } from '../../styles/themes/v2/colors';
@@ -418,3 +419,29 @@ it('animated value changes correctly', () => {
418419
transform: [{ scale: 1.5 }],
419420
});
420421
});
422+
423+
describe('FAB events', () => {
424+
it('onPress passes event', () => {
425+
const onPress = jest.fn();
426+
const { getByText } = render(<FAB onPress={onPress} label="Add items" />);
427+
428+
act(() => {
429+
fireEvent(getByText('Add items'), 'onPress', { key: 'value' });
430+
});
431+
432+
expect(onPress).toHaveBeenCalledWith({ key: 'value' });
433+
});
434+
435+
it('onLongPress passes event', () => {
436+
const onLongPress = jest.fn();
437+
const { getByText } = render(
438+
<FAB onLongPress={onLongPress} label="Add items" />
439+
);
440+
441+
act(() => {
442+
fireEvent(getByText('Add items'), 'onLongPress', { key: 'value' });
443+
});
444+
445+
expect(onLongPress).toHaveBeenCalledWith({ key: 'value' });
446+
});
447+
});

src/components/__tests__/FABGroup.test.tsx

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,62 @@ it('animated value changes correctly', () => {
221221
});
222222
});
223223

224+
describe('FAB.Group events', () => {
225+
it('onPress passes event', () => {
226+
const onPress = jest.fn();
227+
const { getByText } = render(
228+
<FAB.Group
229+
visible
230+
open={false}
231+
label="Stack test"
232+
icon=""
233+
onStateChange={jest.fn()}
234+
onPress={onPress}
235+
actions={[
236+
{
237+
label: 'testing',
238+
onPress() {},
239+
icon: '',
240+
},
241+
]}
242+
/>
243+
);
244+
245+
act(() => {
246+
fireEvent(getByText('Stack test'), 'onPress', { key: 'value' });
247+
});
248+
249+
expect(onPress).toHaveBeenCalledWith({ key: 'value' });
250+
});
251+
252+
it('onLongPress passes event', () => {
253+
const onLongPress = jest.fn();
254+
const { getByText } = render(
255+
<FAB.Group
256+
visible
257+
open={false}
258+
label="Stack test"
259+
icon=""
260+
onStateChange={jest.fn()}
261+
onLongPress={onLongPress}
262+
actions={[
263+
{
264+
label: 'testing',
265+
onPress() {},
266+
icon: '',
267+
},
268+
]}
269+
/>
270+
);
271+
272+
act(() => {
273+
fireEvent(getByText('Stack test'), 'onLongPress', { key: 'value' });
274+
});
275+
276+
expect(onLongPress).toHaveBeenCalledWith({ key: 'value' });
277+
});
278+
});
279+
224280
describe('Toggle Stack visibility', () => {
225281
it('toggles stack visibility on press', () => {
226282
const onStateChange = jest.fn();

0 commit comments

Comments
 (0)