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

chore: forward ref to Button #4150

Merged
merged 1 commit into from
Oct 24, 2023
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
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;
Loading