import * as React from 'react'; import type { TextInputProps } from 'react-native'; import { TextInput as RNTextInput } from 'react-native'; import ViewWrapper from './ViewWrapper'; import { useFormSmartScroll } from './Provider'; import type { ViewStyle } from 'react-native'; import { mergeRefs } from './mergeRefs'; type Props = { containerStyle?: ViewStyle; textInputProps?: TextInputProps; name?: string; renderTop?: () => React.ReactElement; renderBottom?: () => React.ReactElement; renderInput?: ( args: TextInputProps & { ref: React.LegacyRef | undefined; } ) => React.ReactElement; chainTo?: string; ref?: React.RefObject; }; export const TextInput = React.forwardRef( (props, inputRef) => { const id = React.useId(); const name = React.useRef(props.name ?? id); const textInputRef = React.useRef(null); const ref = mergeRefs([textInputRef, inputRef]); const { onBlur, onFocus, onSubmitEditing, ...textInputProps } = props.textInputProps ?? {}; const { registerInput, baseTextInputProps, chainInput } = useFormSmartScroll(); React.useEffect(() => { registerInput(name.current, textInputRef); }, []); return ( {typeof props.renderInput === 'function' ? ( props.renderInput({ ...(textInputProps ?? {}), onSubmitEditing: (e) => { if (props.chainTo) { chainInput(props.chainTo); } onSubmitEditing?.(e); }, ...baseTextInputProps(name.current, { onFocus, onBlur, }), ref, }) ) : ( <> {props.renderTop?.()} { if (props.chainTo) { chainInput(props.chainTo); } onSubmitEditing?.(e); }} /> {props.renderBottom?.()} )} ); } );