Skip to content

Commit c5c60df

Browse files
authored
Merge pull request #50 from react-native-community/@vonovak/add-selectedTextColor
add active text color support
2 parents 9d7328a + 2f67c42 commit c5c60df

5 files changed

+30
-44
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ type SegmentedControlIOSProps = $ReadOnly<{|
112112
* Text color of the control.
113113
*/
114114
textColor?: ?string,
115+
/**
116+
* Text color of the control when selected.
117+
* NOTE: this prop will only work for iOS >= 13
118+
*/
119+
activeTextColor?: ?string,
115120
/**
116121
* (For iOS >= 13)
117122
* Background color of the control.

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)setActiveTextColor:(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(activeTextColor, 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: 6 additions & 3 deletions
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.
@@ -52,17 +52,20 @@ type SegmentedControlIOSProps = $ReadOnly<{|
5252
*/
5353
tintColor?: ?string,
5454
/**
55-
*
5655
* Text color of the control.
5756
* NOTE: this prop will only work for iOS >= 13
5857
*/
5958
textColor?: ?string,
59+
/**
60+
* Text color of the control when selected.
61+
* NOTE: this prop will only work for iOS >= 13
62+
*/
63+
activeTextColor?: ?string,
6064
/**
6165
* Background color of the control.
6266
* NOTE: this prop will only work for iOS >= 13
6367
*/
6468
backgroundColor?: ?string,
65-
/**
6669
/**
6770
* If true, then selecting a segment won't persist visually.
6871
* The `onValueChange` callback will still work as expected.

js/SegmentedControlIOS.ios.js

Lines changed: 7 additions & 39 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>,
@@ -118,8 +84,10 @@ const styles = StyleSheet.create({
11884
},
11985
});
12086

121-
// $FlowFixMe
122-
const SegmentedControlIOSWithRef = React.forwardRef(
87+
const SegmentedControlIOSWithRef = React.forwardRef<
88+
SegmentedControlIOSProps,
89+
RNCSegmentedControlNativeComponent,
90+
>(
12391
(
12492
props: SegmentedControlIOSProps,
12593
forwardedRef: ?React.Ref<typeof RNCSegmentedControlNativeComponent>,

0 commit comments

Comments
 (0)