RCTPicker.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "RCTPicker.h"
  8. #import "RCTConvert.h"
  9. #import "RCTUtils.h"
  10. @interface RCTPicker () <UIPickerViewDataSource, UIPickerViewDelegate, UIPickerViewAccessibilityDelegate>
  11. @end
  12. @implementation RCTPicker
  13. - (instancetype)initWithFrame:(CGRect)frame
  14. {
  15. if ((self = [super initWithFrame:frame])) {
  16. _color = [UIColor blackColor];
  17. _font = [UIFont systemFontOfSize:21]; // TODO: selected title default should be 23.5
  18. _selectedIndex = NSNotFound;
  19. _textAlign = NSTextAlignmentCenter;
  20. self.delegate = self;
  21. [self selectRow:0 inComponent:0
  22. animated:
  23. YES]; // Workaround for missing selection indicator lines (see
  24. // https://stackoverflow.com/questions/39564660/uipickerview-selection-indicator-not-visible-in-ios10)
  25. }
  26. return self;
  27. }
  28. RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : (NSCoder *)aDecoder)
  29. - (void)setItems:(NSArray<NSDictionary *> *)items
  30. {
  31. _items = [items copy];
  32. [self setNeedsLayout];
  33. }
  34. - (void)setSelectedIndex:(NSInteger)selectedIndex
  35. {
  36. if (_selectedIndex != selectedIndex) {
  37. BOOL animated = _selectedIndex != NSNotFound; // Don't animate the initial value
  38. _selectedIndex = selectedIndex;
  39. dispatch_async(dispatch_get_main_queue(), ^{
  40. [self selectRow:selectedIndex inComponent:0 animated:animated];
  41. });
  42. }
  43. }
  44. #pragma mark - UIPickerViewDataSource protocol
  45. - (NSInteger)numberOfComponentsInPickerView:(__unused UIPickerView *)pickerView
  46. {
  47. return 1;
  48. }
  49. - (NSInteger)pickerView:(__unused UIPickerView *)pickerView numberOfRowsInComponent:(__unused NSInteger)component
  50. {
  51. return _items.count;
  52. }
  53. #pragma mark - UIPickerViewDelegate methods
  54. - (NSString *)pickerView:(__unused UIPickerView *)pickerView
  55. titleForRow:(NSInteger)row
  56. forComponent:(__unused NSInteger)component
  57. {
  58. return [RCTConvert NSString:_items[row][@"label"]];
  59. }
  60. - (CGFloat)pickerView:(__unused UIPickerView *)pickerView rowHeightForComponent:(NSInteger)__unused component
  61. {
  62. return _font.pointSize + 19;
  63. }
  64. - (UIView *)pickerView:(UIPickerView *)pickerView
  65. viewForRow:(NSInteger)row
  66. forComponent:(NSInteger)component
  67. reusingView:(UILabel *)label
  68. {
  69. if (!label) {
  70. label = [[UILabel alloc] initWithFrame:(CGRect){CGPointZero,
  71. {
  72. [pickerView rowSizeForComponent:component].width,
  73. [pickerView rowSizeForComponent:component].height,
  74. }}];
  75. }
  76. label.font = _font;
  77. label.textColor = [RCTConvert UIColor:_items[row][@"textColor"]] ?: _color;
  78. label.textAlignment = _textAlign;
  79. label.text = [self pickerView:pickerView titleForRow:row forComponent:component];
  80. return label;
  81. }
  82. - (void)pickerView:(__unused UIPickerView *)pickerView
  83. didSelectRow:(NSInteger)row
  84. inComponent:(__unused NSInteger)component
  85. {
  86. _selectedIndex = row;
  87. if (_onChange && _items.count > (NSUInteger)row) {
  88. _onChange(@{
  89. @"newIndex" : @(row),
  90. @"newValue" : RCTNullIfNil(_items[row][@"value"]),
  91. });
  92. }
  93. }
  94. #pragma mark - UIPickerViewAccessibilityDelegate protocol
  95. - (NSString *)pickerView:(UIPickerView *)pickerView accessibilityLabelForComponent:(NSInteger)component
  96. {
  97. return super.accessibilityLabel;
  98. }
  99. @end