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

[iOS] Add possibility to disable buttons in action sheet ios #28792

Closed
wants to merge 9 commits into from
5 changes: 5 additions & 0 deletions Libraries/ActionSheetIOS/RCTActionSheetManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ - (void)presentViewController:(UIViewController *)alertController
NSArray<NSString *> *buttons = [RCTConvert NSStringArray:options[@"options"]];
NSInteger cancelButtonIndex = options[@"cancelButtonIndex"] ? [RCTConvert NSInteger:options[@"cancelButtonIndex"]] : -1;
NSArray<NSNumber *> *destructiveButtonIndices;
NSArray<NSNumber *> *disabledButtonIndices = [RCTConvert NSArray:options[@"disabledButtonIndices"]];
if ([options[@"destructiveButtonIndex"] isKindOfClass:[NSArray class]]) {
destructiveButtonIndices = [RCTConvert NSArray:options[@"destructiveButtonIndex"]];
} else {
Expand Down Expand Up @@ -108,6 +109,10 @@ - (void)presentViewController:(UIViewController *)alertController
callback(@[@(localIndex)]);
}]];

if ([disabledButtonIndices containsObject:@(localIndex)]) {
[alertController.actions[localIndex] setEnabled:false];
}

index++;
}

Expand Down
38 changes: 38 additions & 0 deletions RNTester/js/ActionSheetIOSExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const ScreenshotManager = NativeModules.ScreenshotManager;
const BUTTONS = ['Option 0', 'Option 1', 'Option 2', 'Delete', 'Cancel'];
const DESTRUCTIVE_INDEX = 3;
const CANCEL_INDEX = 4;
const DISABLED_BUTTON_INDICES = [1, 2];

type Props = $ReadOnly<{||}>;
type State = {|clicked: string|};
Expand Down Expand Up @@ -138,6 +139,37 @@ class ActionSheetAnchorExample extends React.Component<
};
}

class ActionSheetDisabledExample extends React.Component<Props, State> {
state = {
clicked: 'none',
};

render() {
return (
<View>
<Text onPress={this.showActionSheet} style={style.button}>
Click to show the ActionSheet
</Text>
<Text>Clicked button: {this.state.clicked}</Text>
</View>
);
}

showActionSheet = () => {
ActionSheetIOS.showActionSheetWithOptions(
lukewalczak marked this conversation as resolved.
Show resolved Hide resolved
{
options: BUTTONS,
cancelButtonIndex: CANCEL_INDEX,
destructiveButtonIndex: DESTRUCTIVE_INDEX,
disabledButtonIndices: DISABLED_BUTTON_INDICES,
},
buttonIndex => {
this.setState({clicked: BUTTONS[buttonIndex]});
},
);
};
}

class ShareActionSheetExample extends React.Component<
$FlowFixMeProps,
$FlowFixMeState,
Expand Down Expand Up @@ -315,6 +347,12 @@ exports.examples = [
return <ActionSheetAnchorExample />;
},
},
{
title: 'Show Action Sheet with disabled buttons',
render(): React.Element<any> {
return <ActionSheetDisabledExample />;
},
},
{
title: 'Show Share Action Sheet',
render(): React.Element<any> {
Expand Down