Skip to content

Instantly share code, notes, and snippets.

@hd1801
Created April 27, 2024 10:51
Show Gist options
  • Save hd1801/271bcec4b11036ae6ad5194482aa1767 to your computer and use it in GitHub Desktop.
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
// 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