From 3f8a450c20aa7e9d127307c57e1e8530fb5063ec Mon Sep 17 00:00:00 2001 From: Fabrizio Cucci Date: Thu, 1 Feb 2024 12:21:18 -0800 Subject: [PATCH] [skip ci] Convert `Button` to function component (#42795) Summary: As per title. Changelog: [Internal] Reviewed By: cortinico Differential Revision: D53308758 --- .../Libraries/Components/Button.js | 206 +++++++++--------- 1 file changed, 102 insertions(+), 104 deletions(-) diff --git a/packages/react-native/Libraries/Components/Button.js b/packages/react-native/Libraries/Components/Button.js index 306a9998c8d752..6a0c2fdca9a348 100644 --- a/packages/react-native/Libraries/Components/Button.js +++ b/packages/react-native/Libraries/Components/Button.js @@ -281,113 +281,111 @@ type ButtonProps = $ReadOnly<{| ``` */ -class Button extends React.Component { - render(): React.Node { - const { - accessibilityLabel, - accessibilityState, - 'aria-busy': ariaBusy, - 'aria-checked': ariaChecked, - 'aria-disabled': ariaDisabled, - 'aria-expanded': ariaExpanded, - 'aria-label': ariaLabel, - 'aria-selected': ariaSelected, - importantForAccessibility, - color, - onPress, - touchSoundDisabled, - title, - hasTVPreferredFocus, - nextFocusDown, - nextFocusForward, - nextFocusLeft, - nextFocusRight, - nextFocusUp, - testID, - accessible, - accessibilityActions, - accessibilityHint, - accessibilityLanguage, - onAccessibilityAction, - } = this.props; - const buttonStyles: Array = [styles.button]; - const textStyles: Array = [styles.text]; - if (color) { - if (Platform.OS === 'ios') { - textStyles.push({color: color}); - } else { - buttonStyles.push({backgroundColor: color}); - } - } - - let _accessibilityState = { - busy: ariaBusy ?? accessibilityState?.busy, - checked: ariaChecked ?? accessibilityState?.checked, - disabled: ariaDisabled ?? accessibilityState?.disabled, - expanded: ariaExpanded ?? accessibilityState?.expanded, - selected: ariaSelected ?? accessibilityState?.selected, - }; - - const disabled = - this.props.disabled != null - ? this.props.disabled - : _accessibilityState?.disabled; - - _accessibilityState = - disabled !== _accessibilityState?.disabled - ? {..._accessibilityState, disabled} - : _accessibilityState; - - if (disabled) { - buttonStyles.push(styles.buttonDisabled); - textStyles.push(styles.textDisabled); +const Button: React.AbstractComponent = (props: ButtonProps) => { + const { + accessibilityLabel, + accessibilityState, + 'aria-busy': ariaBusy, + 'aria-checked': ariaChecked, + 'aria-disabled': ariaDisabled, + 'aria-expanded': ariaExpanded, + 'aria-label': ariaLabel, + 'aria-selected': ariaSelected, + importantForAccessibility, + color, + onPress, + touchSoundDisabled, + title, + hasTVPreferredFocus, + nextFocusDown, + nextFocusForward, + nextFocusLeft, + nextFocusRight, + nextFocusUp, + testID, + accessible, + accessibilityActions, + accessibilityHint, + accessibilityLanguage, + onAccessibilityAction, + } = props; + const buttonStyles: Array = [styles.button]; + const textStyles: Array = [styles.text]; + if (color) { + if (Platform.OS === 'ios') { + textStyles.push({color: color}); + } else { + buttonStyles.push({backgroundColor: color}); } + } - invariant( - typeof title === 'string', - 'The title prop of a Button must be a string', - ); - const formattedTitle = - Platform.OS === 'android' ? title.toUpperCase() : title; - const Touchable = - Platform.OS === 'android' ? TouchableNativeFeedback : TouchableOpacity; - - // If `no` is specified for `importantForAccessibility`, it will be changed to `no-hide-descendants` because the text inside should not be focused. - const _importantForAccessibility = - importantForAccessibility === 'no' - ? 'no-hide-descendants' - : importantForAccessibility; - - return ( - - - - {formattedTitle} - - - - ); + let _accessibilityState = { + busy: ariaBusy ?? accessibilityState?.busy, + checked: ariaChecked ?? accessibilityState?.checked, + disabled: ariaDisabled ?? accessibilityState?.disabled, + expanded: ariaExpanded ?? accessibilityState?.expanded, + selected: ariaSelected ?? accessibilityState?.selected, + }; + + const disabled = + props.disabled != null ? props.disabled : _accessibilityState?.disabled; + + _accessibilityState = + disabled !== _accessibilityState?.disabled + ? {..._accessibilityState, disabled} + : _accessibilityState; + + if (disabled) { + buttonStyles.push(styles.buttonDisabled); + textStyles.push(styles.textDisabled); } -} + + invariant( + typeof title === 'string', + 'The title prop of a Button must be a string', + ); + const formattedTitle = + Platform.OS === 'android' ? title.toUpperCase() : title; + const Touchable = + Platform.OS === 'android' ? TouchableNativeFeedback : TouchableOpacity; + + // If `no` is specified for `importantForAccessibility`, it will be changed to `no-hide-descendants` because the text inside should not be focused. + const _importantForAccessibility = + importantForAccessibility === 'no' + ? 'no-hide-descendants' + : importantForAccessibility; + + return ( + + + + {formattedTitle} + + + + ); +}; + +Button.displayName = 'Button'; const styles = StyleSheet.create({ button: Platform.select({