Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add disabledButtonTintColor prop in ActionSheetIOS #46883

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface ActionSheetIOSOptions {
anchor?: number | undefined;
tintColor?: ColorValue | ProcessedColorValue | undefined;
cancelButtonTintColor?: ColorValue | ProcessedColorValue | undefined;
disabledButtonTintColor?: ColorValue | ProcessedColorValue | undefined;
userInterfaceStyle?: 'light' | 'dark' | undefined;
disabledButtonIndices?: number[] | undefined;
}
Expand Down
13 changes: 13 additions & 0 deletions packages/react-native/Libraries/ActionSheetIOS/ActionSheetIOS.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const ActionSheetIOS = {
+anchor?: ?number,
+tintColor?: ColorValue | ProcessedColorValue,
+cancelButtonTintColor?: ColorValue | ProcessedColorValue,
+disabledButtonTintColor?: ColorValue | ProcessedColorValue,
+userInterfaceStyle?: string,
+disabledButtonIndices?: Array<number>,
|},
Expand All @@ -64,6 +65,7 @@ const ActionSheetIOS = {
const {
tintColor,
cancelButtonTintColor,
disabledButtonTintColor,
destructiveButtonIndex,
...remainingOptions
} = options;
Expand All @@ -77,6 +79,10 @@ const ActionSheetIOS = {

const processedTintColor = processColor(tintColor);
const processedCancelButtonTintColor = processColor(cancelButtonTintColor);
const processedDisabledButtonTintColor = processColor(
disabledButtonTintColor,
);

invariant(
processedTintColor == null || typeof processedTintColor === 'number',
'Unexpected color given for ActionSheetIOS.showActionSheetWithOptions tintColor',
Expand All @@ -86,13 +92,20 @@ const ActionSheetIOS = {
typeof processedCancelButtonTintColor === 'number',
'Unexpected color given for ActionSheetIOS.showActionSheetWithOptions cancelButtonTintColor',
);
invariant(
processedDisabledButtonTintColor == null ||
typeof processedDisabledButtonTintColor === 'number',
'Unexpected color given for ActionSheetIOS.showActionSheetWithOptions disabledButtonTintColor',
);
RCTActionSheetManager.showActionSheetWithOptions(
{
...remainingOptions,
// $FlowFixMe[incompatible-call]
tintColor: processedTintColor,
// $FlowFixMe[incompatible-call]
cancelButtonTintColor: processedCancelButtonTintColor,
// $FlowFixMe[incompatible-call]
disabledButtonTintColor: processedDisabledButtonTintColor,
destructiveButtonIndices,
},
callback,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ exports[`public API should not change unintentionally Libraries/ActionSheetIOS/A
+anchor?: ?number,
+tintColor?: ColorValue | ProcessedColorValue,
+cancelButtonTintColor?: ColorValue | ProcessedColorValue,
+disabledButtonTintColor?: ColorValue | ProcessedColorValue,
+userInterfaceStyle?: string,
+disabledButtonIndices?: Array<number>,
|},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ - (void)presentViewController:(UIViewController *)alertController
UIColor *tintColor = [RCTConvert UIColor:options.tintColor() ? @(*options.tintColor()) : nil];
UIColor *cancelButtonTintColor =
[RCTConvert UIColor:options.cancelButtonTintColor() ? @(*options.cancelButtonTintColor()) : nil];
UIColor *disabledButtonTintColor =
[RCTConvert UIColor:options.disabledButtonTintColor() ? @(*options.disabledButtonTintColor()) : nil];
NSString *userInterfaceStyle = [RCTConvert NSString:options.userInterfaceStyle()];

dispatch_async(dispatch_get_main_queue(), ^{
Expand All @@ -114,6 +116,7 @@ - (void)presentViewController:(UIViewController *)alertController
@"anchor" : anchor ?: @"(null)",
@"tintColor" : tintColor ?: @"(null)",
@"cancelButtonTintColor" : cancelButtonTintColor ?: @"(null)",
@"disabledButtonTintColor" : disabledButtonTintColor ?: @"(null)",
@"disabledButtonIndices" : disabledButtonIndices ?: @"(null)",
});
return;
Expand Down Expand Up @@ -166,7 +169,11 @@ - (void)presentViewController:(UIViewController *)alertController
if (disabledButtonIndices) {
for (NSNumber *disabledButtonIndex in disabledButtonIndices) {
if ([disabledButtonIndex integerValue] < buttons.count) {
[alertController.actions[[disabledButtonIndex integerValue]] setEnabled:false];
UIAlertAction *action = alertController.actions[[disabledButtonIndex integerValue]];
[action setEnabled:false];
if (disabledButtonTintColor) {
[action setValue:disabledButtonTintColor forKey:@"titleTextColor"];
}
} else {
RCTLogError(
@"Index %@ from `disabledButtonIndices` is out of bounds. Maximum index value is %@.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface Spec extends TurboModule {
+anchor?: ?number,
+tintColor?: ?number,
+cancelButtonTintColor?: ?number,
+disabledButtonTintColor?: ?number,
+userInterfaceStyle?: ?string,
+disabledButtonIndices?: Array<number>,
|},
Expand All @@ -37,6 +38,7 @@ export interface Spec extends TurboModule {
+anchor?: ?number,
+tintColor?: ?number,
+cancelButtonTintColor?: ?number,
+disabledButtonTintColor?: ?number,
+excludedActivityTypes?: ?Array<string>,
+userInterfaceStyle?: ?string,
|},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,50 @@ class ActionSheetCancelButtonTintExample extends React.Component<
};
}

class ActionSheetDisabledButtonTintExample extends React.Component<
$FlowFixMeProps,
$FlowFixMeState,
> {
state: any | {clicked: string} = {
clicked: 'none',
};

render(): React.Node {
return (
<RNTesterThemeContext.Consumer>
{theme => (
<View>
<Text
onPress={this.showActionSheet}
style={[style.button, {color: theme.SecondaryLabelColor}]}>
Click to show the ActionSheet
</Text>
<Text style={{color: theme.SecondaryLabelColor}}>
Clicked button: {this.state.clicked}
</Text>
</View>
)}
</RNTesterThemeContext.Consumer>
);
}

showActionSheet = () => {
ActionSheetIOS.showActionSheetWithOptions(
{
options: BUTTONS,
cancelButtonIndex: CANCEL_INDEX,
destructiveButtonIndex: DESTRUCTIVE_INDEX,
disabledButtonIndices: DISABLED_BUTTON_INDICES,
tintColor: 'black',
disabledButtonTintColor: 'gray',
},
buttonIndex => {
this.setState({clicked: BUTTONS[buttonIndex]});
},
);
};
}

class ActionSheetAnchorExample extends React.Component<
$FlowFixMeProps,
$FlowFixMeState,
Expand Down Expand Up @@ -480,6 +524,12 @@ exports.examples = [
return <ActionSheetCancelButtonTintExample />;
},
},
{
title: 'Show Action Sheet with disabled tinted button',
render(): React.MixedElement {
return <ActionSheetDisabledButtonTintExample />;
},
},
{
title: 'Show Action Sheet with anchor',
render(): React.MixedElement {
Expand Down