Skip to content

Commit af6c4b2

Browse files
[Fabric] Implement textAlign prop in TextInput (#14407)
* Introduce support for textAlign * Add playground textinputs demonstrating textAlign * Oops, let's remove textAlignOnUpdateProps * Change files * Lint fixes * Nit * Make textAlign to be of facebook::react::TextAlignment type * Lint fixes * Don't reinvent the wheel :D * Nit: yarn format
1 parent 8323d62 commit af6c4b2

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "Implemented textAlign in TextInput for Fabric",
4+
"packageName": "react-native-windows",
5+
"email": "14967941+danielayala94@users.noreply.github.com",
6+
"dependentChangeType": "patch"
7+
}

packages/playground/Samples/textinput.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,24 @@ export default class Bootstrap extends React.Component<{}, any> {
101101
autoCapitalize="characters"
102102
placeholder={'autoCapitalize characters'}
103103
/>
104+
<TextInput
105+
style={styles.input}
106+
textAlign={'left'}
107+
autoCapitalize="none"
108+
placeholder={'Text aligned to the left'}
109+
/>
110+
<TextInput
111+
style={styles.input}
112+
textAlign={'center'}
113+
autoCapitalize="none"
114+
placeholder={'Text aligned to the center'}
115+
/>
116+
<TextInput
117+
style={styles.input}
118+
textAlign={'right'}
119+
autoCapitalize="none"
120+
placeholder={'Text aligned to the right'}
121+
/>
104122
<TextInput
105123
// @ts-ignore
106124
ref={ref => (textInputRef = ref)}

vnext/Microsoft.ReactNative/Fabric/Composition/TextInput/WindowsTextInputComponentView.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,12 @@ void WindowsTextInputComponentView::updateProps(
10421042
autoCapitalizeOnUpdateProps(oldTextInputProps.autoCapitalize, newTextInputProps.autoCapitalize);
10431043
}
10441044

1045+
if (oldTextInputProps.textAlign != newTextInputProps.textAlign) {
1046+
// Let UpdateParaFormat() to refresh the text field with the new text alignment.
1047+
m_propBitsMask |= TXTBIT_PARAFORMATCHANGE;
1048+
m_propBits |= TXTBIT_PARAFORMATCHANGE;
1049+
}
1050+
10451051
UpdatePropertyBits();
10461052
}
10471053

@@ -1305,7 +1311,15 @@ void WindowsTextInputComponentView::UpdateParaFormat() noexcept {
13051311
m_pf.cbSize = sizeof(PARAFORMAT2);
13061312
m_pf.dwMask = PFM_ALL;
13071313

1308-
m_pf.wAlignment = PFA_LEFT;
1314+
auto &textAlign = windowsTextInputProps().textAlign;
1315+
1316+
if (textAlign == facebook::react::TextAlignment::Center) {
1317+
m_pf.wAlignment = PFA_CENTER;
1318+
} else if (textAlign == facebook::react::TextAlignment::Right) {
1319+
m_pf.wAlignment = PFA_RIGHT;
1320+
} else {
1321+
m_pf.wAlignment = PFA_LEFT;
1322+
}
13091323

13101324
m_pf.cTabCount = 1;
13111325
m_pf.rgxTabs[0] = lDefaultTab;
@@ -1525,4 +1539,4 @@ void WindowsTextInputComponentView::autoCapitalizeOnUpdateProps(
15251539
}
15261540
}
15271541

1528-
} // namespace winrt::Microsoft::ReactNative::Composition::implementation
1542+
} // namespace winrt::Microsoft::ReactNative::Composition::implementation

vnext/Microsoft.ReactNative/Fabric/Composition/TextInput/WindowsTextInputProps.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ WindowsTextInputProps::WindowsTextInputProps(
4747
autoCapitalize(convertRawProp(context, rawProps, "autoCapitalize", sourceProps.autoCapitalize, {})),
4848
clearTextOnSubmit(convertRawProp(context, rawProps, "clearTextOnSubmit", sourceProps.clearTextOnSubmit, {false})),
4949
submitKeyEvents(convertRawProp(context, rawProps, "submitKeyEvents", sourceProps.submitKeyEvents, {})),
50-
autoFocus(convertRawProp(context, rawProps, "autoFocus", sourceProps.autoFocus, {false})) {}
50+
autoFocus(convertRawProp(context, rawProps, "autoFocus", sourceProps.autoFocus, {false})),
51+
textAlign(
52+
convertRawProp(context, rawProps, "textAlign", sourceProps.textAlign, facebook::react::TextAlignment::Left)) {
53+
}
5154

5255
void WindowsTextInputProps::setProp(
5356
const PropsParserContext &context,

vnext/Microsoft.ReactNative/Fabric/Composition/TextInput/WindowsTextInputProps.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#pragma once
55

66
#include <react/components/rnwcore/Props.h>
7+
#include <react/renderer/attributedstring/conversions.h>
78
#include <react/renderer/components/text/BaseTextProps.h>
89
#include <react/renderer/core/propsConversions.h>
910

@@ -118,6 +119,7 @@ class WindowsTextInputProps final : public ViewProps, public BaseTextProps {
118119
bool clearTextOnSubmit{false};
119120
std::vector<CompWindowsTextInputSubmitKeyEventsStruct> submitKeyEvents{};
120121
bool autoFocus{false};
122+
facebook::react::TextAlignment textAlign{};
121123
};
122124

123125
} // namespace facebook::react

0 commit comments

Comments
 (0)