Skip to content

Add cursorColor support to TextInput #1787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Libraries/Components/TextInput/RCTTextInputViewConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ const RCTTextInputViewConfig = {
pastedTypes: true,
submitKeyEvents: true,
tooltip: true,
cursorColor: {process: require('../../StyleSheet/processColor').default},
// macOS]
...ConditionallyIgnoredEventHandlers({
onChange: true,
Expand Down
3 changes: 2 additions & 1 deletion Libraries/Components/TextInput/TextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,8 @@ type AndroidProps = $ReadOnly<{|
* When provided it will set the color of the cursor (or "caret") in the component.
* Unlike the behavior of `selectionColor` the cursor color will be set independently
* from the color of the text selection box.
* @platform android
// [macOS]
* @platform android macos
*/
cursorColor?: ?ColorValue,

Expand Down
3 changes: 2 additions & 1 deletion Libraries/Text/TextInput/Multiline/RCTUITextView.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ NS_ASSUME_NONNULL_BEGIN

#if TARGET_OS_OSX // [macOS
@property (nonatomic, getter=isScrollEnabled) BOOL scrollEnabled;
@property (nonatomic, strong, nullable) RCTUIColor *selectionColor; // TODO(OSS Candidate ISS#2710739)
@property (nonatomic, strong, nullable) RCTUIColor *selectionColor;
@property (nonatomic, strong, nullable) RCTUIColor *cursorColor;
@property (nonatomic, assign) UIEdgeInsets textContainerInsets;
@property (nonatomic, copy) NSString *text;
@property (nonatomic, assign) NSTextAlignment textAlignment;
Expand Down
8 changes: 7 additions & 1 deletion Libraries/Text/TextInput/Multiline/RCTUITextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,20 @@ - (void)setSelectionColor:(RCTUIColor *)selectionColor
NSMutableDictionary *selectTextAttributes = self.selectedTextAttributes.mutableCopy;
selectTextAttributes[NSBackgroundColorAttributeName] = selectionColor ?: [NSColor selectedControlColor];
self.selectedTextAttributes = selectTextAttributes.copy;
self.insertionPointColor = self.selectionColor ?: [NSColor textColor];
self.insertionPointColor = _cursorColor ?: self.selectionColor ?: [NSColor textColor];
}

- (RCTUIColor*)selectionColor
{
return (RCTUIColor*)self.selectedTextAttributes[NSBackgroundColorAttributeName];
}

- (void)setCursorColor:(NSColor *)cursorColor
{
_cursorColor = cursorColor;
self.insertionPointColor = cursorColor;
}

- (void)setEnabledTextCheckingTypes:(NSTextCheckingTypes)checkingType
{
[super setEnabledTextCheckingTypes:checkingType];
Expand Down
1 change: 1 addition & 0 deletions Libraries/Text/TextInput/RCTBaseTextInputView.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, assign) BOOL clearTextOnSubmit;
@property (nonatomic, copy, nullable) RCTDirectEventBlock onSubmitEditing;
@property (nonatomic, copy) NSArray<NSDictionary *> *submitKeyEvents;
@property (nonatomic, strong, nullable) RCTUIColor *cursorColor;
#endif // macOS]
@property (nonatomic, assign) NSInteger mostRecentEventCount;
@property (nonatomic, assign, readonly) NSInteger nativeEventCount;
Expand Down
2 changes: 2 additions & 0 deletions Libraries/Text/TextInput/RCTBaseTextInputViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ @implementation RCTBaseTextInputViewManager {
RCT_EXPORT_VIEW_PROPERTY(clearTextOnSubmit, BOOL);
RCT_EXPORT_VIEW_PROPERTY(onSubmitEditing, RCTBubblingEventBlock);
RCT_EXPORT_VIEW_PROPERTY(submitKeyEvents, NSArray<NSDictionary *>);

RCT_REMAP_VIEW_PROPERTY(cursorColor, backedTextInputView.cursorColor, UIColor)
#endif // macOS]

RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
Expand Down
14 changes: 13 additions & 1 deletion Libraries/Text/TextInput/Singleline/RCTUITextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ @interface RCTUITextFieldCell : NSTextFieldCell
@property (nonatomic, getter=isContinuousSpellCheckingEnabled) BOOL continuousSpellCheckingEnabled;
@property (nonatomic, getter=isGrammarCheckingEnabled) BOOL grammarCheckingEnabled;
@property (nonatomic, strong, nullable) RCTUIColor *selectionColor;
@property (nonatomic, strong, nullable) RCTUIColor *insertionPointColor;

@end

Expand Down Expand Up @@ -77,7 +78,8 @@ - (NSText *)setUpFieldEditorAttributes:(NSText *)textObj
fieldEditor.grammarCheckingEnabled = self.isGrammarCheckingEnabled;
NSMutableDictionary *selectTextAttributes = fieldEditor.selectedTextAttributes.mutableCopy;
selectTextAttributes[NSBackgroundColorAttributeName] = self.selectionColor ?: [NSColor selectedControlColor];
fieldEditor.selectedTextAttributes = selectTextAttributes;
fieldEditor.selectedTextAttributes = selectTextAttributes;
fieldEditor.insertionPointColor = self.insertionPointColor ?: [NSColor textColor];
return fieldEditor;
}

Expand Down Expand Up @@ -262,6 +264,16 @@ - (RCTUIColor*)selectionColor
{
return ((RCTUITextFieldCell*)self.cell).selectionColor;
}

- (void)setCursorColor:(NSColor *)cursorColor
{
((RCTUITextFieldCell*)self.cell).insertionPointColor = cursorColor;
}

- (RCTUIColor*)cursorColor
{
return ((RCTUITextFieldCell*)self.cell).insertionPointColor;
}

- (void)setFont:(UIFont *)font
{
Expand Down
49 changes: 49 additions & 0 deletions packages/rn-tester/js/examples/TextInput/TextInputExample.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,55 @@ if (Platform.OS === 'macos') {
return <OnPaste />;
},
},
{
title: 'Cursor color',
render: function (): React.Node {
return (
<View>
<TextInput
placeholder="Single line"
cursorColor="#00FF00"
placeholderTextColor="grey"
style={[
styles.default,
{backgroundColor: 'black', color: 'white', marginBottom: 4},
]}
/>
<TextInput
multiline={true}
placeholder="Multiline"
cursorColor="#00FF00"
placeholderTextColor="grey"
style={[
styles.default,
{backgroundColor: 'black', color: 'white', marginBottom: 4},
]}
/>
<TextInput
placeholder="Single line with selection color"
cursorColor="#00FF00"
selectionColor="yellow"
placeholderTextColor="grey"
style={[
styles.default,
{backgroundColor: 'black', color: 'white', marginBottom: 4},
]}
/>
<TextInput
multiline={true}
placeholder="Multiline with selection color"
cursorColor="#00FF00"
selectionColor="yellow"
placeholderTextColor="grey"
style={[
styles.default,
{backgroundColor: 'black', color: 'white'},
]}
/>
</View>
);
},
},
);
}
// [macOS]