import type {MutableRefObject, ReactNode, RefObject} from 'react'; import React, { forwardRef, isValidElement, useCallback, useMemo, useRef, useState, } from 'react'; import type { StyleProp, TextInputProps, TextStyle, ViewStyle, } from 'react-native'; import { Platform, Text, TextInput, TouchableWithoutFeedback, View, } from 'react-native'; import {useHover} from 'react-native-web-hooks'; import {useTheme} from '@dooboo-ui/theme'; import {css} from '@emotion/native'; import {cloneElemWithDefaultColors} from '../../utils/guards'; import {Icon} from '../Icon'; import GlobalStyles from './style'; export type EditTextStyles = { container?: StyleProp; labelContainer?: StyleProp; label?: StyleProp; inputContainer?: StyleProp; input?: StyleProp; error?: StyleProp; counter?: StyleProp; }; export type EditTextStatus = | 'disabled' | 'error' | 'focused' | 'hovered' | 'basic'; type RenderType = (stats: EditTextStatus) => JSX.Element; type CustomRenderType = | (({color, status}: {color: string; status: EditTextStatus}) => JSX.Element) | null; export type EditTextProps = { testID?: TextInputProps['testID']; inputRef?: MutableRefObject | RefObject; style?: StyleProp; styles?: EditTextStyles; // Component startElement?: JSX.Element | CustomRenderType; endElement?: JSX.Element | CustomRenderType; required?: boolean; label?: string | RenderType; error?: string | RenderType; direction?: 'row' | 'column'; decoration?: 'underline' | 'boxed'; value?: TextInputProps['value']; multiline?: TextInputProps['multiline']; onChange?: TextInputProps['onChange']; onChangeText?: TextInputProps['onChangeText']; placeholder?: TextInputProps['placeholder']; placeholderColor?: TextInputProps['placeholderTextColor']; onFocus?: TextInputProps['onFocus'] | undefined; onBlur?: TextInputProps['onBlur'] | undefined; editable?: TextInputProps['editable']; autoComplete?: TextInputProps['autoComplete']; autoCapitalize?: TextInputProps['autoCapitalize']; secureTextEntry?: TextInputProps['secureTextEntry']; onSubmitEditing?: TextInputProps['onSubmitEditing']; numberOfLines?: TextInputProps['numberOfLines']; maxLength?: TextInputProps['maxLength']; hideCounter?: boolean; textInputProps?: Omit< TextInputProps, | 'value' | 'onChange' | 'numberOfLines' | 'multiline' | 'onChange' | 'onChangeText' | 'placeholder' | 'placeholderTextColor' | 'onFocus' | 'onBlur' | 'editable' | 'autoComplete' | 'autoCapitalize' | 'secureTextEntry' | 'onSubmitEditing' | 'maxLength' >; colors?: { basic?: string; disabled?: string; error?: string; focused?: string; hovered?: string; placeholder?: string; }; }; export const EditText = forwardRef( ( { testID, textInputProps, style, styles, label, error, startElement, endElement, multiline = false, value = '', placeholder, placeholderColor, onChange, onChangeText, onFocus, onBlur, onSubmitEditing, numberOfLines, maxLength, hideCounter = false, autoComplete, autoCapitalize = 'none', secureTextEntry = false, editable = true, direction = 'column', decoration = 'underline', colors = {}, required = false, }: EditTextProps, ref, ): JSX.Element => { EditText.displayName = 'EditText'; const {theme} = useTheme(); const webRef = useRef(null); const [focused, setFocused] = useState(false); const defaultInputRef = useRef(null); const inputRef = (ref as MutableRefObject) || defaultInputRef; const hovered = useHover(webRef); const defaultContainerStyle = css` flex-direction: ${direction}; `; const defaultColor = !editable ? colors.disabled || theme.text.disabled : error ? colors.error || theme.text.validation : focused ? colors.focused || theme.text.basic : hovered ? colors.hovered || theme.text.basic : colors.placeholder || theme.text.placeholder; // Default label placeholder color has different value compared to default input placeholder color const labelPlaceholderColor = useMemo( () => defaultColor === (colors.placeholder || theme.text.placeholder) && { color: colors.placeholder || theme.text.disabled, }, [ colors.placeholder, defaultColor, theme.text.disabled, theme.text.placeholder, ], ); const status: EditTextStatus = !editable ? 'disabled' : error ? 'error' : hovered ? 'hovered' : focused ? 'focused' : 'basic'; const renderLabel = useCallback((): JSX.Element | null => { // eslint-disable-next-line react/no-unstable-nested-components function Wrapper({children}: {children: ReactNode}): JSX.Element { return ( {children} {required ? ( ) : null} ); } return typeof label === 'string' ? ( {label} ) : label ? ( {label(status)} ) : null; }, [ decoration, defaultColor, focused, label, labelPlaceholderColor, required, status, styles?.label, styles?.labelContainer, theme.role.danger, ]); const renderContainer = useCallback( (children: ReactNode): JSX.Element => { return ( inputRef.current?.focus()} testID="container-touch" > {children} ); }, [ decoration, defaultColor, defaultContainerStyle, direction, inputRef, labelPlaceholderColor, styles?.container, ], ); const renderInput = useCallback((): JSX.Element | null => { return ( <> {isValidElement(startElement) ? cloneElemWithDefaultColors({ element: startElement, color: defaultColor, style: css` margin-left: -4px; margin-right: 4px; `, }) : startElement} { setFocused(false); onBlur?.(e); }} onChange={onChange} onChangeText={onChangeText} onFocus={(e) => { setFocused(true); onFocus?.(e); }} onSubmitEditing={onSubmitEditing} placeholder={placeholder} placeholderTextColor={placeholderColor || theme.text.placeholder} ref={inputRef} secureTextEntry={secureTextEntry} selectionColor={theme.role.underlay} style={[ // Stretch input in order to make remaining space clickable css` font-family: Pretendard; flex: 1; font-size: 16px; text-align-vertical: ${multiline ? 'top' : 'center'}; `, Platform.OS === 'web' && css` outline-width: 0; `, direction === 'column' ? css` padding-top: 12px; ` : css` padding-left: 12px; `, css` color: ${defaultColor}; padding: 10px 0 12px 0; `, styles?.input, ]} testID={testID} value={value} {...textInputProps} /> {isValidElement(endElement) ? cloneElemWithDefaultColors({ element: endElement, color: defaultColor, style: css` margin-left: 4px; margin-right: ${decoration === 'boxed' ? '-8px' : '-4px'}; `, }) : endElement} ); }, [ autoCapitalize, autoComplete, decoration, defaultColor, direction, editable, endElement, inputRef, maxLength, multiline, numberOfLines, onBlur, onChange, onChangeText, onFocus, onSubmitEditing, placeholder, placeholderColor, secureTextEntry, startElement, styles?.input, styles?.inputContainer, testID, textInputProps, theme.role.underlay, theme.text.placeholder, value, ]); const renderError = useCallback((): JSX.Element | null => { return error ? ( typeof error === 'string' ? ( {error} ) : ( error?.(status) ) ) : null; }, [error, status, styles?.error, theme.text.validation]); const renderCounter = useCallback((): JSX.Element | null => { if (hideCounter) { return null; } return maxLength ? ( {`${value.length}/${maxLength}`} ) : null; }, [ hideCounter, maxLength, styles?.counter, theme.text.placeholder, value.length, ]); return ( <> {Platform.OS === 'web' ? : null} {renderLabel()} {renderContainer(renderInput())} {renderError() || renderCounter() ? ( {renderError()} {renderCounter()} ) : null} ); }, );