import "./search-input.scss"; import React, { createRef } from "react"; import { observer } from "mobx-react"; import { autobind, cssNames } from "../../utils"; import { Icon } from "../icon"; import { Input, InputProps } from "./input"; interface Props extends InputProps { compact?: boolean; // show only search-icon when not focused bindGlobalFocusHotkey?: boolean; showClearIcon?: boolean; onClear?(): void; } const defaultProps: Partial = { autoFocus: true, bindGlobalFocusHotkey: true, showClearIcon: true, get placeholder() { return `Search...`; }, }; @observer export class SearchInput extends React.Component { static defaultProps = defaultProps as object; private inputRef = createRef(); componentDidMount() { if (!this.props.bindGlobalFocusHotkey) return; window.addEventListener("keydown", this.onGlobalKey); } componentWillUnmount() { window.removeEventListener("keydown", this.onGlobalKey); } @autobind() onGlobalKey(evt: KeyboardEvent) { const meta = evt.metaKey || evt.ctrlKey; if (meta && evt.key === "f") { this.inputRef.current.focus(); } } @autobind() onKeyDown(evt: React.KeyboardEvent) { if (this.props.onKeyDown) { this.props.onKeyDown(evt); } // clear on escape-key const escapeKey = evt.nativeEvent.code === "Escape"; if (escapeKey) { this.clear(); evt.stopPropagation(); } } @autobind() clear() { if (this.props.onClear) { this.props.onClear(); } else { this.inputRef.current.setValue(""); } } render() { const { className, compact, onClear, showClearIcon, bindGlobalFocusHotkey, value, ...inputProps } = this.props; let rightIcon = ; if (showClearIcon && value) { rightIcon = ; } return ( ); } }