-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5681ee6
commit 04cffb8
Showing
5 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as useActionSheet } from './useActionSheet'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { useCallback } from 'react'; | ||
import { ActionSheetIOS } from 'react-native'; | ||
|
||
interface Option { | ||
disabled?: boolean; | ||
destructive?: boolean; | ||
onPress: () => void; | ||
title: string; | ||
} | ||
|
||
interface CancelOption extends Omit<Option, 'destructive'> {} | ||
|
||
interface Options { | ||
cancel?: CancelOption; | ||
options: Option[]; | ||
} | ||
|
||
function useActionSheet() { | ||
const showActionSheetWithOptions = useCallback(({ cancel, options }: Options) => { | ||
const actionSheetOptions = cancel ? [cancel, ...options] : options; | ||
const actionSheetTitles = actionSheetOptions.map(({ title }) => title); | ||
|
||
ActionSheetIOS.showActionSheetWithOptions( | ||
{ | ||
options: actionSheetTitles, | ||
destructiveButtonIndex: [ | ||
...options | ||
.filter(({ destructive }) => destructive) | ||
.map((_, index) => (cancel ? index + 1 : index)), | ||
], | ||
disabledButtonIndices: [ | ||
...actionSheetOptions.filter(({ disabled }) => disabled).map((_, index) => index), | ||
], | ||
cancelButtonIndex: cancel ? 0 : undefined, | ||
userInterfaceStyle: 'dark', | ||
}, | ||
(selectedIndex: number) => { | ||
if (cancel && selectedIndex === 0) { | ||
return cancel.onPress(); | ||
} | ||
|
||
if (cancel) { | ||
return options[selectedIndex - 1].onPress(); | ||
} | ||
|
||
return options[selectedIndex].onPress(); | ||
} | ||
); | ||
}, []); | ||
|
||
return { | ||
showActionSheetWithOptions, | ||
}; | ||
} | ||
|
||
export default useActionSheet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
// components | ||
export { Button } from 'components/Button'; | ||
export { Switch } from 'components/Switch'; | ||
export { TabBar } from 'components/TabBar'; | ||
export { Table } from 'components/Table'; | ||
export { TextInput } from 'components/TextInput'; | ||
export { TopNavigation } from 'components/TopNavigation'; | ||
|
||
// hooks | ||
export { useActionSheet } from 'hooks/useActionSheet'; |