import React from "react"; import { AccessibilityRole, ImageBackground, ImageSourcePropType, StyleProp, TextStyle, ViewStyle, } from "react-native"; import theme from "mazlo-theme"; import { Text } from "../Text"; import BaseButton from "./BaseButton"; import styles from "./styles"; export type ButtonPropsType = { accessibilityLabel?: string; accessibilityRole?: AccessibilityRole; backgroundImage?: ImageSourcePropType; children?: React.ReactNode; disabled?: boolean; href?: string; onPress?: (e: any) => void; onPressIn?: (e: any) => void; style?: StyleProp; textStyle?: StyleProp; backgroundStyle?: StyleProp; size?: "sm" | "md"; variant?: keyof typeof theme.variants.buttons; title?: string; pill?: boolean; loading?: boolean; }; const Button = React.forwardRef( ( { accessibilityLabel, accessibilityRole = "button", backgroundImage, backgroundStyle, children, disabled, style, textStyle, pill, variant, loading, title, ...props }: ButtonPropsType, ref ) => { const isDisabled = disabled || loading; const backgroundView = backgroundImage ? ( ) : null; const childComponents = children ? ( React.Children.map(children, (child) => { if (child === null || typeof child !== "object") return child; // @ts-ignore const { type: { displayName } = {}, props: childProps } = child; if (displayName === "Text") { return React.cloneElement(child as any, { style: [ styles.text, variant && theme.variants.text[variant], textStyle, ], ...childProps, }); } return child; }) ) : title ? ( {title} ) : null; const buttonStyles = [ styles.container, variant && theme.variants.buttons[variant], pill && styles.pill, isDisabled && styles.disabled, style, ]; return ( {backgroundView} {loading ? ( Loading... ) : ( childComponents )} ); } ); export default Button;