Skip to content

Commit a426efa

Browse files
committed
support separate prop for UIControlStateSelected
1 parent 9d7328a commit a426efa

4 files changed

+22
-40
lines changed

ios/RNCSegmentedControl.m

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,24 @@ - (void)setTextColor:(UIColor *)textColor
6060
#endif
6161
}
6262

63+
- (void)setSelectedTextColor:(UIColor *)textColor
64+
{
65+
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
66+
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
67+
if (@available(iOS 13.0, *)) {
68+
[self setTitleTextAttributes:@{NSForegroundColorAttributeName: textColor}
69+
forState:UIControlStateSelected];
70+
}
71+
#endif
72+
}
73+
6374
- (void)setTintColor:(UIColor *)tintColor
6475
{
6576
[super setTintColor:tintColor];
6677
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
6778
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
6879
if (@available(iOS 13.0, *)) {
6980
[self setSelectedSegmentTintColor:tintColor];
70-
[self setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}
71-
forState:UIControlStateSelected];
7281
[self setTitleTextAttributes:@{NSForegroundColorAttributeName: tintColor}
7382
forState:UIControlStateNormal];
7483
}

ios/RNCSegmentedControlManager.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ - (UIView *)view
2525
RCT_EXPORT_VIEW_PROPERTY(tintColor, UIColor)
2626
RCT_EXPORT_VIEW_PROPERTY(backgroundColor, UIColor)
2727
RCT_EXPORT_VIEW_PROPERTY(textColor, UIColor)
28+
RCT_EXPORT_VIEW_PROPERTY(selectedTextColor, UIColor)
2829
RCT_EXPORT_VIEW_PROPERTY(momentary, BOOL)
2930
RCT_EXPORT_VIEW_PROPERTY(enabled, BOOL)
3031
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)

js/RNCSegmentedControlNativeComponent.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type Event = SyntheticEvent<
2222
|}>,
2323
>;
2424

25-
type SegmentedControlIOSProps = $ReadOnly<{|
25+
export type SegmentedControlIOSProps = $ReadOnly<{|
2626
...ViewProps,
2727
/**
2828
* The labels for the control's segment buttons, in order.
@@ -57,6 +57,12 @@ type SegmentedControlIOSProps = $ReadOnly<{|
5757
* NOTE: this prop will only work for iOS >= 13
5858
*/
5959
textColor?: ?string,
60+
/**
61+
*
62+
* Text color of the control when selected.
63+
* NOTE: this prop will only work for iOS >= 13
64+
*/
65+
selectedTextColor?: ?string,
6066
/**
6167
* Background color of the control.
6268
* NOTE: this prop will only work for iOS >= 13

js/SegmentedControlIOS.ios.js

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import {StyleSheet} from 'react-native';
1616
import type {SyntheticEvent} from 'react-native/Libraries/Types/CoreEventTypes';
1717
import type {ViewProps} from 'react-native/Libraries/Components/View/ViewPropTypes';
1818

19-
import RNCSegmentedControlNativeComponent from './RNCSegmentedControlNativeComponent';
19+
import RNCSegmentedControlNativeComponent, {
20+
type SegmentedControlIOSProps,
21+
} from './RNCSegmentedControlNativeComponent';
2022

2123
type Event = SyntheticEvent<
2224
$ReadOnly<{|
@@ -25,42 +27,6 @@ type Event = SyntheticEvent<
2527
|}>,
2628
>;
2729

28-
type SegmentedControlIOSProps = $ReadOnly<{|
29-
...ViewProps,
30-
/**
31-
* The labels for the control's segment buttons, in order.
32-
*/
33-
values?: $ReadOnlyArray<string>,
34-
/**
35-
* The index in `props.values` of the segment to be (pre)selected.
36-
*/
37-
selectedIndex?: ?number,
38-
/**
39-
* Callback that is called when the user taps a segment;
40-
* passes the segment's value as an argument
41-
*/
42-
onValueChange?: ?(value: number) => mixed,
43-
/**
44-
* Callback that is called when the user taps a segment;
45-
* passes the event as an argument
46-
*/
47-
onChange?: ?(event: Event) => mixed,
48-
/**
49-
* If false the user won't be able to interact with the control.
50-
* Default value is true.
51-
*/
52-
enabled?: boolean,
53-
/**
54-
* Accent color of the control.
55-
*/
56-
tintColor?: ?string,
57-
/**
58-
* If true, then selecting a segment won't persist visually.
59-
* The `onValueChange` callback will still work as expected.
60-
*/
61-
momentary?: ?boolean,
62-
|}>;
63-
6430
type Props = $ReadOnly<{|
6531
...SegmentedControlIOSProps,
6632
forwardedRef: ?React.Ref<typeof RNCSegmentedControlNativeComponent>,

0 commit comments

Comments
 (0)