Skip to content

Commit 3e66909

Browse files
dlitsmanDmitrii Litsman
andauthored
Add cursorColor support to TextInput (#1787)
* Add cursorColor support to TextInput * Better organize macOS comments and rely on autosynthesized behavior. * Use RCT_REMAP_VIEW_PROPERTY instead of RCT_REMAP_OSX_VIEW_PROPERTY --------- Co-authored-by: Dmitrii Litsman <[email protected]>
1 parent 535dd99 commit 3e66909

File tree

8 files changed

+77
-4
lines changed

8 files changed

+77
-4
lines changed

Libraries/Components/TextInput/RCTTextInputViewConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ const RCTTextInputViewConfig = {
163163
pastedTypes: true,
164164
submitKeyEvents: true,
165165
tooltip: true,
166+
cursorColor: {process: require('../../StyleSheet/processColor').default},
166167
// macOS]
167168
...ConditionallyIgnoredEventHandlers({
168169
onChange: true,

Libraries/Components/TextInput/TextInput.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,8 @@ type AndroidProps = $ReadOnly<{|
612612
* When provided it will set the color of the cursor (or "caret") in the component.
613613
* Unlike the behavior of `selectionColor` the cursor color will be set independently
614614
* from the color of the text selection box.
615-
* @platform android
615+
// [macOS]
616+
* @platform android macos
616617
*/
617618
cursorColor?: ?ColorValue,
618619

Libraries/Text/TextInput/Multiline/RCTUITextView.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ NS_ASSUME_NONNULL_BEGIN
4747

4848
#if TARGET_OS_OSX // [macOS
4949
@property (nonatomic, getter=isScrollEnabled) BOOL scrollEnabled;
50-
@property (nonatomic, strong, nullable) RCTUIColor *selectionColor; // TODO(OSS Candidate ISS#2710739)
50+
@property (nonatomic, strong, nullable) RCTUIColor *selectionColor;
51+
@property (nonatomic, strong, nullable) RCTUIColor *cursorColor;
5152
@property (nonatomic, assign) UIEdgeInsets textContainerInsets;
5253
@property (nonatomic, copy) NSString *text;
5354
@property (nonatomic, assign) NSTextAlignment textAlignment;

Libraries/Text/TextInput/Multiline/RCTUITextView.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,20 @@ - (void)setSelectionColor:(RCTUIColor *)selectionColor
152152
NSMutableDictionary *selectTextAttributes = self.selectedTextAttributes.mutableCopy;
153153
selectTextAttributes[NSBackgroundColorAttributeName] = selectionColor ?: [NSColor selectedControlColor];
154154
self.selectedTextAttributes = selectTextAttributes.copy;
155-
self.insertionPointColor = self.selectionColor ?: [NSColor textColor];
155+
self.insertionPointColor = _cursorColor ?: self.selectionColor ?: [NSColor textColor];
156156
}
157157

158158
- (RCTUIColor*)selectionColor
159159
{
160160
return (RCTUIColor*)self.selectedTextAttributes[NSBackgroundColorAttributeName];
161161
}
162162

163+
- (void)setCursorColor:(NSColor *)cursorColor
164+
{
165+
_cursorColor = cursorColor;
166+
self.insertionPointColor = cursorColor;
167+
}
168+
163169
- (void)setEnabledTextCheckingTypes:(NSTextCheckingTypes)checkingType
164170
{
165171
[super setEnabledTextCheckingTypes:checkingType];

Libraries/Text/TextInput/RCTBaseTextInputView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ NS_ASSUME_NONNULL_BEGIN
5050
@property (nonatomic, assign) BOOL clearTextOnSubmit;
5151
@property (nonatomic, copy, nullable) RCTDirectEventBlock onSubmitEditing;
5252
@property (nonatomic, copy) NSArray<NSDictionary *> *submitKeyEvents;
53+
@property (nonatomic, strong, nullable) RCTUIColor *cursorColor;
5354
#endif // macOS]
5455
@property (nonatomic, assign) NSInteger mostRecentEventCount;
5556
@property (nonatomic, assign, readonly) NSInteger nativeEventCount;

Libraries/Text/TextInput/RCTBaseTextInputViewManager.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ @implementation RCTBaseTextInputViewManager {
7474
RCT_EXPORT_VIEW_PROPERTY(clearTextOnSubmit, BOOL);
7575
RCT_EXPORT_VIEW_PROPERTY(onSubmitEditing, RCTBubblingEventBlock);
7676
RCT_EXPORT_VIEW_PROPERTY(submitKeyEvents, NSArray<NSDictionary *>);
77+
78+
RCT_REMAP_VIEW_PROPERTY(cursorColor, backedTextInputView.cursorColor, UIColor)
7779
#endif // macOS]
7880

7981
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)

Libraries/Text/TextInput/Singleline/RCTUITextField.m

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ @interface RCTUITextFieldCell : NSTextFieldCell
3030
@property (nonatomic, getter=isContinuousSpellCheckingEnabled) BOOL continuousSpellCheckingEnabled;
3131
@property (nonatomic, getter=isGrammarCheckingEnabled) BOOL grammarCheckingEnabled;
3232
@property (nonatomic, strong, nullable) RCTUIColor *selectionColor;
33+
@property (nonatomic, strong, nullable) RCTUIColor *insertionPointColor;
3334

3435
@end
3536

@@ -77,7 +78,8 @@ - (NSText *)setUpFieldEditorAttributes:(NSText *)textObj
7778
fieldEditor.grammarCheckingEnabled = self.isGrammarCheckingEnabled;
7879
NSMutableDictionary *selectTextAttributes = fieldEditor.selectedTextAttributes.mutableCopy;
7980
selectTextAttributes[NSBackgroundColorAttributeName] = self.selectionColor ?: [NSColor selectedControlColor];
80-
fieldEditor.selectedTextAttributes = selectTextAttributes;
81+
fieldEditor.selectedTextAttributes = selectTextAttributes;
82+
fieldEditor.insertionPointColor = self.insertionPointColor ?: [NSColor textColor];
8183
return fieldEditor;
8284
}
8385

@@ -262,6 +264,16 @@ - (RCTUIColor*)selectionColor
262264
{
263265
return ((RCTUITextFieldCell*)self.cell).selectionColor;
264266
}
267+
268+
- (void)setCursorColor:(NSColor *)cursorColor
269+
{
270+
((RCTUITextFieldCell*)self.cell).insertionPointColor = cursorColor;
271+
}
272+
273+
- (RCTUIColor*)cursorColor
274+
{
275+
return ((RCTUITextFieldCell*)self.cell).insertionPointColor;
276+
}
265277

266278
- (void)setFont:(UIFont *)font
267279
{

packages/rn-tester/js/examples/TextInput/TextInputExample.ios.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,55 @@ if (Platform.OS === 'macos') {
11071107
return <OnPaste />;
11081108
},
11091109
},
1110+
{
1111+
title: 'Cursor color',
1112+
render: function (): React.Node {
1113+
return (
1114+
<View>
1115+
<TextInput
1116+
placeholder="Single line"
1117+
cursorColor="#00FF00"
1118+
placeholderTextColor="grey"
1119+
style={[
1120+
styles.default,
1121+
{backgroundColor: 'black', color: 'white', marginBottom: 4},
1122+
]}
1123+
/>
1124+
<TextInput
1125+
multiline={true}
1126+
placeholder="Multiline"
1127+
cursorColor="#00FF00"
1128+
placeholderTextColor="grey"
1129+
style={[
1130+
styles.default,
1131+
{backgroundColor: 'black', color: 'white', marginBottom: 4},
1132+
]}
1133+
/>
1134+
<TextInput
1135+
placeholder="Single line with selection color"
1136+
cursorColor="#00FF00"
1137+
selectionColor="yellow"
1138+
placeholderTextColor="grey"
1139+
style={[
1140+
styles.default,
1141+
{backgroundColor: 'black', color: 'white', marginBottom: 4},
1142+
]}
1143+
/>
1144+
<TextInput
1145+
multiline={true}
1146+
placeholder="Multiline with selection color"
1147+
cursorColor="#00FF00"
1148+
selectionColor="yellow"
1149+
placeholderTextColor="grey"
1150+
style={[
1151+
styles.default,
1152+
{backgroundColor: 'black', color: 'white'},
1153+
]}
1154+
/>
1155+
</View>
1156+
);
1157+
},
1158+
},
11101159
);
11111160
}
11121161
// [macOS]

0 commit comments

Comments
 (0)