Skip to content

Commit

Permalink
feat: added onLongPress to RadioButton.Item and Checkbox.Item (#4215
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ondrejnedoma committed Jan 8, 2024
1 parent f46e9a3 commit 4d57fe2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/components/Checkbox/CheckboxItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export type Props = {
* Function to execute on press.
*/
onPress?: (e: GestureResponderEvent) => void;
/**
* Function to execute on long press.
*/
onLongPress?: (e: GestureResponderEvent) => void;
/**
* Accessibility label for the touchable. This is read by the screen reader when the user taps the touchable.
*/
Expand Down Expand Up @@ -122,6 +126,7 @@ const CheckboxItem = ({
status,
label,
onPress,
onLongPress,
labelStyle,
theme: themeOverrides,
testID,
Expand Down Expand Up @@ -167,6 +172,7 @@ const CheckboxItem = ({
disabled,
}}
onPress={onPress}
onLongPress={onLongPress}
testID={testID}
disabled={disabled}
rippleColor={rippleColor}
Expand Down
6 changes: 6 additions & 0 deletions src/components/RadioButton/RadioButtonItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export type Props = {
* Function to execute on press.
*/
onPress?: (e: GestureResponderEvent) => void;
/**
* Function to execute on long press.
*/
onLongPress?: (e: GestureResponderEvent) => void;
/**
* Accessibility label for the touchable. This is read by the screen reader when the user taps the touchable.
*/
Expand Down Expand Up @@ -132,6 +136,7 @@ const RadioButtonItem = ({
style,
labelStyle,
onPress,
onLongPress,
disabled,
color,
uncheckedColor,
Expand Down Expand Up @@ -195,6 +200,7 @@ const RadioButtonItem = ({
event,
})
}
onLongPress={onLongPress}
accessibilityLabel={accessibilityLabel}
accessibilityRole="radio"
accessibilityState={{
Expand Down
21 changes: 20 additions & 1 deletion src/components/__tests__/Checkbox/CheckboxItem.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { Platform } from 'react-native';

import { render } from '@testing-library/react-native';
import { act, fireEvent, render } from '@testing-library/react-native';

import Checkbox from '../../Checkbox';

Expand Down Expand Up @@ -95,3 +95,22 @@ it('should have maxFontSizeMultiplier set to 1.5 by default', () => {
const checkboxItemText = getByTestId('checkbox-item-text');
expect(checkboxItemText.props.maxFontSizeMultiplier).toBe(1.5);
});

it('should execute onLongPress', () => {
const onLongPress = jest.fn();

const { getByTestId } = render(
<Checkbox.Item
label="Item"
status="unchecked"
testID="checkbox-item"
onLongPress={onLongPress}
/>
);

act(() => {
fireEvent(getByTestId('checkbox-item'), 'longPress', { key: 'value' });
});

expect(onLongPress).toHaveBeenCalledTimes(1);
});
21 changes: 20 additions & 1 deletion src/components/__tests__/RadioButton/RadioButtonItem.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { Platform } from 'react-native';

import { render } from '@testing-library/react-native';
import { act, fireEvent, render } from '@testing-library/react-native';

import RadioButton from '../../RadioButton';

Expand Down Expand Up @@ -58,3 +58,22 @@ it('can render leading radio button control', () => {

expect(tree).toMatchSnapshot();
});

it('should execute onLongPress', () => {
const onLongPress = jest.fn();

const { getByTestId } = render(
<RadioButton.Item
label="Item"
value="android"
testID="radio-button-item"
onLongPress={onLongPress}
/>
);

act(() => {
fireEvent(getByTestId('radio-button-item'), 'longPress', { key: 'value' });
});

expect(onLongPress).toHaveBeenCalledTimes(1);
});

0 comments on commit 4d57fe2

Please sign in to comment.