Created
April 27, 2024 10:51
-
-
Save hd1801/271bcec4b11036ae6ad5194482aa1767 to your computer and use it in GitHub Desktop.
Clean way to implement a common components that provides you a bunch of options to choose from, like menu , action-sheets, etc
This file contains hidden or 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
// ActionSheetRenderer.tsx | |
import _ from 'lodash'; | |
import React, { FC, ReactElement } from 'react'; | |
import { Icon } from 'react-native-paper'; | |
import { useDisclose } from '../../hooks'; | |
import { CustomBottomSheet } from '../../pages'; | |
import { ActionSheetItem } from '../events'; | |
import { VStack } from '../layout'; | |
export interface ActionSheetRendererProps { | |
options: Array<{ | |
icon: string; | |
text: string; | |
onPress: () => void; | |
}>; | |
trigger: ReactElement; | |
shouldCloseOnPress?: boolean; | |
} | |
export const ActionSheetRenderer: FC<ActionSheetRendererProps> = ({ | |
options, | |
trigger, | |
shouldCloseOnPress = true, | |
}) => { | |
const { isOpen, onClose, onOpen } = useDisclose(); | |
return ( | |
<> | |
{React.cloneElement(trigger, { onPress: onOpen })} | |
<CustomBottomSheet isOpen={isOpen} onClose={onClose}> | |
<VStack pointerEvents='box-none'> | |
{_.map(options, option => { | |
return ( | |
<ActionSheetItem | |
title={option.text} | |
Icon={<Icon size={25} source={option.icon} />} | |
onPress={() => { | |
option.onPress(); | |
shouldCloseOnPress && onClose(); | |
}} | |
/> | |
); | |
})} | |
</VStack> | |
</CustomBottomSheet> | |
</> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment