Skip to content

Commit 187f3a6

Browse files
authored
[Fabric] Fix OnSelectionChanged running multiple times (#12903)
* remove extra method calls * add check for call coming from state * Change files * update snapshot
1 parent 926121b commit 187f3a6

File tree

3 files changed

+13
-21
lines changed

3 files changed

+13
-21
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": "fix OnSelectionChanged running multiple times",
4+
"packageName": "react-native-windows",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

packages/e2e-test-app-fabric/test/__snapshots__/LegacyTextInputTest.test.ts.snap

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ onKeyPress key: c
104104
onChange text: ab
105105
onSelectionChange range: 2,2
106106
onKeyPress key: b
107-
onSelectionChange range: 1,1
108107
onChange text: a
109-
onSelectionChange range: 0,0
110108
onChange text: a
111109
onSelectionChange range: 1,1
112110
onKeyPress key: a
@@ -125,7 +123,7 @@ onFocus
125123
"Opacity": 1,
126124
"Size": [
127125
998,
128-
858,
126+
820,
129127
],
130128
"Visual Type": "SpriteVisual",
131129
},
@@ -147,9 +145,7 @@ onKeyPress key: c
147145
onChange text: ab
148146
onSelectionChange range: 2,2
149147
onKeyPress key: b
150-
onSelectionChange range: 1,1
151148
onChange text: a
152-
onSelectionChange range: 0,0
153149
onChange text: a
154150
onSelectionChange range: 1,1
155151
onKeyPress key: a
@@ -168,7 +164,7 @@ onFocus
168164
"Opacity": 1,
169165
"Size": [
170166
998,
171-
299,
167+
262,
172168
],
173169
"Visual Type": "SpriteVisual",
174170
},
@@ -213,9 +209,7 @@ onKeyPress key: c
213209
onChange text: ab
214210
onSelectionChange range: 2,2
215211
onKeyPress key: b
216-
onSelectionChange range: 1,1
217212
onChange text: a
218-
onSelectionChange range: 0,0
219213
onChange text: a
220214
onSelectionChange range: 1,1
221215
onKeyPress key: a
@@ -234,7 +228,7 @@ onFocus
234228
"Opacity": 1,
235229
"Size": [
236230
998,
237-
727,
231+
690,
238232
],
239233
"Visual Type": "SpriteVisual",
240234
},

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,8 +1113,6 @@ void WindowsTextInputComponentView::updateState(
11131113
return;
11141114
}
11151115

1116-
auto data = m_state->getData();
1117-
11181116
if (!oldState) {
11191117
m_mostRecentEventCount = m_state->getData().mostRecentEventCount;
11201118
}
@@ -1123,19 +1121,11 @@ void WindowsTextInputComponentView::updateState(
11231121
m_comingFromState = true;
11241122
// Only handle single/empty fragments right now -- ignore the other fragments
11251123

1126-
LRESULT res;
1127-
CHARRANGE cr;
1128-
cr.cpMin = cr.cpMax = 0;
1129-
winrt::check_hresult(m_textServices->TxSendMessage(EM_EXGETSEL, 0, reinterpret_cast<LPARAM>(&cr), &res));
1130-
11311124
UpdateText(
11321125
m_state->getData().attributedString.getFragments().size()
11331126
? m_state->getData().attributedString.getFragments()[0].string
11341127
: "");
11351128

1136-
winrt::check_hresult(
1137-
m_textServices->TxSendMessage(EM_SETSEL, static_cast<WPARAM>(cr.cpMin), static_cast<LPARAM>(cr.cpMax), &res));
1138-
11391129
m_comingFromState = false;
11401130
}
11411131
}
@@ -1154,7 +1144,8 @@ void WindowsTextInputComponentView::UpdateText(const std::string &str) noexcept
11541144
winrt::check_hresult(m_textServices->TxSendMessage(
11551145
EM_SETTEXTEX, reinterpret_cast<WPARAM>(&stt), reinterpret_cast<LPARAM>(str.c_str()), &res));
11561146

1157-
winrt::check_hresult(m_textServices->TxSendMessage(EM_EXGETSEL, 0, reinterpret_cast<LPARAM>(&cr), &res));
1147+
winrt::check_hresult(
1148+
m_textServices->TxSendMessage(EM_SETSEL, static_cast<WPARAM>(cr.cpMin), static_cast<LPARAM>(cr.cpMax), &res));
11581149

11591150
// enable colored emojis
11601151
winrt::check_hresult(
@@ -1225,7 +1216,7 @@ void WindowsTextInputComponentView::OnTextUpdated() noexcept {
12251216
}
12261217

12271218
void WindowsTextInputComponentView::OnSelectionChanged(LONG start, LONG end) noexcept {
1228-
if (m_eventEmitter /* && !m_comingFromJS ?? */) {
1219+
if (m_eventEmitter && !m_comingFromState /* && !m_comingFromJS ?? */) {
12291220
auto emitter = std::static_pointer_cast<const facebook::react::WindowsTextInputEventEmitter>(m_eventEmitter);
12301221
facebook::react::WindowsTextInputEventEmitter::OnSelectionChange onSelectionChangeArgs;
12311222
onSelectionChangeArgs.selection.start = start;

0 commit comments

Comments
 (0)