import React, { FC, memo } from 'react'; import { View, ViewStyle, StyleSheet, Text, TextStyle } from 'react-native'; import TouchableOpacity from '../TouchableOpacity'; import { useThemeFactory } from '../Theme'; import Loading from '../Loading'; import createStyle from './style'; import type { ButtonProps } from './type'; const Button: FC = memo(props => { const { type = 'default', size = 'normal', loading, loadingText, loadingType, loadingSize, icon, iconPosition = 'left', color, plain, square, round, disabled, textStyle, children, ...rest } = props; const { styles } = useThemeFactory(createStyle, { type, size, plain }); const text = loading ? loadingText : children; const textFlattenStyle = StyleSheet.flatten([ styles.text, !!color && { color: plain ? color : 'white' }, textStyle, ]); const renderIcon = () => { const defaultIconSize = textFlattenStyle.fontSize; const iconColor = color ?? (textFlattenStyle.color as string); let marginStyles: ViewStyle; if (!text) { marginStyles = {}; } else if (iconPosition === 'left') { marginStyles = { marginRight: 4 }; } else { marginStyles = { marginLeft: 4 }; } return ( <> {icon && loading !== true && ( {React.isValidElement(icon) ? React.cloneElement(icon as React.ReactElement, { size: defaultIconSize, color: iconColor, }) : icon} )} {loading && ( )} ); }; const renderText = () => { if (!text) return null; return ( {text} ); }; return ( {iconPosition === 'left' && renderIcon()} {renderText()} {iconPosition === 'right' && renderIcon()} ); }); export default Button;