import React, { useEffect, useRef, useState } from 'react'; import { Keyboard, Platform, TextInput as RNTextInput, View, useWindowDimensions } from 'react-native'; import { NOOP } from '@sendbird/uikit-utils'; import Modal from '../../components/Modal'; import Text from '../../components/Text'; import TextInput from '../../components/TextInput'; import createStyleSheet from '../../styles/createStyleSheet'; import useHeaderStyle from '../../styles/useHeaderStyle'; import useUIKitTheme from '../../theme/useUIKitTheme'; import Button from '../Button'; import DialogBox from '../Dialog/DialogBox'; export type PromptItem = { title: string; placeholder?: string; defaultValue?: string; onSubmit?: (text: string) => void; submitLabel?: string; onCancel?: () => void; cancelLabel?: string; }; type Props = { visible: boolean; onHide: () => void; onDismiss?: () => void; autoFocus?: boolean; } & PromptItem; const Prompt = ({ onDismiss, visible, onHide, autoFocus = true, title, defaultValue = '', placeholder = 'Enter', onSubmit = NOOP, onCancel = NOOP, submitLabel = 'Submit', cancelLabel = 'Cancel', }: Props) => { const { statusBarTranslucent } = useHeaderStyle(); const { colors } = useUIKitTheme(); const inputRef = useRef(null); const { width, height } = useWindowDimensions(); const buttons = [ { text: cancelLabel, onPress: onCancel }, { text: submitLabel, onPress: () => onSubmit(text) }, ]; const [text, setText] = useState(defaultValue); // FIXME: autoFocus trick with modal // Android // - InputProps.autoFocus is not trigger keyboard appearing. // - InputRef.focus() is trigger keyboard appearing, but position of keyboard selection is always the start of text. // iOS // - InputProps.autoFocus is trigger weird UI behavior on landscape mode. useEffect(() => { if (autoFocus && visible) { setTimeout(() => { if (Platform.OS === 'android') inputRef.current?.blur(); inputRef.current?.focus(); }, 250); } }, [autoFocus, visible, `${width}-${height}`]); return ( { setText(''); onDismiss?.(); }} statusBarTranslucent={statusBarTranslucent} visible={visible} backgroundStyle={{ alignItems: 'center', justifyContent: 'center' }} > {Boolean(title) && ( {title} )} {buttons.map(({ text, onPress }, index) => { return ( ); })} ); }; const styles = createStyleSheet({ container: { paddingTop: 8, }, titleContainer: { marginBottom: 12, paddingVertical: 12, paddingHorizontal: 24, alignItems: 'flex-start', }, inputContainer: { paddingHorizontal: 24, marginBottom: 12, }, buttonContainer: { flexDirection: 'row', alignItems: 'center', justifyContent: 'flex-end', padding: 12, }, button: { marginStart: 8, }, }); export default Prompt;