Skip to content

Commit

Permalink
feat: Add support for verticalAlign style (#34567)
Browse files Browse the repository at this point in the history
Summary:
This adds support for the `verticalAlign` style attribute, mapping the already existing `textAlignVertical` attribute as requested on #34425. This PR also updates the TextExample.android on the RNTester in order to facilitate the manual QA of this.

## Changelog

[Android] [Added] - Add support for verticalAlign style

Pull Request resolved: #34567

Test Plan:
1. On Android open the RNTester app and navigate to the Text page
2. Check the text alignment through the `Text alignment` section

https://user-images.githubusercontent.com/11707729/188051914-bf15f7eb-e53f-4de5-8033-d1b572352935.mov

Reviewed By: jacdebug

Differential Revision: D39771237

Pulled By: cipolleschi

fbshipit-source-id: d2a81bec1edd8d49a0fcd36a42fea53734909739
  • Loading branch information
gabrieldonadel authored and facebook-github-bot committed Oct 3, 2022
1 parent 9864586 commit 32b6f31
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Libraries/Components/TextInput/TextInput.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,11 @@ export interface TextInputAndroidProps {
* When false, it will prevent the soft keyboard from showing when the field is focused. The default value is true
*/
showSoftInputOnFocus?: boolean | undefined;

/**
* Vertically align text when `multiline` is set to true
*/
verticalAlign?: 'auto' | 'top' | 'bottom' | 'middle' | undefined;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions Libraries/Components/TextInput/TextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {ViewProps} from '../View/ViewPropTypes';
import type {TextInputType} from './TextInput.flow';

import usePressability from '../../Pressability/usePressability';
import flattenStyle from '../../StyleSheet/flattenStyle';
import StyleSheet, {
type ColorValue,
type TextStyleProp,
Expand Down Expand Up @@ -1599,6 +1600,13 @@ const ExportedForwardRef: React.AbstractComponent<
React.ElementRef<HostComponent<mixed>> & ImperativeMethods,
>,
) {
const style = flattenStyle(restProps.style);

if (style?.verticalAlign != null) {
style.textAlignVertical =
verticalAlignToTextAlignVerticalMap[style.verticalAlign];
}

return (
<InternalTextInput
allowFontScaling={allowFontScaling}
Expand Down Expand Up @@ -1628,6 +1636,7 @@ const ExportedForwardRef: React.AbstractComponent<
}
{...restProps}
forwardedRef={forwardedRef}
style={style}
/>
);
});
Expand Down Expand Up @@ -1659,5 +1668,12 @@ const styles = StyleSheet.create({
},
});

const verticalAlignToTextAlignVerticalMap = {
auto: 'auto',
top: 'top',
bottom: 'bottom',
middle: 'center',
};

// $FlowFixMe[unclear-type] Unclear type. Using `any` type is not safe.
module.exports = ((ExportedForwardRef: any): TextInputType);
1 change: 1 addition & 0 deletions Libraries/Components/View/ReactNativeStyleAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ const ReactNativeStyleAttributes: {[string]: AnyAttributeType, ...} = {
textShadowRadius: true,
textTransform: true,
userSelect: true,
verticalAlign: true,
writingDirection: true,

/**
Expand Down
1 change: 1 addition & 0 deletions Libraries/StyleSheet/StyleSheetTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ export interface TextStyleIOS extends ViewStyle {

export interface TextStyleAndroid extends ViewStyle {
textAlignVertical?: 'auto' | 'top' | 'bottom' | 'center' | undefined;
verticalAlign?: 'auto' | 'top' | 'bottom' | 'middle' | undefined;
includeFontPadding?: boolean | undefined;
}

Expand Down
1 change: 1 addition & 0 deletions Libraries/StyleSheet/StyleSheetTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ export type ____TextStyle_InternalCore = $ReadOnly<{
textDecorationColor?: ____ColorValue_Internal,
textTransform?: 'none' | 'capitalize' | 'uppercase' | 'lowercase',
userSelect?: 'auto' | 'text' | 'none' | 'contain' | 'all',
verticalAlign?: 'auto' | 'top' | 'bottom' | 'middle',
writingDirection?: 'auto' | 'ltr' | 'rtl',
}>;

Expand Down
14 changes: 14 additions & 0 deletions Libraries/Text/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ const Text: React.AbstractComponent<
_selectable = userSelectToSelectableMap[style.userSelect];
}

if (style?.verticalAlign != null) {
style = StyleSheet.compose(style, {
textAlignVertical:
verticalAlignToTextAlignVerticalMap[style.verticalAlign],
});
}

if (__DEV__) {
if (PressabilityDebug.isEnabled() && onPress != null) {
style = StyleSheet.compose(restProps.style, {
Expand Down Expand Up @@ -275,4 +282,11 @@ const userSelectToSelectableMap = {
all: true,
};

const verticalAlignToTextAlignVerticalMap = {
auto: 'auto',
top: 'top',
bottom: 'bottom',
middle: 'center',
};

module.exports = Text;
22 changes: 22 additions & 0 deletions packages/rn-tester/js/examples/Text/TextExample.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -1001,4 +1001,26 @@ exports.examples = [
);
},
},
{
title: 'Text alignment',
render: function (): React.Node {
return (
<View>
<Text style={{textAlignVertical: 'top', borderWidth: 1, height: 75}}>
Text element aligned to the top via textAlignVertical
</Text>
<Text style={{verticalAlign: 'top', borderWidth: 1, height: 75}}>
Text element aligned to the top via verticalAlign
</Text>
<Text
style={{textAlignVertical: 'center', borderWidth: 1, height: 75}}>
Text element aligned to the middle via textAlignVertical
</Text>
<Text style={{verticalAlign: 'middle', borderWidth: 1, height: 75}}>
Text element aligned to the middle via verticalAlign
</Text>
</View>
);
},
},
];

0 comments on commit 32b6f31

Please sign in to comment.