|
| 1 | +import React, {forwardRef, useRef, type ReactNode} from 'react'; |
| 2 | +import Animated, {useAnimatedStyle, interpolateColor, interpolate} from 'react-native-reanimated'; |
| 3 | +import { |
| 4 | + Text, |
| 5 | + View, |
| 6 | + TextInput, |
| 7 | + TouchableWithoutFeedback, |
| 8 | + type StyleProp, |
| 9 | + type ViewStyle, |
| 10 | + type TextStyle, |
| 11 | + type TextInputProps, |
| 12 | + type LayoutChangeEvent, |
| 13 | +} from 'react-native'; |
| 14 | + |
| 15 | +import {ErrorIcon} from '../../icons'; |
| 16 | +import {styles} from './filled-text-input.styles'; |
| 17 | +import {type IconProps} from '../../icons/icon-props'; |
| 18 | +import {useTextInputColors} from '../use-text-input-colors.hook'; |
| 19 | +import {useTextInputFocus} from '../use-text-input-focus-anim.hook'; |
| 20 | +import {useTypography} from '../../typography/useTypography.component'; |
| 21 | + |
| 22 | +export interface FilledTextInputProps<T> extends TextInputProps { |
| 23 | + label: string; |
| 24 | + |
| 25 | + disabled?: boolean; |
| 26 | + errorText?: string; |
| 27 | + suportingText?: string; |
| 28 | + |
| 29 | + leadingIconProps?: T; |
| 30 | + trailingIconProps?: T; |
| 31 | + trailingIcon?: React.FC<T>; |
| 32 | + leadingIcon?: React.FC<T>; |
| 33 | + |
| 34 | + leadingComponent?: ReactNode; |
| 35 | + trailingComponent?: ReactNode; |
| 36 | + |
| 37 | + labelStyle?: StyleProp<ViewStyle>; |
| 38 | + supportingTextStyle?: StyleProp<TextStyle>; |
| 39 | + innerContainerStyle?: StyleProp<ViewStyle>; |
| 40 | + outerContainerStyle?: StyleProp<ViewStyle>; |
| 41 | + activeIndicatorStyle?: StyleProp<ViewStyle>; |
| 42 | + |
| 43 | + onOuterContainerLayout?: (e: LayoutChangeEvent) => void; |
| 44 | +} |
| 45 | + |
| 46 | +const FOCUSED_LABEL_TOP_PLACEMENT = 8; |
| 47 | +const DEFAULT_LABEL_SMALL_FONT_SIZE = 12; |
| 48 | +const DEFAULT_LABEL_LARGE_FONT_SIZE = 16; |
| 49 | +const ACTIVE_INDICATOR_FOCUSED_HEIGHT = 3; |
| 50 | +const ACTIVE_INDICATOR_UNFOCUSED_HEIGHT = 1; |
| 51 | + |
| 52 | +export const FilledTextInput = forwardRef( |
| 53 | + <T extends IconProps>( |
| 54 | + { |
| 55 | + label, |
| 56 | + placeholder, |
| 57 | + |
| 58 | + errorText, |
| 59 | + suportingText, |
| 60 | + disabled = false, |
| 61 | + |
| 62 | + leadingIcon, |
| 63 | + trailingIcon, |
| 64 | + leadingIconProps = {} as T, |
| 65 | + trailingIconProps = {} as T, |
| 66 | + |
| 67 | + leadingComponent, |
| 68 | + trailingComponent, |
| 69 | + |
| 70 | + style, |
| 71 | + labelStyle, |
| 72 | + outerContainerStyle, |
| 73 | + innerContainerStyle, |
| 74 | + supportingTextStyle, |
| 75 | + activeIndicatorStyle, |
| 76 | + |
| 77 | + onFocus, |
| 78 | + onBlur, |
| 79 | + onOuterContainerLayout, |
| 80 | + ...props |
| 81 | + }: FilledTextInputProps<T>, |
| 82 | + ref: React.Ref<TextInput> |
| 83 | + ) => { |
| 84 | + const {bodyLarge, bodySmall} = useTypography(); |
| 85 | + // eslint-disable-next-line react-hooks/rules-of-hooks |
| 86 | + const inputRef = (ref as React.MutableRefObject<TextInput>) || useRef<TextInput>(null); |
| 87 | + const { |
| 88 | + valueColor, |
| 89 | + containerColor, |
| 90 | + selectionColor, |
| 91 | + placeholderColor, |
| 92 | + labelFocusedColor, |
| 93 | + trailingIconColor, |
| 94 | + leadingIconColor, |
| 95 | + labelUnfocusedColor, |
| 96 | + supportingTextColor, |
| 97 | + activeIndicatorFocusedColor, |
| 98 | + activeIndicatorUnfocusedColor, |
| 99 | + } = useTextInputColors({ |
| 100 | + disabled, |
| 101 | + isError: (errorText?.length ?? 0) > 0, |
| 102 | + }); |
| 103 | + const {focusAnim, focusAnimRange, focusInput, onInputBlur, onInputFocus} = useTextInputFocus({inputRef, onBlur, onFocus}); |
| 104 | + |
| 105 | + const LeadingIcon = leadingIcon; |
| 106 | + const TrailingIcon = errorText ? ErrorIcon : trailingIcon; |
| 107 | + |
| 108 | + const [smallLabelFontSize, largeLabelFontSize] = [ |
| 109 | + bodySmall.fontSize ?? DEFAULT_LABEL_SMALL_FONT_SIZE, |
| 110 | + bodyLarge.fontSize ?? DEFAULT_LABEL_LARGE_FONT_SIZE, |
| 111 | + ]; |
| 112 | + const [unfocusedLabelFontSize, unfocusedLabelTopPlacement] = placeholder |
| 113 | + ? [smallLabelFontSize, 0] |
| 114 | + : [largeLabelFontSize, FOCUSED_LABEL_TOP_PLACEMENT]; |
| 115 | + |
| 116 | + const labelAnimatedStyles = useAnimatedStyle( |
| 117 | + () => ({ |
| 118 | + top: interpolate(focusAnim.value, focusAnimRange, [unfocusedLabelTopPlacement, 0]), |
| 119 | + color: interpolateColor(focusAnim.value, focusAnimRange, [labelUnfocusedColor, labelFocusedColor] as string[]), |
| 120 | + fontSize: interpolate(focusAnim.value, focusAnimRange, [unfocusedLabelFontSize, smallLabelFontSize]), |
| 121 | + }), |
| 122 | + [labelUnfocusedColor, labelFocusedColor, unfocusedLabelTopPlacement, unfocusedLabelFontSize, smallLabelFontSize] |
| 123 | + ); |
| 124 | + |
| 125 | + const activeIndicatorAnimatedStyles = useAnimatedStyle( |
| 126 | + () => ({ |
| 127 | + backgroundColor: interpolateColor(focusAnim.value, focusAnimRange, [activeIndicatorUnfocusedColor, activeIndicatorFocusedColor] as string[]), |
| 128 | + height: interpolate(focusAnim.value, focusAnimRange, [ACTIVE_INDICATOR_UNFOCUSED_HEIGHT, ACTIVE_INDICATOR_FOCUSED_HEIGHT]), |
| 129 | + }), |
| 130 | + [activeIndicatorUnfocusedColor, activeIndicatorFocusedColor] |
| 131 | + ); |
| 132 | + |
| 133 | + return ( |
| 134 | + <View style={outerContainerStyle} onLayout={onOuterContainerLayout}> |
| 135 | + <TouchableWithoutFeedback onPress={focusInput}> |
| 136 | + <Animated.View style={[styles.container, {backgroundColor: containerColor}, innerContainerStyle]}> |
| 137 | + {leadingComponent} |
| 138 | + {LeadingIcon ? <LeadingIcon color={leadingIconColor} style={styles.leadingIcon} {...leadingIconProps} /> : null} |
| 139 | + <View style={[styles.inputWithLabelContainer]}> |
| 140 | + <Animated.Text style={[styles.label, bodyLarge, labelAnimatedStyles, labelStyle]}>{label}</Animated.Text> |
| 141 | + <TextInput |
| 142 | + ref={inputRef} |
| 143 | + onBlur={onInputBlur} |
| 144 | + onFocus={onInputFocus} |
| 145 | + editable={!disabled} |
| 146 | + placeholder={placeholder} |
| 147 | + selectionColor={selectionColor} |
| 148 | + placeholderTextColor={placeholderColor} |
| 149 | + style={[styles.input, bodyLarge, {color: valueColor}, style]} |
| 150 | + {...props} |
| 151 | + /> |
| 152 | + </View> |
| 153 | + {TrailingIcon ? <TrailingIcon color={trailingIconColor} style={styles.trailingIcon} {...trailingIconProps} /> : null} |
| 154 | + {trailingComponent} |
| 155 | + <Animated.View style={[styles.activeIndicator, activeIndicatorAnimatedStyles, activeIndicatorStyle]} /> |
| 156 | + </Animated.View> |
| 157 | + </TouchableWithoutFeedback> |
| 158 | + {suportingText || errorText ? ( |
| 159 | + <Text style={[bodySmall, styles.supportingText, {color: supportingTextColor}, supportingTextStyle]}>{errorText ?? suportingText}</Text> |
| 160 | + ) : null} |
| 161 | + </View> |
| 162 | + ); |
| 163 | + } |
| 164 | +); |
0 commit comments