Skip to content

Commit

Permalink
chore: forward ref to Button (#4150)
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitarNestorov authored Oct 24, 2023
1 parent e86fc0d commit 1b89fc6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
63 changes: 34 additions & 29 deletions src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import color from 'color';
import { ButtonMode, getButtonColors } from './utils';
import { useInternalTheme } from '../../core/theming';
import type { $Omit, ThemeProp } from '../../types';
import { forwardRef } from '../../utils/forwardRef';
import hasTouchHandler from '../../utils/hasTouchHandler';
import { splitStyles } from '../../utils/splitStyles';
import ActivityIndicator from '../ActivityIndicator';
Expand Down Expand Up @@ -149,34 +150,37 @@ export type Props = $Omit<React.ComponentProps<typeof Surface>, 'mode'> & {
* export default MyComponent;
* ```
*/
const Button = ({
disabled,
compact,
mode = 'text',
dark,
loading,
icon,
buttonColor: customButtonColor,
textColor: customTextColor,
rippleColor: customRippleColor,
children,
accessibilityLabel,
accessibilityHint,
onPress,
onPressIn,
onPressOut,
onLongPress,
delayLongPress,
style,
theme: themeOverrides,
uppercase: uppercaseProp,
contentStyle,
labelStyle,
testID = 'button',
accessible,
maxFontSizeMultiplier,
...rest
}: Props) => {
const Button = (
{
disabled,
compact,
mode = 'text',
dark,
loading,
icon,
buttonColor: customButtonColor,
textColor: customTextColor,
rippleColor: customRippleColor,
children,
accessibilityLabel,
accessibilityHint,
onPress,
onPressIn,
onPressOut,
onLongPress,
delayLongPress,
style,
theme: themeOverrides,
uppercase: uppercaseProp,
contentStyle,
labelStyle,
testID = 'button',
accessible,
maxFontSizeMultiplier,
...rest
}: Props,
ref: React.ForwardedRef<View>
) => {
const theme = useInternalTheme(themeOverrides);
const isMode = React.useCallback(
(modeToCompare: ButtonMode) => {
Expand Down Expand Up @@ -295,6 +299,7 @@ const Button = ({
return (
<Surface
{...rest}
ref={ref}
testID={`${testID}-container`}
style={
[
Expand Down Expand Up @@ -460,4 +465,4 @@ const styles = StyleSheet.create({
},
});

export default Button;
export default forwardRef(Button);
8 changes: 4 additions & 4 deletions src/utils/forwardRef.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import type {
ForwardRefExoticComponent,
} from 'react';

export type ForwarRefComponent<T, P = {}> = ForwardRefExoticComponent<
export type ForwardRefComponent<T, P = {}> = ForwardRefExoticComponent<
PropsWithoutRef<P> & RefAttributes<T>
>;

/**
* TypeScript generated a large union of props from `ViewProps` in
* `d.ts` files when using `React.forwardRef`. To prevent this
* `ForwarRefComponent` was created and exported. Use this
* `ForwardRefComponent` was created and exported. Use this
* `forwardRef` instead of `React.forwardRef` so you don't have to
* import `ForwarRefComponent`.
* import `ForwardRefComponent`.
* More info: https://github.com/callstack/react-native-paper/pull/3603
*/
export const forwardRef: <T, P = {}>(
render: ForwardRefRenderFunction<T, P>
) => ForwarRefComponent<T, P> = React.forwardRef;
) => ForwardRefComponent<T, P> = React.forwardRef;

0 comments on commit 1b89fc6

Please sign in to comment.