Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extend Button props by adding onPressIn and onPressOut #3277

Merged
merged 2 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ type Props = React.ComponentProps<typeof Surface> & {
* Function to execute on press.
*/
onPress?: () => void;
/**
* @supported Available in v5.x
* Function to execute as soon as the touchable element is pressed and invoked even before onPress.
*/
onPressIn?: () => void;
/**
* @supported Available in v5.x
* Function to execute as soon as the touch is released even before onPress.
*/
onPressOut?: () => void;
/**
* Function to execute on long press.
*/
Expand Down Expand Up @@ -162,6 +172,8 @@ const Button = ({
accessibilityLabel,
accessibilityHint,
onPress,
onPressIn,
onPressOut,
onLongPress,
style,
theme,
Expand Down Expand Up @@ -194,7 +206,8 @@ const Button = ({
}, [isElevationEntitled, elevation, initialElevation]);

const handlePressIn = () => {
if (isMode('contained')) {
onPressIn?.();
if (isV3 ? isMode('elevated') : isMode('contained')) {
const { scale } = animation;
Animated.timing(elevation, {
toValue: activeElevation,
Expand All @@ -205,7 +218,8 @@ const Button = ({
};

const handlePressOut = () => {
if (isMode('contained')) {
onPressOut?.();
if (isV3 ? isMode('elevated') : isMode('contained')) {
const { scale } = animation;
Animated.timing(elevation, {
toValue: initialElevation,
Expand Down
21 changes: 21 additions & 0 deletions src/components/__tests__/Button.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import { StyleSheet } from 'react-native';
import renderer from 'react-test-renderer';
import { fireEvent, render } from 'react-native-testing-library';
import color from 'color';
import Button from '../Button/Button.tsx';
import { pink500, black, white } from '../../styles/themes/v2/colors';
Expand Down Expand Up @@ -125,6 +126,26 @@ it('renders button with an accessibility hint', () => {
expect(tree).toMatchSnapshot();
});

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

const { getByTestId } = render(
<Button onPressIn={onPressInMock} testID="button" />
);
fireEvent(getByTestId('button'), 'onPressIn');
expect(onPressInMock).toHaveBeenCalledTimes(1);
});

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

const { getByTestId } = render(
<Button onPressOut={onPressOutMock} testID="button" />
);
fireEvent(getByTestId('button'), 'onPressOut');
expect(onPressOutMock).toHaveBeenCalledTimes(1);
});

describe('getButtonColors - background color', () => {
const customButtonColor = '#111111';

Expand Down