import { View } from '@tarojs/components' import classnames from 'classnames' import type { ReactNode } from 'react' import { forwardRef, memo, useEffect, useImperativeHandle, useState } from 'react' import IconFont from '../iconfont/iconfont' import InputX from '../InputX/index' import styles from './index.module.scss' import CloseBtn from '@/components/closeBtn' interface Params { clickOnSearch?: (val: string) => void disabled?: boolean placeholder?: string changeOnSearch?: (value: string) => void showIcon?: boolean placeIcon?: 'out' | 'inner' style?: Object showBtn?: boolean btnStyle?: Object btnTitle?: string defaultValue?: string children?: ReactNode customRightSlot?: ReactNode adjustPosition?: boolean showScan?: boolean handScan?: () => void cursorSpacing?: Number } const Search = ( { clickOnSearch, // 点击筛选按钮触发 changeOnSearch, // 输入文字触发 adjustPosition = false, disabled = false, // 是否禁用 placeholder = '输入搜索内容', showIcon = true, // 是否显示关闭图标 showBtn = false, // 是否显示搜索按钮 btnStyle = {}, placeIcon = 'inner', // 搜索图标位置:inner在里面,out在外面 btnTitle = '搜索', // 搜索文字 defaultValue = '', // 默认值 children, customRightSlot, showScan = false, handScan, cursorSpacing = 0, }: Params, ref, ) => { const [inputCon, setInputCon] = useState('') useEffect(() => { setInputCon(defaultValue) }, [defaultValue]) const onInputEven = (e) => { const value = e.detail.value setInputCon(value) changeOnSearch?.(value) } const clearInput = () => { setInputCon('') changeOnSearch?.('') } useImperativeHandle(ref, () => ({ clearInput, })) const onSearch = () => { clickOnSearch?.(inputCon) } return ( <> {showIcon && ( )} onInputEven(e)} customClassName={classnames(placeIcon == 'out' && styles.input_out)} customStyle={{ width: '100%' }} /> {!!inputCon && clearInput()} styleObj={{ width: '20rpx', height: '20rpx', backgroundColor: '#fff', border: '0' }} />} {showScan && ( { handScan?.() }} > )} {customRightSlot} {showBtn && ( {btnTitle} )} {children} ) } export default memo( forwardRef( Search, ), )