import React, { useState } from 'react'; import { StyleSheet, View } from 'react-native'; import { renderTextLikeJSX } from '../../core/helpers'; import { NavBar } from '@xrnjs/ui'; import { IconMASearch1 } from '../../icons'; import Input from '../Input'; import { InputFocusEvent } from '../Input/interface'; import ShouldRender from '../ShouldRender'; import { SearchBarProps } from './interface'; import styles from './style/search-bar.style'; import { useControllableValue } from 'ahooks'; const SearchBar: React.FC = (props) => { const { style, wrapStyle, leftExtra, leftExtraStyle, rightExtra, rightExtraStyle, onFocus, onBlur, inputStyle, searchIcon = true, onChange, colorType = 'default', title, allowClear, inputWrapperStyle, backgroundColor, ...inputProps } = props; const [value, setValue] = useControllableValue( props ); const [focus, setFocus] = useState(false); const handleFocus = (e: InputFocusEvent) => { setFocus(true); onFocus?.(e); }; const handleBlur = (e: InputFocusEvent) => { setFocus(false); onBlur?.(e); }; const handleChange = (val: string | number) => { setValue(val); onChange?.(val); }; const typeStyle = { default: styles.default, primary: styles.primary, }[colorType]; const renderLeftExtra = () => ( {leftExtra} ); const renderRightExtra = () => ( {rightExtra} ); const titleJSX = renderTextLikeJSX( title, StyleSheet.flatten([ styles.title, { color: typeStyle.color, }, ]) ); const renderInput = () => ( 0)) && styles.inputWrapperFocus, colorType === 'primary' && styles.inputWrapperPrimary, inputWrapperStyle, ])} > ), })} placeholderTextColor={colorType === 'primary' ? '#fff' : '#999'} allowClear={allowClear || colorType !== 'primary'} size="small" {...inputProps} onFocus={handleFocus} onBlur={handleBlur} onChange={handleChange} borderNone={colorType === 'primary'} /> ); return ( {titleJSX} {renderLeftExtra()} {renderInput()} {renderRightExtra()} } /> ); }; export default SearchBar;