import React, { Component } from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import { translatable } from 'react-instantsearch-core'; import { createClassNames } from '../core/utils'; const cx = createClassNames('SearchBox'); const defaultLoadingIndicator = ( ); const defaultReset = ( ); const defaultSubmit = ( ); class SearchBox extends Component { static propTypes = { currentRefinement: PropTypes.string, className: PropTypes.string, refine: PropTypes.func.isRequired, translate: PropTypes.func.isRequired, loadingIndicator: PropTypes.node, reset: PropTypes.node, submit: PropTypes.node, focusShortcuts: PropTypes.arrayOf( PropTypes.oneOfType([PropTypes.string, PropTypes.number]) ), autoFocus: PropTypes.bool, searchAsYouType: PropTypes.bool, onSubmit: PropTypes.func, onReset: PropTypes.func, onChange: PropTypes.func, isSearchStalled: PropTypes.bool, showLoadingIndicator: PropTypes.bool, formRef: PropTypes.oneOfType([ PropTypes.func, PropTypes.exact({ current: PropTypes.object }), ]), inputRef: PropTypes.oneOfType([ PropTypes.func, PropTypes.exact({ current: PropTypes.object }), ]), inputId: PropTypes.string, }; static defaultProps = { currentRefinement: '', className: '', focusShortcuts: ['s', '/'], autoFocus: false, searchAsYouType: true, showLoadingIndicator: false, isSearchStalled: false, loadingIndicator: defaultLoadingIndicator, reset: defaultReset, submit: defaultSubmit, }; constructor(props) { super(); this.state = { query: props.searchAsYouType ? null : props.currentRefinement, }; } componentDidMount() { document.addEventListener('keydown', this.onKeyDown); } componentWillUnmount() { document.removeEventListener('keydown', this.onKeyDown); } componentDidUpdate(prevProps) { if ( !this.props.searchAsYouType && prevProps.currentRefinement !== this.props.currentRefinement ) { this.setState({ query: this.props.currentRefinement, }); } } getQuery = () => this.props.searchAsYouType ? this.props.currentRefinement : this.state.query; onInputMount = (input) => { this.input = input; if (!this.props.inputRef) return; if (typeof this.props.inputRef === 'function') { this.props.inputRef(input); } else { this.props.inputRef.current = input; } }; // From https://github.com/algolia/autocomplete.js/pull/86 onKeyDown = (e) => { if (!this.props.focusShortcuts) { return; } const shortcuts = this.props.focusShortcuts.map((key) => typeof key === 'string' ? key.toUpperCase().charCodeAt(0) : key ); const elt = e.target || e.srcElement; const tagName = elt.tagName; if ( elt.isContentEditable || tagName === 'INPUT' || tagName === 'SELECT' || tagName === 'TEXTAREA' ) { // already in an input return; } const which = e.which || e.keyCode; if (shortcuts.indexOf(which) === -1) { // not the right shortcut return; } this.input.focus(); e.stopPropagation(); e.preventDefault(); }; onSubmit = (e) => { e.preventDefault(); e.stopPropagation(); this.input.blur(); const { refine, searchAsYouType } = this.props; if (!searchAsYouType) { refine(this.getQuery()); } return false; }; onChange = (event) => { const { searchAsYouType, refine, onChange } = this.props; const value = event.target.value; if (searchAsYouType) { refine(value); } else { this.setState({ query: value }); } if (onChange) { onChange(event); } }; onReset = (event) => { const { searchAsYouType, refine, onReset } = this.props; refine(''); this.input.focus(); if (!searchAsYouType) { this.setState({ query: '' }); } if (onReset) { onReset(event); } }; render() { const { className, inputId, translate, autoFocus, loadingIndicator, submit, reset, } = this.props; const query = this.getQuery(); const searchInputEvents = Object.keys(this.props).reduce((props, prop) => { if ( ['onsubmit', 'onreset', 'onchange'].indexOf(prop.toLowerCase()) === -1 && prop.indexOf('on') === 0 ) { return { ...props, [prop]: this.props[prop] }; } return props; }, {}); const isSearchStalled = this.props.showLoadingIndicator && this.props.isSearchStalled; return (