import React from 'react'; import PropTypes from 'prop-types'; import {KEYCODES} from './constants'; import {gettext} from 'core/utils'; /** * @ngdoc react * @name SearchBox * @description Search box with input to search */ class SearchBox extends React.Component { static propTypes: any; static defaultProps: any; constructor(props) { super(props); this.state = {inputValue: this.props.value}; this.onChangeHandler = this.onChangeHandler.bind(this); this.onKeyPressHandler = this.onKeyPressHandler.bind(this); } componentWillReceiveProps(nextProps) { if (this.props.value !== nextProps.value && this.props.activeFilter !== nextProps.activeFilter) { this.setState({inputValue: nextProps.value}); } if (this.state.inputValue !== '' && this.props.value !== '' && nextProps.value === '' && this.props.activeFilter === nextProps.activeFilter) { this.setState({inputValue: nextProps.value}); } } onChangeHandler(evt) { this.setState({inputValue: evt.target.value}); } onKeyPressHandler(evt) { if (evt.charCode === KEYCODES.ENTER) { this.props.search(this.state.inputValue); } } render() { return (
); } } SearchBox.propTypes = { label: PropTypes.string.isRequired, value: PropTypes.string.isRequired, search: PropTypes.func.isRequired, activeFilter: PropTypes.string.isRequired, }; export default SearchBox;