You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.6 KiB
67 lines
1.6 KiB
import { useCallback, useState, useEffect, useRef } from 'react'; |
|
|
|
import { FormattedMessage } from 'react-intl'; |
|
|
|
export const ColumnSearchHeader: React.FC<{ |
|
onBack: () => void; |
|
onSubmit: (value: string) => void; |
|
onActivate: () => void; |
|
placeholder: string; |
|
active: boolean; |
|
}> = ({ onBack, onActivate, onSubmit, placeholder, active }) => { |
|
const inputRef = useRef<HTMLInputElement>(null); |
|
const [value, setValue] = useState(''); |
|
|
|
useEffect(() => { |
|
if (!active) { |
|
setValue(''); |
|
} |
|
}, [active]); |
|
|
|
const handleChange = useCallback( |
|
({ target: { value } }: React.ChangeEvent<HTMLInputElement>) => { |
|
setValue(value); |
|
onSubmit(value); |
|
}, |
|
[setValue, onSubmit], |
|
); |
|
|
|
const handleKeyUp = useCallback( |
|
(e: React.KeyboardEvent<HTMLInputElement>) => { |
|
if (e.key === 'Escape') { |
|
e.preventDefault(); |
|
onBack(); |
|
inputRef.current?.blur(); |
|
} |
|
}, |
|
[onBack], |
|
); |
|
|
|
const handleFocus = useCallback(() => { |
|
onActivate(); |
|
}, [onActivate]); |
|
|
|
const handleSubmit = useCallback(() => { |
|
onSubmit(value); |
|
}, [onSubmit, value]); |
|
|
|
return ( |
|
<form className='column-search-header' onSubmit={handleSubmit}> |
|
<input |
|
ref={inputRef} |
|
type='search' |
|
value={value} |
|
onChange={handleChange} |
|
onKeyUp={handleKeyUp} |
|
placeholder={placeholder} |
|
onFocus={handleFocus} |
|
/> |
|
|
|
{active && ( |
|
<button type='button' className='link-button' onClick={onBack}> |
|
<FormattedMessage id='column_search.cancel' defaultMessage='Cancel' /> |
|
</button> |
|
)} |
|
</form> |
|
); |
|
};
|
|
|