Skip to content

Commit 0185663

Browse files
numandev1facebook-github-bot
authored andcommitted
feat: add cancelButtonTintColor props in ActionSheetIOS for change cancel button tint color (#31972)
Summary: we need to change the text color of the cancel button in `ActionSheetIOS` but `tintColor` changes the all button text color except `destructiveButtonIndex` so I have added `cancelButtonTintColor` prop to change only the text color of the cancel button ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://github.com/facebook/react-native/wiki/Changelog --> [General] [Changed] - added `cancelButtonTintColor` prop for `ActionSheetIOS` to change only the text color of the cancel button Pull Request resolved: #31972 Test Plan: With this PR you can change the cancel text button of `ActionSheetIOS` by this `cancelButtonTintColor` prop | <img src="https://user-images.githubusercontent.com/36044436/128414537-c4454786-a5cf-49d2-8225-1ff26c9c5058.png" /> | <img src="https://user-images.githubusercontent.com/36044436/128414549-74a21509-711e-48e0-baf1-3718beae1598.png" /> | <img src="https://user-images.githubusercontent.com/36044436/128414559-4bee9d1a-ac9f-4cd2-b158-5c4c441158ec.png" /> | |-|-|-| Reviewed By: lunaleaps Differential Revision: D30878022 Pulled By: yungsters fbshipit-source-id: c70204f9f2510c75d8e9bed4e0fba79f1c941a1f
1 parent e2b5b65 commit 0185663

File tree

4 files changed

+71
-6
lines changed

4 files changed

+71
-6
lines changed

Libraries/ActionSheetIOS/ActionSheetIOS.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const ActionSheetIOS = {
4747
+cancelButtonIndex?: ?number,
4848
+anchor?: ?number,
4949
+tintColor?: ColorValue | ProcessedColorValue,
50+
+cancelButtonTintColor?: ColorValue | ProcessedColorValue,
5051
+userInterfaceStyle?: string,
5152
+disabledButtonIndices?: Array<number>,
5253
|},
@@ -59,7 +60,12 @@ const ActionSheetIOS = {
5960
invariant(typeof callback === 'function', 'Must provide a valid callback');
6061
invariant(RCTActionSheetManager, "ActionSheetManager doesn't exist");
6162

62-
const {tintColor, destructiveButtonIndex, ...remainingOptions} = options;
63+
const {
64+
tintColor,
65+
cancelButtonTintColor,
66+
destructiveButtonIndex,
67+
...remainingOptions
68+
} = options;
6369
let destructiveButtonIndices = null;
6470

6571
if (Array.isArray(destructiveButtonIndex)) {
@@ -69,14 +75,21 @@ const ActionSheetIOS = {
6975
}
7076

7177
const processedTintColor = processColor(tintColor);
78+
const processedCancelButtonTintColor = processColor(cancelButtonTintColor);
7279
invariant(
7380
processedTintColor == null || typeof processedTintColor === 'number',
7481
'Unexpected color given for ActionSheetIOS.showActionSheetWithOptions tintColor',
7582
);
83+
invariant(
84+
processedCancelButtonTintColor == null ||
85+
typeof processedCancelButtonTintColor === 'number',
86+
'Unexpected color given for ActionSheetIOS.showActionSheetWithOptions cancelButtonTintColor',
87+
);
7688
RCTActionSheetManager.showActionSheetWithOptions(
7789
{
7890
...remainingOptions,
7991
tintColor: processedTintColor,
92+
cancelButtonTintColor: processedCancelButtonTintColor,
8093
destructiveButtonIndices,
8194
},
8295
callback,

Libraries/ActionSheetIOS/NativeActionSheetManager.js

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface Spec extends TurboModule {
2222
+cancelButtonIndex?: ?number,
2323
+anchor?: ?number,
2424
+tintColor?: ?number,
25+
+cancelButtonTintColor?: ?number,
2526
+userInterfaceStyle?: ?string,
2627
+disabledButtonIndices?: Array<number>,
2728
|},
@@ -34,6 +35,7 @@ export interface Spec extends TurboModule {
3435
+subject?: ?string,
3536
+anchor?: ?number,
3637
+tintColor?: ?number,
38+
+cancelButtonTintColor?: ?number,
3739
+excludedActivityTypes?: ?Array<string>,
3840
+userInterfaceStyle?: ?string,
3941
|},

React/CoreModules/RCTActionSheetManager.mm

+14-5
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ - (void)presentViewController:(UIViewController *)alertController
9494
UIViewController *controller = RCTPresentedViewController();
9595
NSNumber *anchor = [RCTConvert NSNumber:options.anchor() ? @(*options.anchor()) : nil];
9696
UIColor *tintColor = [RCTConvert UIColor:options.tintColor() ? @(*options.tintColor()) : nil];
97+
UIColor *cancelButtonTintColor =
98+
[RCTConvert UIColor:options.cancelButtonTintColor() ? @(*options.cancelButtonTintColor()) : nil];
9799

98100
if (controller == nil) {
99101
RCTLogError(
@@ -105,6 +107,7 @@ - (void)presentViewController:(UIViewController *)alertController
105107
@"destructiveButtonIndices" : destructiveButtonIndices,
106108
@"anchor" : anchor,
107109
@"tintColor" : tintColor,
110+
@"cancelButtonTintColor" : cancelButtonTintColor,
108111
@"disabledButtonIndices" : disabledButtonIndices,
109112
});
110113
return;
@@ -122,20 +125,26 @@ - (void)presentViewController:(UIViewController *)alertController
122125
preferredStyle:UIAlertControllerStyleActionSheet];
123126

124127
NSInteger index = 0;
128+
bool isCancelButtonIndex = false;
125129
for (NSString *option in buttons) {
126130
UIAlertActionStyle style = UIAlertActionStyleDefault;
127131
if ([destructiveButtonIndices containsObject:@(index)]) {
128132
style = UIAlertActionStyleDestructive;
129133
} else if (index == cancelButtonIndex) {
130134
style = UIAlertActionStyleCancel;
135+
isCancelButtonIndex = true;
131136
}
132137

133138
NSInteger localIndex = index;
134-
[alertController addAction:[UIAlertAction actionWithTitle:option
135-
style:style
136-
handler:^(__unused UIAlertAction *action) {
137-
callback(@[ @(localIndex) ]);
138-
}]];
139+
UIAlertAction *actionButton = [UIAlertAction actionWithTitle:option
140+
style:style
141+
handler:^(__unused UIAlertAction *action) {
142+
callback(@[ @(localIndex) ]);
143+
}];
144+
if (isCancelButtonIndex) {
145+
[actionButton setValue:cancelButtonTintColor forKey:@"titleTextColor"];
146+
}
147+
[alertController addAction:actionButton];
139148

140149
index++;
141150
}

packages/rn-tester/js/examples/ActionSheetIOS/ActionSheetIOSExample.js

+41
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,41 @@ class ActionSheetTintExample extends React.Component<
9393
};
9494
}
9595

96+
class ActionSheetCancelButtonTintExample extends React.Component<
97+
$FlowFixMeProps,
98+
$FlowFixMeState,
99+
> {
100+
state = {
101+
clicked: 'none',
102+
};
103+
104+
render() {
105+
return (
106+
<View>
107+
<Text onPress={this.showActionSheet} style={style.button}>
108+
Click to show the ActionSheet
109+
</Text>
110+
<Text>Clicked button: {this.state.clicked}</Text>
111+
</View>
112+
);
113+
}
114+
115+
showActionSheet = () => {
116+
ActionSheetIOS.showActionSheetWithOptions(
117+
{
118+
options: BUTTONS,
119+
cancelButtonIndex: CANCEL_INDEX,
120+
destructiveButtonIndex: DESTRUCTIVE_INDEX,
121+
tintColor: 'green',
122+
cancelButtonTintColor: 'brown',
123+
},
124+
buttonIndex => {
125+
this.setState({clicked: BUTTONS[buttonIndex]});
126+
},
127+
);
128+
};
129+
}
130+
96131
class ActionSheetAnchorExample extends React.Component<
97132
$FlowFixMeProps,
98133
$FlowFixMeState,
@@ -342,6 +377,12 @@ exports.examples = [
342377
return <ActionSheetTintExample />;
343378
},
344379
},
380+
{
381+
title: 'Show Action Sheet with cancel tinted button',
382+
render(): React.Element<any> {
383+
return <ActionSheetCancelButtonTintExample />;
384+
},
385+
},
345386
{
346387
title: 'Show Action Sheet with anchor',
347388
render(): React.Element<any> {

0 commit comments

Comments
 (0)