From 58cda9cc7fb79ca9df6746de7f9662bc08dc156a Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Fri, 13 Oct 2017 12:29:40 +0200 Subject: initial commit --- src/components/ui/SearchInput.js | 124 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 src/components/ui/SearchInput.js (limited to 'src/components/ui/SearchInput.js') diff --git a/src/components/ui/SearchInput.js b/src/components/ui/SearchInput.js new file mode 100644 index 000000000..bca412cef --- /dev/null +++ b/src/components/ui/SearchInput.js @@ -0,0 +1,124 @@ +import React, { Component } from 'react'; +import PropTypes from 'prop-types'; +import { observer } from 'mobx-react'; +import classnames from 'classnames'; +import uuidv1 from 'uuid/v1'; +import { debounce } from 'lodash'; + +@observer +export default class SearchInput extends Component { + static propTypes = { + value: PropTypes.string, + defaultValue: PropTypes.string, + className: PropTypes.string, + onChange: PropTypes.func, + onReset: PropTypes.func, + name: PropTypes.string, + throttle: PropTypes.bool, + throttleDelay: PropTypes.number, + }; + + static defaultProps = { + value: '', + defaultValue: '', + className: '', + name: uuidv1(), + throttle: false, + throttleDelay: 250, + onChange: () => null, + onReset: () => null, + } + + constructor(props) { + super(props); + + this.state = { + value: props.value || props.defaultValue, + }; + + this.throttledOnChange = debounce(this.throttledOnChange, this.props.throttleDelay); + } + + onChange(e) { + const { throttle, onChange } = this.props; + const { value } = e.target; + this.setState({ value }); + + if (throttle) { + e.persist(); + this.throttledOnChange(value); + } else { + onChange(value); + } + } + + onClick() { + const { defaultValue } = this.props; + const { value } = this.state; + + if (value === defaultValue) { + this.setState({ value: '' }); + } + + this.input.focus(); + } + + onBlur() { + const { defaultValue } = this.props; + const { value } = this.state; + + if (value === '') { + this.setState({ value: defaultValue }); + } + } + + throttledOnChange(e) { + const { onChange } = this.props; + + onChange(e); + } + + reset() { + const { defaultValue, onReset } = this.props; + this.setState({ value: defaultValue }); + + onReset(); + } + + input = null; + + render() { + const { className, name, defaultValue } = this.props; + const { value } = this.state; + + return ( +
+
+ ); + } +} -- cgit v1.2.3-70-g09d2