Skip to content

Commit cafd1a2

Browse files
shwantonSaadnajmi
authored andcommitted
[fabric] TextInput should handle pasted types prop
Summary: **Context** - Configuring paste options was never implemented in our Fabric TextInput - Paper Implementation: https://www.internalfb.com/code/fbsource/[93ff90a797da]/third-party/microsoft-fork-of-react-native/react-native-macos/packages/react-native/Libraries/Text/TextInput/Multiline/RCTMultilineTextInputViewManager.m?lines=29-35 https://www.internalfb.com/code/fbsource/[563690cf01f1]/third-party/microsoft-fork-of-react-native/react-native-macos/packages/react-native/React/Base/RCTConvert.m?lines=1239 **Change** - Add c++ enum/prop - Add conversion - Add `pastedTypes` to Fabric component Test Plan: |Fabric|Paper| | https://pxl.cl/4LcPw | https://pxl.cl/4LcQL| Reviewers: lefever, #rn-desktop Differential Revision: https://phabricator.intern.facebook.com/D56558929 Tasks: T186086013
1 parent af67f70 commit cafd1a2

File tree

9 files changed

+91
-1
lines changed

9 files changed

+91
-1
lines changed

packages/react-native/Libraries/Text/TextInput/RCTBackedTextInputViewProtocol.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ NS_ASSUME_NONNULL_BEGIN
8888
#if TARGET_OS_OSX // [macOS
8989
// UITextInput method for OSX
9090
- (CGSize)sizeThatFits:(CGSize)size;
91+
- (void)setReadablePasteBoardTypes:(NSArray<NSPasteboardType> *)readablePasteboardTypes;
9192
#endif // macOS]
9293

9394
// This protocol disallows direct access to `text` property because

packages/react-native/Libraries/Text/TextInput/Singleline/RCTUITextField.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ NS_ASSUME_NONNULL_BEGIN
6969
@property (nonatomic, strong, nullable) RCTUIColor *selectionColor;
7070
@property (weak, nullable) id<RCTUITextFieldDelegate> delegate;
7171
@property (nonatomic, assign) CGFloat pointScaleFactor;
72+
73+
- (void)setReadablePasteBoardTypes:(NSArray<NSPasteboardType> *)readablePasteboardTypes;
7274
#endif // macOS]
7375

7476
@property (nonatomic, getter=isGhostTextChanging) BOOL ghostTextChanging; // [macOS]

packages/react-native/Libraries/Text/TextInput/Singleline/RCTUITextField.mm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ @implementation RCTUITextField {
9999
#endif // [macOS]
100100
#if TARGET_OS_OSX // [macOS
101101
BOOL _isUpdatingPlaceholderText;
102+
NSArray<NSPasteboardType> *_readablePasteboardTypes;
102103
#endif // macOS]
103104
}
104105

@@ -705,5 +706,12 @@ - (void)keyUp:(NSEvent *)event {
705706
}
706707
}
707708
#endif // macOS]
709+
710+
#if TARGET_OS_OSX // [macOS
711+
- (void)setReadablePasteBoardTypes:(NSArray<NSPasteboardType> *)readablePasteboardTypes
712+
{
713+
_readablePasteboardTypes = readablePasteboardTypes;
714+
}
715+
#endif // macOS]
708716

709717
@end

packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,13 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
344344
_backedTextInputView.disableKeyboardShortcuts = newTextInputProps.disableKeyboardShortcuts;
345345
}
346346

347+
#if TARGET_OS_OSX // [macOS
348+
if (newTextInputProps.traits.pastedTypes!= oldTextInputProps.traits.pastedTypes) {
349+
NSArray<NSPasteboardType> *types = RCTPasteboardTypeArrayFromProps(newTextInputProps.traits.pastedTypes);
350+
[_backedTextInputView setReadablePasteBoardTypes:types];
351+
}
352+
#endif // macOS]
353+
347354
[super updateProps:props oldProps:oldProps];
348355

349356
#if TARGET_OS_IOS // [macOS] [visionOS]

packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
623623
self.toolTip = nil;
624624
}
625625
}
626-
#endif // [macOS]
626+
#endif // macOS]
627627

628628
_needsInvalidateLayer = _needsInvalidateLayer || needsInvalidateLayer;
629629

packages/react-native/React/Fabric/RCTConversions.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
#import <react/renderer/graphics/RCTPlatformColorUtils.h>
1515
#import <react/renderer/graphics/Transform.h>
1616

17+
#if TARGET_OS_OSX // [macOS
18+
#import <react/renderer/components/iostextinput/primitives.h>
19+
#endif // macOS]
20+
1721
NS_ASSUME_NONNULL_BEGIN
1822

1923
inline NSString *RCTNSStringFromString(
@@ -281,4 +285,29 @@ inline facebook::react::LayoutDirection RCTLayoutDirection(BOOL isRTL)
281285
return isRTL ? facebook::react::LayoutDirection::RightToLeft : facebook::react::LayoutDirection::LeftToRight;
282286
}
283287

288+
#if TARGET_OS_OSX // [macOS
289+
inline NSArray<NSPasteboardType> *RCTPasteboardTypeArrayFromProps(const std::vector<facebook::react::PastedTypesType> &pastedTypes)
290+
{
291+
NSMutableArray<NSPasteboardType> *types = [NSMutableArray new];
292+
293+
for (const auto &type : pastedTypes) {
294+
switch (type) {
295+
case facebook::react::PastedTypesType::FileUrl:
296+
[types addObjectsFromArray:@[NSFilenamesPboardType]];
297+
break;
298+
case facebook::react::PastedTypesType::Image:
299+
[types addObjectsFromArray:@[NSPasteboardTypePNG, NSPasteboardTypeTIFF]];
300+
break;
301+
case facebook::react::PastedTypesType::String:
302+
[types addObjectsFromArray:@[NSPasteboardTypeString]];
303+
break;
304+
default:
305+
break;
306+
}
307+
}
308+
309+
return [types copy];
310+
}
311+
#endif // macOS]
312+
284313
NS_ASSUME_NONNULL_END

packages/react-native/ReactCommon/react/renderer/components/textinput/platform/ios/react/renderer/components/iostextinput/conversions.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,26 @@ inline void fromRawValue(
217217
abort();
218218
}
219219

220+
#ifdef TARGET_OS_OSX // [macOS
221+
inline void fromRawValue(
222+
const PropsParserContext &context,
223+
const RawValue &value,
224+
PastedTypesType &result) {
225+
auto string = (std::string)value;
226+
if (string == "fileUrl") {
227+
result = PastedTypesType::FileUrl;
228+
return;
229+
}
230+
if (string == "image") {
231+
result = PastedTypesType::Image;
232+
return;
233+
}
234+
if (string == "string") {
235+
result = PastedTypesType::String;
236+
return;
237+
}
238+
abort();
239+
}
240+
#endif // macOS]
241+
220242
} // namespace facebook::react

packages/react-native/ReactCommon/react/renderer/components/textinput/platform/ios/react/renderer/components/iostextinput/primitives.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ class Selection final {
8383
int end{0};
8484
};
8585

86+
#if TARGET_OS_OSX // [macOS
87+
enum class PastedTypesType {
88+
FileUrl,
89+
Image,
90+
String,
91+
};
92+
#endif // macOS]
93+
8694
/*
8795
* Controls features of text inputs.
8896
*/
@@ -227,6 +235,13 @@ class TextInputTraits final {
227235
* Default value: `empty` (`null`).
228236
*/
229237
std::optional<bool> grammarCheck{};
238+
239+
/*
240+
* List of pastable types
241+
* macOS-only
242+
* Default value: `empty list`
243+
*/
244+
std::vector<PastedTypesType> pastedTypes{};
230245
#endif // macOS]
231246
};
232247

packages/react-native/ReactCommon/react/renderer/components/textinput/platform/ios/react/renderer/components/iostextinput/propsConversions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ static TextInputTraits convertRawProp(
143143
"grammarCheck",
144144
sourceTraits.grammarCheck,
145145
defaultTraits.grammarCheck);
146+
traits.pastedTypes = convertRawProp(
147+
context,
148+
rawProps,
149+
"pastedTypes",
150+
sourceTraits.pastedTypes,
151+
defaultTraits.pastedTypes);
146152
#endif // macOS]
147153

148154
traits.dataDetectorTypes = convertRawProp(

0 commit comments

Comments
 (0)