import React from 'react'; import type { ReactElement, ReactText } from 'react'; import { TouchableOpacity, View, type TouchableOpacityProps, } from 'react-native'; import { Text, TextCategoryEnum } from './text'; import { classNames } from '../utils'; export enum AppearanceEnum { FILLED = 'FILLED', OUTLINE = 'OUTLINE', GHOST = 'GHOST', } export enum SizeEnum { SMALL = 'SMALL', MEDIUM = 'MEDIUM', LARGE = 'LARGE', } export interface ButtonProps extends TouchableOpacityProps { appearance?: AppearanceEnum; accessoryLeft?: ReactElement | (() => ReactElement); accessoryRight?: ReactElement | (() => ReactElement); size?: SizeEnum; text: string | ReactText | (() => ReactText); className?: string; } const RenderLeftAcc = ({ component, }: { component: ButtonProps['accessoryRight']; }) => { return ( {/* @ts-ignore */} {typeof component === 'function' ? component() : component} ); }; const RenderRightAcc = ({ component, }: { component: ButtonProps['accessoryRight']; }) => { return ( {/* @ts-ignore */} {typeof component === 'function' ? component() : component} ); }; // @ts-ignore const RenderText: React.FC<{ component: ButtonProps['text']; size: SizeEnum; appearance: AppearanceEnum; }> = ({ component, size, appearance }) => { switch (typeof component) { case 'function': return component(); case 'object': return component; default: // string let category = TextCategoryEnum.MEDIUM_PARAGRAPH; if (size === SizeEnum.LARGE) { category = TextCategoryEnum.LARGE_PARAGRAPH; } if (size === SizeEnum.SMALL) { category = TextCategoryEnum.SMALL_PARAGRAPH; } return ( {component} ); } }; export const Button: React.FC = (props) => { const { text, appearance = AppearanceEnum.FILLED, accessoryLeft, accessoryRight, size = SizeEnum.MEDIUM, className, ...restOfProps } = props; return ( {accessoryLeft && } {text && ( )} {accessoryRight && } ); };