/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at . */ // @flow import React, { Component } from "react"; import Svg from "./Svg"; import classnames from "classnames"; import CloseButton from "./Button/Close"; import "./SearchInput.css"; const arrowBtn = (onClick, type, className, tooltip) => { const props = { className, key: type, onClick, title: tooltip, type }; return ( ); }; type Props = { count: number, expanded: boolean, handleClose: (e: SyntheticMouseEvent) => void, handleNext?: (e: SyntheticMouseEvent) => void, handlePrev?: (e: SyntheticMouseEvent) => void, hasPrefix?: boolean, onBlur?: (e: SyntheticFocusEvent) => void, onChange: (e: SyntheticInputEvent) => void, onFocus?: (e: SyntheticFocusEvent) => void, onKeyDown: (e: SyntheticKeyboardEvent) => void, onKeyUp?: (e: SyntheticKeyboardEvent) => void, placeholder: string, query: string, selectedItemId?: string, shouldFocus?: boolean, showErrorEmoji: boolean, size: string, summaryMsg: string }; class SearchInput extends Component { displayName: "SearchInput"; $input: ?HTMLInputElement; static defaultProps = { expanded: false, hasPrefix: false, selectedItemId: "", size: "" }; componentDidMount() { this.setFocus(); } componentDidUpdate(prevProps: Props) { if (this.props.shouldFocus && !prevProps.shouldFocus) { this.setFocus(); } } setFocus() { if (this.$input) { const input = this.$input; input.focus(); if (!input.value) { return; } // omit prefix @:# from being selected const selectStartPos = this.props.hasPrefix ? 1 : 0; input.setSelectionRange(selectStartPos, input.value.length + 1); } } renderSvg() { const svgName = this.props.showErrorEmoji ? "sad-face" : "magnifying-glass"; return ; } renderArrowButtons() { const { handleNext, handlePrev } = this.props; return [ arrowBtn( handleNext, "arrow-down", classnames("nav-btn", "next"), L10N.getFormatStr("editor.searchResults.nextResult") ), arrowBtn( handlePrev, "arrow-up", classnames("nav-btn", "prev"), L10N.getFormatStr("editor.searchResults.prevResult") ) ]; } renderNav() { const { count, handleNext, handlePrev } = this.props; if ((!handleNext && !handlePrev) || (!count || count == 1)) { return; } return (
{this.renderArrowButtons()}
); } render() { const { expanded, handleClose, onBlur, onChange, onFocus, onKeyDown, onKeyUp, placeholder, query, selectedItemId, showErrorEmoji, size, summaryMsg } = this.props; const inputProps = { className: classnames({ empty: showErrorEmoji }), onChange, onKeyDown, onKeyUp, onFocus, onBlur, "aria-autocomplete": "list", "aria-controls": "result-list", "aria-activedescendant": expanded && selectedItemId ? `${selectedItemId}-title` : "", placeholder, value: query, spellCheck: false, ref: c => (this.$input = c) }; return (
{this.renderSvg()} {summaryMsg &&
{summaryMsg}
} {this.renderNav()}
); } } export default SearchInput;