RCTPickerManager.m 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import "RCTPickerManager.h"
  8. #import <React/RCTUIManager.h>
  9. #import "RCTBridge.h"
  10. #import "RCTFont.h"
  11. #import "RCTPicker.h"
  12. @implementation RCTPickerManager
  13. RCT_EXPORT_MODULE()
  14. - (UIView *)view
  15. {
  16. return [RCTPicker new];
  17. }
  18. RCT_EXPORT_VIEW_PROPERTY(items, NSArray<NSDictionary *>)
  19. RCT_EXPORT_VIEW_PROPERTY(selectedIndex, NSInteger)
  20. RCT_REMAP_VIEW_PROPERTY(accessibilityLabel, reactAccessibilityElement.accessibilityLabel, NSString)
  21. RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
  22. RCT_EXPORT_VIEW_PROPERTY(color, UIColor)
  23. RCT_EXPORT_VIEW_PROPERTY(textAlign, NSTextAlignment)
  24. RCT_CUSTOM_VIEW_PROPERTY(fontSize, NSNumber, RCTPicker)
  25. {
  26. view.font = [RCTFont updateFont:view.font withSize:json ?: @(defaultView.font.pointSize)];
  27. }
  28. RCT_CUSTOM_VIEW_PROPERTY(fontWeight, NSString, __unused RCTPicker)
  29. {
  30. view.font = [RCTFont updateFont:view.font withWeight:json]; // defaults to normal
  31. }
  32. RCT_CUSTOM_VIEW_PROPERTY(fontStyle, NSString, __unused RCTPicker)
  33. {
  34. view.font = [RCTFont updateFont:view.font withStyle:json]; // defaults to normal
  35. }
  36. RCT_CUSTOM_VIEW_PROPERTY(fontFamily, NSString, RCTPicker)
  37. {
  38. view.font = [RCTFont updateFont:view.font withFamily:json ?: defaultView.font.familyName];
  39. }
  40. RCT_EXPORT_METHOD(setNativeSelectedIndex : (nonnull NSNumber *)viewTag toIndex : (nonnull NSNumber *)index)
  41. {
  42. [self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
  43. UIView *view = viewRegistry[viewTag];
  44. if ([view isKindOfClass:[RCTPicker class]]) {
  45. [(RCTPicker *)view setSelectedIndex:index.integerValue];
  46. } else {
  47. // This component is used in Fabric through LegacyInteropLayer.
  48. // `RCTPicker` view is subview of `RCTLegacyViewManagerInteropComponentView`.
  49. // `viewTag` passed as parameter to this method is tag of the `RCTLegacyViewManagerInteropComponentView`.
  50. UIView *subview = view.subviews.firstObject;
  51. if ([subview isKindOfClass:[RCTPicker class]]) {
  52. [(RCTPicker *)subview setSelectedIndex:index.integerValue];
  53. } else {
  54. RCTLogError(@"view type must be RCTPicker");
  55. }
  56. }
  57. }];
  58. }
  59. @end