import React from 'react'; import { StyleProp, StyleSheet, View, ViewStyle } from 'react-native'; import type { $Omit } from '../../../../src/types'; import type { IconSource } from '../../Icon'; import IconButton from '../../IconButton'; export type Props = $Omit< React.ComponentProps, 'icon' | 'theme' | 'color' > & { /** * Icon to show. */ name: IconSource; /** * Function to execute on press. */ onPress?: () => void; /** * Whether the TextInput will focus after onPress. */ forceTextInputFocus?: boolean; /** * Color of the icon or a function receiving a boolean indicating whether the TextInput is focused and returning the color. */ color?: ((isTextInputFocused: boolean) => string | undefined) | string; style?: StyleProp; /** * @optional */ theme?: ReactNativePaper.Theme; }; export const ICON_SIZE = 24; const ICON_OFFSET = 12; type StyleContextType = { style: StyleProp; isTextInputFocused: boolean; forceFocus: () => void; }; const StyleContext = React.createContext({ style: {}, isTextInputFocused: false, forceFocus: () => {}, }); const IconAdornment: React.FunctionComponent< { testID: string; icon: React.ReactNode; topPosition: number; side: 'left' | 'right'; } & Omit > = ({ icon, topPosition, side, isTextInputFocused, forceFocus }) => { const style = { top: topPosition, [side]: ICON_OFFSET, }; const contextState = { style, isTextInputFocused, forceFocus }; return ( {icon} ); }; /** * A component to render a leading / trailing icon 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 TextInputIcon = ({ name, onPress, forceTextInputFocus, color, ...rest }: Props) => { const { style, isTextInputFocused, forceFocus } = React.useContext(StyleContext); const onPressWithFocusControl = React.useCallback(() => { if (forceTextInputFocus && !isTextInputFocused) { forceFocus(); } onPress?.(); }, [forceTextInputFocus, forceFocus, isTextInputFocused, onPress]); return ( ); }; TextInputIcon.displayName = 'TextInput.Icon'; TextInputIcon.defaultProps = { forceTextInputFocus: true, }; const styles = StyleSheet.create({ container: { position: 'absolute', width: ICON_SIZE, height: ICON_SIZE, justifyContent: 'center', alignItems: 'center', }, iconButton: { margin: 0, }, }); export default TextInputIcon; // @component-docs ignore-next-line export { IconAdornment };