import React from 'react'; import { Animated, StyleSheet } from 'react-native'; import AnimatedText from '../../Typography/AnimatedText'; import type { InputLabelProps } from '../types'; const InputLabel = (props: InputLabelProps) => { const { parentState, labelBackground } = props; const { label, error, onLayoutAnimatedText, hasActiveOutline, activeColor, placeholderStyle, baseLabelTranslateX, baseLabelTranslateY, font, fontSize, fontWeight, placeholderOpacity, wiggleOffsetX, labelScale, topPosition, paddingOffset, placeholderColor, errorColor, labelTranslationXOffset, maxFontSizeMultiplier, } = props.labelProps; const labelTranslationX = { transform: [ { // Offset label scale since RN doesn't support transform origin translateX: parentState.labeled.interpolate({ inputRange: [0, 1], outputRange: [baseLabelTranslateX, labelTranslationXOffset || 0], }), }, ], }; const labelStyle = { ...font, fontSize, fontWeight, transform: [ { // Wiggle the label when there's an error translateX: parentState.error.interpolate({ inputRange: [0, 0.5, 1], outputRange: [0, parentState.value && error ? wiggleOffsetX : 0, 0], }), }, { // Move label to top translateY: parentState.labeled.interpolate({ inputRange: [0, 1], outputRange: [baseLabelTranslateY, 0], }), }, { // Make label smaller scale: parentState.labeled.interpolate({ inputRange: [0, 1], outputRange: [labelScale, 1], }), }, ], }; return label ? ( // Position colored placeholder and gray placeholder on top of each other and crossfade them // This gives the effect of animating the color, but allows us to use native driver {labelBackground?.({ parentState, labelStyle, labelProps: props.labelProps, maxFontSizeMultiplier: maxFontSizeMultiplier, })} {label} {label} ) : null; }; const styles = StyleSheet.create({ labelContainer: { zIndex: 3, }, }); export default InputLabel;