import React, { forwardRef, useContext, useRef, useImperativeHandle } from 'react'; import { View, Text } from 'react-native'; import { Search as SearchIco } from '@pingtou/rn-vant-icons'; import { useControllableValue } from '../hooks'; import Field, { FieldInstance } from '../Field'; import type { KeyPressEvent } from '../Input/type'; import { useThemeFactory } from '../Theme'; import TouchableOpacity from '../TouchableOpacity'; import ConfigProvider from '../ConfigProvider/ConfigProviderContext'; import type { SearchProps, SearchInstance } from './types'; import { createStyle } from './style'; const Search = forwardRef((props, ref) => { const { background, shape = 'square', leftIcon = , label, action, showAction, actionText, onSearch, onCancel, ...rest } = props; const [value, setValue] = useControllableValue(props); const { locale } = useContext(ConfigProvider); const { styles, theme } = useThemeFactory(createStyle); const filedRef = useRef(null); const blur = () => { filedRef.current?.blur(); }; const focus = () => { filedRef.current?.focus(); }; useImperativeHandle(ref, () => ({ focus, blur, })); const onKeyPress = (e: KeyPressEvent) => { if (e.nativeEvent.key === 'Enter') { onSearch?.(value); } rest.onKeyPress?.(e); }; const renderAction = () => { if (action) return action; if (showAction) { return ( {actionText || locale.cancel} ); } return null; }; const renderLeftIcon = () => { if (React.isValidElement(leftIcon)) { return React.cloneElement<{ color: string }>(leftIcon as any, { color: theme.search_left_icon_color, }); } return leftIcon; }; return ( {label && {label}} {renderAction()} ); }); export default Search;