Skip to content

Commit

Permalink
minor: add useActionSheet hook
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewwolfe committed Oct 15, 2022
1 parent 5681ee6 commit 04cffb8
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 2 deletions.
18 changes: 17 additions & 1 deletion App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import { Table } from 'components/Table';
import { TextInput } from 'components/TextInput';
import { TopNavigation } from 'components/TopNavigation';

import { useActionSheet } from 'hooks/useActionSheet';

function App() {
const { showActionSheetWithOptions } = useActionSheet();

const [activeTab, setActiveTab] = useState<number>(0);

return (
<SafeAreaView style={{ backgroundColor: '#F2F1F6', flex: 1 }}>
<ScrollView style={{ marginHorizontal: 16 }}>
<ScrollView style={{ paddingHorizontal: 16 }}>
<TopNavigation
left={{ icon: <Ionicons name="chevron-back" size={32} />, title: 'Back' }}
right={{ title: 'Save' }}
Expand Down Expand Up @@ -57,6 +61,18 @@ function App() {
rows={[{ rightIcon: true, title: 'Z17AHW' }, { title: 'HP24' }, { title: 'My Wifi' }]}
/>
</Flex>

<Flex>
<Button
title="Action Sheet"
onPress={() =>
showActionSheetWithOptions({
cancel: { onPress: () => console.log('cancel'), title: 'Cancel' },
options: [{ onPress: () => console.log('save'), title: 'Save' }],
})
}
/>
</Flex>
</ScrollView>
</SafeAreaView>
);
Expand Down
3 changes: 2 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ module.exports = function (api) {
],
alias: {
components: './src/components',
theme: './src/theme'
hooks: './src/hooks',
theme: './src/theme',
},
},
],
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useActionSheet/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as useActionSheet } from './useActionSheet';
56 changes: 56 additions & 0 deletions src/hooks/useActionSheet/useActionSheet.ts
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;
4 changes: 4 additions & 0 deletions src/index.ts
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';

0 comments on commit 04cffb8

Please sign in to comment.