import color from 'color'; import React from 'react'; import { Animated, LayoutChangeEvent, StyleProp, StyleSheet, Text, TextStyle, ViewStyle, } from 'react-native'; import { withTheme } from '../../../core/theming'; import { AdornmentSide } from './enums'; const AFFIX_OFFSET = 12; export type Props = { /** * Text to show. */ text: string; onLayout?: (event: LayoutChangeEvent) => void; /** * Style that is passed to the Text element. */ textStyle?: StyleProp; /** * @optional */ theme: ReactNativePaper.Theme; }; type ContextState = { topPosition: number | null; onLayout?: (event: LayoutChangeEvent) => void; visible?: Animated.Value; textStyle?: StyleProp; side: AdornmentSide; paddingHorizontal?: number | string; maxFontSizeMultiplier?: number | undefined | null; }; const AffixContext = React.createContext({ textStyle: { fontFamily: '', color: '' }, topPosition: null, side: AdornmentSide.Left, }); const AffixAdornment: React.FunctionComponent< { affix: React.ReactNode; testID: string; } & ContextState > = ({ affix, side, textStyle, topPosition, onLayout, visible, paddingHorizontal, maxFontSizeMultiplier, }) => { return ( {affix} ); }; /** * A component to render a leading / trailing text in the TextInput * *
*
* *
*
* * ## Usage * ```js * import * as React from 'react'; * import { TextInput } from 'react-native-paper'; * * const MyComponent = () => { * const [text, setText] = React.useState(''); * * return ( * } * /> * ); * }; * * export default MyComponent; * ``` */ const TextInputAffix = ({ text, textStyle: labelStyle, theme }: Props) => { const { textStyle, onLayout, topPosition, side, visible, paddingHorizontal, maxFontSizeMultiplier, } = React.useContext(AffixContext); const textColor = color(theme.colors.text) .alpha(theme.dark ? 0.7 : 0.54) .rgb() .string(); const offset = typeof paddingHorizontal === 'number' ? paddingHorizontal : AFFIX_OFFSET; const style = { top: topPosition, [side]: offset, } as ViewStyle; return ( {text} ); }; TextInputAffix.displayName = 'TextInput.Affix'; const styles = StyleSheet.create({ container: { position: 'absolute', justifyContent: 'center', alignItems: 'center', }, }); export default withTheme(TextInputAffix); // @component-docs ignore-next-line export { TextInputAffix, AffixAdornment };