Skip to content

Commit 81112b9

Browse files
authored
feat: Allow multiple destructive indices (#210)
1 parent f0b38da commit 81112b9

3 files changed

Lines changed: 22 additions & 10 deletions

File tree

src/ActionSheet/ActionGroup.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import * as React from 'react';
22
import {
3-
StyleSheet,
4-
Text,
5-
Image,
6-
View,
7-
ScrollView,
8-
findNodeHandle,
93
AccessibilityInfo,
4+
findNodeHandle,
5+
Image,
106
Platform,
7+
ScrollView,
8+
StyleSheet,
9+
Text,
1110
UIManager,
11+
View,
1212
} from 'react-native';
1313
import TouchableNativeFeedbackSafe from './TouchableNativeFeedbackSafe';
1414
import { ActionSheetOptions } from '../types';
@@ -46,6 +46,14 @@ const focusViewOnRender = (ref: React.Component | null) => {
4646
}
4747
};
4848

49+
const isIndexDestructive = (index: number, destructiveIndex?: number | number[]) => {
50+
if (Array.isArray(destructiveIndex)) {
51+
return destructiveIndex.includes(index);
52+
}
53+
54+
return index === destructiveIndex;
55+
};
56+
4957
export default class ActionGroup extends React.Component<Props> {
5058
static defaultProps = {
5159
title: null,
@@ -126,7 +134,7 @@ export default class ActionGroup extends React.Component<Props> {
126134
const defaultColor = tintColor
127135
? tintColor
128136
: (textStyle || {}).color || BLACK_87PC_TRANSPARENT;
129-
const color = i === destructiveButtonIndex ? destructiveColor : defaultColor;
137+
const color = isIndexDestructive(i, destructiveButtonIndex) ? destructiveColor : defaultColor;
130138
const iconSource = icons != null ? icons[i] : null;
131139

132140
optionViews.push(
@@ -169,7 +177,7 @@ const styles = StyleSheet.create({
169177
width: 24,
170178
height: 24,
171179
marginRight: 32,
172-
justifyContent: "center",
180+
justifyContent: 'center',
173181
},
174182
message: {
175183
marginTop: 12,

src/ActionSheet/index.ios.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export default class ActionSheet extends React.Component<Props> {
3434
anchor: dataOptions.anchor || undefined,
3535
userInterfaceStyle: dataOptions.userInterfaceStyle || undefined,
3636
};
37+
// @ts-ignore: Even though ActionSheetIOS supports array of numbers for `destructiveIndex` the types are not yet updated. See https://github.com/facebook/react-native/pull/18254.
3738
ActionSheetIOS.showActionSheetWithOptions(iosOptions, onSelect);
3839
}
3940
}

src/types.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import * as React from 'react';
22
import { TextStyle, ViewStyle } from 'react-native';
33

44
export interface ActionSheetProps {
5-
showActionSheetWithOptions: (options: ActionSheetOptions, callback: (i: number) => void | Promise<void>) => void;
5+
showActionSheetWithOptions: (
6+
options: ActionSheetOptions,
7+
callback: (i: number) => void | Promise<void>
8+
) => void;
69
}
710

811
// for iOS
@@ -12,7 +15,7 @@ export interface ActionSheetIOSOptions {
1215
message?: string;
1316
tintColor?: string;
1417
cancelButtonIndex?: number;
15-
destructiveButtonIndex?: number;
18+
destructiveButtonIndex?: number | number[];
1619
anchor?: number;
1720
userInterfaceStyle?: 'light' | 'dark';
1821
}

0 commit comments

Comments
 (0)