Skip to content

Commit 8e7263a

Browse files
chiaramooneyfacebook-github-bot
authored andcommitted
Add forwardRef call to Button Component (#43666)
Summary: When RN moved Button component from a class component to a function component (07e8ae4) a forwardRef call was not added to the Button control. This caused a set of tests downstream in React Native for Windows to fail because they rely on being able to pass a ref through to the Button control. ## Changelog: [GENERAL] [FIXED] - Adds forwardRef call to new functional component implementation of Button control. Pull Request resolved: #43666 Test Plan: Button render remains the same. Reviewed By: fabriziocucci Differential Revision: D55398765 Pulled By: zeyap fbshipit-source-id: ba32c764c16cb529ab1c92cb7888f2bae0f16f5f
1 parent 66634be commit 8e7263a

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

packages/react-native/Libraries/Components/Button.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,13 @@ type ButtonProps = $ReadOnly<{|
280280
```
281281
*/
282282

283-
const Button: React.AbstractComponent<ButtonProps> = (props: ButtonProps) => {
283+
const Touchable: typeof TouchableNativeFeedback | typeof TouchableOpacity =
284+
Platform.OS === 'android' ? TouchableNativeFeedback : TouchableOpacity;
285+
286+
const Button: React.AbstractComponent<
287+
ButtonProps,
288+
React.ElementRef<typeof Touchable>,
289+
> = React.forwardRef((props: ButtonProps, ref) => {
284290
const {
285291
accessibilityLabel,
286292
accessibilityState,
@@ -345,8 +351,6 @@ const Button: React.AbstractComponent<ButtonProps> = (props: ButtonProps) => {
345351
);
346352
const formattedTitle =
347353
Platform.OS === 'android' ? title.toUpperCase() : title;
348-
const Touchable =
349-
Platform.OS === 'android' ? TouchableNativeFeedback : TouchableOpacity;
350354

351355
// If `no` is specified for `importantForAccessibility`, it will be changed to `no-hide-descendants` because the text inside should not be focused.
352356
const _importantForAccessibility =
@@ -374,15 +378,16 @@ const Button: React.AbstractComponent<ButtonProps> = (props: ButtonProps) => {
374378
testID={testID}
375379
disabled={disabled}
376380
onPress={onPress}
377-
touchSoundDisabled={touchSoundDisabled}>
381+
touchSoundDisabled={touchSoundDisabled}
382+
ref={ref}>
378383
<View style={buttonStyles}>
379384
<Text style={textStyles} disabled={disabled}>
380385
{formattedTitle}
381386
</Text>
382387
</View>
383388
</Touchable>
384389
);
385-
};
390+
});
386391

387392
Button.displayName = 'Button';
388393

packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1677,7 +1677,13 @@ exports[`public API should not change unintentionally Libraries/Components/Butto
16771677
accessibilityHint?: ?string,
16781678
accessibilityLanguage?: ?Stringish,
16791679
|}>;
1680-
declare const Button: React.AbstractComponent<ButtonProps>;
1680+
declare const Touchable:
1681+
| typeof TouchableNativeFeedback
1682+
| typeof TouchableOpacity;
1683+
declare const Button: React.AbstractComponent<
1684+
ButtonProps,
1685+
React.ElementRef<typeof Touchable>,
1686+
>;
16811687
declare export default typeof Button;
16821688
"
16831689
`;

0 commit comments

Comments
 (0)