import React from 'react'; import { TextInput as RnTextInput, StyleSheet, StyleProp, ViewStyle, TextStyle, } from 'react-native'; import { Controller } from 'react-hook-form'; import { TextInputMask, TextInputMaskTypeProp } from 'react-native-masked-text'; import { View } from '../../View'; import { HelperText } from '../../Typography'; import type { Theme } from '../../../types'; import { withTheme } from '../../../core/theming'; import { INPUT_PADDING_HORIZONTAL } from './constants'; import { getOutlinedInputColors } from './helpers'; export type Props = React.ComponentProps & React.ComponentProps & { name?: string; label?: string; left?: React.ReactNode; right?: React.ReactNode; style?: StyleProp; theme: Theme; methods?: any; rules?: any; options?: any; type?: TextInputMaskTypeProp; error?: string; outlineColor?: string; showErrorLabel?: boolean; activeOutlineColor?: string; disabled?: boolean; errorStyle?: StyleProp; errorLabelStyle?: StyleProp; labelStyle?: StyleProp; containerStyle?: StyleProp; }; const TextInput = ({ name = '', label, methods = null, style, left, right, theme, rules, options, type = 'cpf', outlineColor: customOutlineColor, activeOutlineColor, disabled, error: errorMessage, showErrorLabel = true, errorStyle, errorLabelStyle, labelStyle, containerStyle, ...rest }: Props) => { const { roundness: borderRadius, baseHeight } = theme; const [focused, setFocused] = React.useState(false); const error: boolean = errorMessage != null || methods?.formState?.errors[name]?.message != null ? true : false; const hasActiveOutline = focused || error; const { inputTextColor, activeColor, outlineColor, placeholderColor } = getOutlinedInputColors({ activeOutlineColor, customOutlineColor, disabled, error, theme, }); const handleLeft = () => { if (!left) { return null; } return left; }; const handleRight = () => { if (!right) { return null; } return right; }; return ( {label && ( {label} )} {handleLeft()} {methods !== null ? ( type && options ? ( { onBlur(); setFocused(false); }} onFocus={() => { setFocused(true); }} onChangeText={onChange} value={value} {...rest} /> ) : ( { onBlur(); setFocused(false); }} onFocus={() => { setFocused(true); }} onChangeText={onChange} value={value} {...rest} /> ) } name={name as string} /> ) : type && options ? ( ) : ( )} {handleRight()} {error && showErrorLabel && ( {errorMessage != null ? errorMessage : methods?.formState?.errors[name]?.message} )} ); }; const styles = StyleSheet.create({ container: { borderWidth: 1, }, textInput: { flex: 1, padding: 0, paddingHorizontal: INPUT_PADDING_HORIZONTAL, }, }); export default withTheme(TextInput);