import React, {useCallback} from 'react'; import type {SearchBoxProps} from '../hyper'; import {VscArrowUp} from '@react-icons/all-files/vsc/VscArrowUp'; import {VscArrowDown} from '@react-icons/all-files/vsc/VscArrowDown'; import {VscClose} from '@react-icons/all-files/vsc/VscClose'; import {VscCaseSensitive} from '@react-icons/all-files/vsc/VscCaseSensitive'; import {VscRegex} from '@react-icons/all-files/vsc/VscRegex'; import {VscWholeWord} from '@react-icons/all-files/vsc/VscWholeWord'; import clsx from 'clsx'; type SearchButtonColors = { foregroundColor: string; selectionColor: string; backgroundColor: string; }; type SearchButtonProps = React.PropsWithChildren< { onClick: () => void; active: boolean; title: string; } & SearchButtonColors >; const SearchButton = ({ onClick, active, title, foregroundColor, backgroundColor, selectionColor, children }: SearchButtonProps) => { const handleKeyUp = useCallback( (event: React.KeyboardEvent) => { if (event.key === 'Enter' || event.key === ' ') { onClick(); } }, [onClick] ); return (
{children}
); }; class SearchBox extends React.PureComponent { searchTerm: string; input: HTMLInputElement | null = null; searchButtonColors: SearchButtonColors; constructor(props: SearchBoxProps) { super(props); this.searchTerm = ''; this.searchButtonColors = { backgroundColor: this.props.borderColor, selectionColor: this.props.selectionColor, foregroundColor: this.props.foregroundColor }; } handleChange = (event: React.KeyboardEvent) => { this.searchTerm = event.currentTarget.value; if (event.shiftKey && event.key === 'Enter') { this.props.prev(this.searchTerm); } else if (event.key === 'Enter') { this.props.next(this.searchTerm); } }; componentDidMount(): void { this.input?.focus(); } render() { const { caseSensitive, wholeWord, regex, results, toggleCaseSensitive, toggleWholeWord, toggleRegex, next, prev, close, backgroundColor, foregroundColor, borderColor, selectionColor, font } = this.props; return (
{ this.input = input; }} placeholder="Search" >
{results === undefined ? '' : results.resultCount === 0 ? 'No results' : `${results.resultIndex + 1} of ${results.resultCount}`}
prev(this.searchTerm)} active={false} title="Previous Match" {...this.searchButtonColors} > next(this.searchTerm)} active={false} title="Next Match" {...this.searchButtonColors} > close()} active={false} title="Close" {...this.searchButtonColors}>
); } } export default SearchBox;