Skip to content

Commit 1fb3411

Browse files
author
BradByte
authored
fix: defer calling onSelect until after animation (#248)
- Fixes #203
1 parent 2f43b7e commit 1fb3411

File tree

3 files changed

+53
-15
lines changed

3 files changed

+53
-15
lines changed

example/App.tsx

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ import {
1212
ScrollView,
1313
SafeAreaView,
1414
TouchableOpacity,
15+
Share,
1516
} from 'react-native';
1617

1718
import ShowActionSheetButton from './ShowActionSheetButton';
1819

1920
type Props = ActionSheetProps;
2021

2122
interface State {
22-
selectedIndex: number | null;
23+
selectedIndex?: number | null;
2324
isModalOpen: boolean;
2425
}
2526

@@ -29,7 +30,7 @@ class App extends React.Component<Props, State> {
2930
isModalOpen: false,
3031
};
3132

32-
_updateSelectionText = (selectedIndex: number) => {
33+
_updateSelectionText = (selectedIndex?: number) => {
3334
this.setState({
3435
selectedIndex,
3536
});
@@ -38,7 +39,9 @@ class App extends React.Component<Props, State> {
3839
_renderSelectionText = () => {
3940
const { selectedIndex } = this.state;
4041
const text =
41-
selectedIndex === null ? 'No Option Selected' : `Option #${selectedIndex + 1} Selected`;
42+
selectedIndex === null || selectedIndex === undefined
43+
? 'No Option Selected'
44+
: `Option #${selectedIndex + 1} Selected`;
4245
return <Text style={styles.selectionText}>{text}</Text>;
4346
};
4447

@@ -50,6 +53,23 @@ class App extends React.Component<Props, State> {
5053
this.setState((prevState) => ({ isModalOpen: !prevState.isModalOpen }));
5154
};
5255

56+
async _onShare() {
57+
try {
58+
const result = await Share.share({
59+
message: 'React Native | A framework for building native apps using React',
60+
});
61+
if (result.action === Share.sharedAction) {
62+
if (result.activityType) {
63+
// shared with activity type of result.activityType
64+
} else {
65+
// shared
66+
}
67+
} else if (result.action === Share.dismissedAction) {
68+
// dismissed
69+
}
70+
} catch (error) {}
71+
}
72+
5373
_renderButtons() {
5474
const { showActionSheetWithOptions } = this.props;
5575
return (
@@ -86,7 +106,7 @@ class App extends React.Component<Props, State> {
86106
<ShowActionSheetButton
87107
title="Nested Action Sheets"
88108
onSelection={(index) => {
89-
if (index < 3) {
109+
if (!index || index < 3) {
90110
showActionSheetWithOptions(
91111
{
92112
title: 'Sub Action Sheet',
@@ -99,6 +119,21 @@ class App extends React.Component<Props, State> {
99119
}}
100120
showActionSheetWithOptions={showActionSheetWithOptions}
101121
/>
122+
<ShowActionSheetButton
123+
title="Share Menu"
124+
showActionSheetWithOptions={() =>
125+
showActionSheetWithOptions(
126+
{
127+
title: 'Share Menu',
128+
options: ['Share', 'Cancel'],
129+
cancelButtonIndex: 1,
130+
},
131+
(i) => {
132+
i === 0 && this._onShare();
133+
}
134+
)
135+
}
136+
/>
102137
{this._renderSectionHeader('Android-Only Options')}
103138
<ShowActionSheetButton
104139
title="Icons"

example/ShowActionSheetButton.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ interface Props {
1111
title: string;
1212
showActionSheetWithOptions: (
1313
options: ActionSheetOptions,
14-
callback: (buttonIndex: number) => void
14+
callback: (buttonIndex?: number) => void
1515
) => void;
16-
onSelection: (index: number) => void;
16+
onSelection: (index?: number) => void;
1717
withTitle?: boolean;
1818
withMessage?: boolean;
1919
withIcons?: boolean;
@@ -52,7 +52,7 @@ export default class ShowActionSheetButton extends React.PureComponent<Props> {
5252
} = this.props;
5353

5454
// Same interface as https://facebook.github.io/react-native/docs/actionsheetios.html
55-
const options = ['Delete', 'Disabled', 'Save', 'Share', 'Cancel'];
55+
const options = ['Delete', 'Disabled', 'Save', 'Cancel'];
5656
const icons = withIcons
5757
? [icon('delete'), icon('save'), icon('share'), icon('cancel')]
5858
: undefined;
@@ -62,7 +62,7 @@ export default class ShowActionSheetButton extends React.PureComponent<Props> {
6262
: undefined;
6363
const destructiveButtonIndex = 0;
6464
const disabledButtonIndices = [1];
65-
const cancelButtonIndex = 4;
65+
const cancelButtonIndex = 3;
6666
const textStyle: TextStyle | undefined = withCustomStyles
6767
? {
6868
fontSize: 20,
@@ -120,7 +120,7 @@ export default class ShowActionSheetButton extends React.PureComponent<Props> {
120120
// Android only,
121121
useModal,
122122
},
123-
(buttonIndex: number) => {
123+
(buttonIndex?: number) => {
124124
// Do something here depending on the button index selected
125125
onSelection(buttonIndex);
126126
}

src/ActionSheet/index.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default class ActionSheet extends React.Component<Props, State> {
4949
sheetOpacity: new Animated.Value(0),
5050
};
5151

52-
_deferNextShow?: () => void = undefined;
52+
_deferAfterAnimation?: () => void = undefined;
5353

5454
_setActionSheetHeight = ({ nativeEvent }: any) =>
5555
(this._actionSheetHeight = nativeEvent.layout.height);
@@ -171,7 +171,7 @@ export default class ActionSheet extends React.Component<Props, State> {
171171
const { isVisible, overlayOpacity, sheetOpacity } = this.state;
172172

173173
if (isVisible) {
174-
this._deferNextShow = this.showActionSheetWithOptions.bind(this, options, onSelect);
174+
this._deferAfterAnimation = this.showActionSheetWithOptions.bind(this, options, onSelect);
175175
return;
176176
}
177177

@@ -201,7 +201,7 @@ export default class ActionSheet extends React.Component<Props, State> {
201201
this.setState({
202202
isAnimating: false,
203203
});
204-
this._deferNextShow = undefined;
204+
this._deferAfterAnimation = undefined;
205205
}
206206
});
207207
// @ts-ignore: Argument of type '"actionSheetHardwareBackPress"' is not assignable to parameter of type '"hardwareBackPress"'
@@ -231,7 +231,10 @@ export default class ActionSheet extends React.Component<Props, State> {
231231
return false;
232232
}
233233

234-
onSelect && onSelect(index);
234+
if (onSelect) {
235+
this._deferAfterAnimation = onSelect.bind(this, index);
236+
}
237+
235238
return this._animateOut();
236239
};
237240

@@ -267,8 +270,8 @@ export default class ActionSheet extends React.Component<Props, State> {
267270
isAnimating: false,
268271
});
269272

270-
if (this._deferNextShow) {
271-
this._deferNextShow();
273+
if (this._deferAfterAnimation) {
274+
this._deferAfterAnimation();
272275
}
273276
}
274277
});

0 commit comments

Comments
 (0)