import React from 'react'; import {ThemeProps, themeable} from '../theme'; import {Icon} from './icons'; import {uncontrollable} from 'uncontrollable'; import {autobind} from '../utils/helper'; import {LocaleProps, localeable} from '../locale'; import debounce from 'lodash/debounce'; export interface SearchBoxProps extends ThemeProps, LocaleProps { name?: string; disabled?: boolean; mini?: boolean; searchImediately?: boolean; onChange?: (text: string) => void; placeholder?: string; defaultValue?: string; value?: string; active?: boolean; defaultActive?: boolean; onActiveChange?: (active: boolean) => void; onSearch?: (value: string) => void; onCancel?: () => void; } export class SearchBox extends React.Component { inputRef: React.RefObject = React.createRef(); static defaultProps = { mini: true, searchImediately: true }; lazyEmitSearch = debounce( () => { const onSearch = this.props.onSearch; onSearch?.(this.props.value || ''); }, 250, { leading: false, trailing: true } ); componentWillUnmount() { this.lazyEmitSearch.cancel(); } @autobind handleActive() { const {onActiveChange} = this.props; onActiveChange?.(true); this.inputRef.current?.focus(); } @autobind handleCancel() { const {onActiveChange, onChange, onCancel} = this.props; onActiveChange?.(false); onCancel?.(); onChange?.(''); } @autobind handleChange(e: React.ChangeEvent) { const {onChange, onSearch, searchImediately} = this.props; onChange?.(e.currentTarget.value); searchImediately && this.lazyEmitSearch(); } @autobind handleSearch() { const {value, onSearch} = this.props; onSearch?.(value || ''); } @autobind handleKeyDown(e: React.KeyboardEvent) { if (e.key === 'Enter') { this.handleSearch(); e.preventDefault(); } } render() { const { classnames: cx, value, active, name, className, onChange, disabled, placeholder, mini, translate: __ } = this.props; return (
{!mini ? ( ) : active ? ( ) : ( )}
); } } export default themeable( localeable( uncontrollable(SearchBox, { active: 'onActiveChange', value: 'onChange' }) ) );