import React, { forwardRef, useState } from 'react'; import { TextInput } from 'react-native'; import styled from 'styled-components/native'; import { border, color, flexbox, layout } from 'styled-system'; import { customBorder, customBackground, customOutline, customLayout, customExtra, customShadow, } from '../../../utils/customProps'; import { Box, Text } from '..'; import type { IInputProps } from './IInputProps'; import { InputRightAddon, InputGroup, InputLeftAddon } from './InputGroup'; import { usePropsConfig } from '../../../theme'; import { isNil } from 'lodash'; const StyledInput = styled(TextInput)( color, layout, flexbox, border, customBorder, customBackground, customOutline, customShadow, customExtra, customLayout ); const Input = ( { style, isInvalid, isDisabled, placeholder, errorMessage, // isRequired, isReadOnly, isFullWidth, onFocus, onBlur, focusBorderColor, errorBorderColor, errorMessageColor, ariaLabel, InputLeftElement, InputRightElement, type, w, width, h, height, m, mr, ml, mt, mb, p, pr, pl, pt, pb, ...props }: IInputProps, ref: any ) => { const LayoutProps = { w, width, m, mr, ml, mt, mb, }; const [isFocused, setIsFocused] = useState(false); const handleFocus = (focusState: boolean, callback: any) => { setIsFocused(focusState); callback(); }; const focusProps = isFocused ? { borderWidth: 1, borderColor: focusBorderColor ? focusBorderColor : 'default.0', } : {}; const isInvalidProps = isInvalid ? { borderColor: errorBorderColor ? errorBorderColor : 'danger.2', } : {}; const newProps = usePropsConfig('Input', props); const computedProps = { display: 'flex', flexDirection: 'row', ...focusProps, ...isInvalidProps, p: !isNil(p) ? p : 3, pr, pl, pt, pb, h, height, }; return ( {InputLeftElement} { e.persist(); }} onFocus={() => { handleFocus(true, onFocus ? onFocus : () => {}); }} onBlur={() => { handleFocus(false, onBlur ? onBlur : () => {}); }} placeholder={placeholder} p={1} editable={isDisabled || isReadOnly ? false : true} borderRadius={undefined} //Remove variant props from StyledInput borderWidth={undefined} borderBottomWidth={undefined} ref={ref} /> {InputRightElement} {isInvalid && errorMessage ? ( {errorMessage} ) : null} ); }; //-------------------------------- InputGroup and other Child components ------------------------------- export { InputRightAddon, InputGroup, InputLeftAddon }; export { IInputProps } from './IInputProps'; export default forwardRef(Input);