import { Text, ActivityIndicator, TouchableOpacity, StyleSheet, View, Keyboard, StyleProp, ViewStyle, TextStyle, } from 'react-native'; import React, { ComponentType } from 'react'; import { ThemeType } from '../themes/hero'; import { useTheme } from './ThemeContext'; import hasHomeBar from '../helpers/hasHomeBar'; const Wrapper = ({ enabled, onPress, style, children, }: { enabled: boolean; onPress?: () => void; style: StyleProp; children: React.ReactElement; }) => { if (!enabled) { return {children}; } else { return ( {children} ); } }; export type Props = { readonly buttonStyle?: StyleProp; readonly disabled?: boolean; readonly forceInset?: string; readonly loading?: boolean; readonly onPress?: () => void; readonly text: string; readonly textStyle?: StyleProp; readonly wrapperStyle?: StyleProp; }; const getThemeWrapperStyle = ( forceInset: string | undefined, showKeyboard: boolean, theme: ThemeType, ): ThemeType['bottomButton']['wrapperWithHomeBar'] => { if (forceInset) { switch (forceInset) { case 'always': return theme.bottomButton.wrapperWithHomeBar; case 'never': return theme.bottomButton.wrapperWithoutHomeBar; default: { return theme.bottomButton.wrapperWithoutHomeBar; } } } return !showKeyboard && hasHomeBar() ? theme.bottomButton.wrapperWithHomeBar : theme.bottomButton.wrapperWithoutHomeBar; }; const BottomButton: ComponentType = ({ text, onPress, disabled = false, loading = false, forceInset, wrapperStyle = {}, buttonStyle = {}, textStyle = {}, }: Props) => { const theme = useTheme(); const [showKeyboard, setShowKeyboard] = React.useState(() => false); React.useEffect(() => { const listener = Keyboard.addListener('keyboardWillShow', () => setShowKeyboard(() => true)); return () => { listener.remove(); }; }, []); React.useEffect(() => { const listener = Keyboard.addListener('keyboardWillHide', () => setShowKeyboard(() => false)); return () => { listener.remove(); }; }, []); const themeWrapperStyle = getThemeWrapperStyle(forceInset, showKeyboard, theme); return ( {loading && !disabled ? ( ) : ( {text} )} ); }; export default BottomButton;