mirror of
https://github.com/Kong/insomnia
synced 2024-11-08 23:00:30 +00:00
52 lines
1.2 KiB
JavaScript
52 lines
1.2 KiB
JavaScript
|
import React, {Component, PropTypes} from 'react';
|
||
|
|
||
|
const DEFAULT_DEBOUNCE_MILLIS = 300;
|
||
|
|
||
|
class Input extends Component {
|
||
|
valueChanged (e) {
|
||
|
if (!this.props.onChange) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// Surround in closure because callback may change before debounce
|
||
|
clearTimeout(this._timeout);
|
||
|
((value, cb, debounceMillis = DEFAULT_DEBOUNCE_MILLIS) => {
|
||
|
this._timeout = setTimeout(() => cb(value), debounceMillis);
|
||
|
})(e.target.value, this.props.onChange, this.props.debounceMillis);
|
||
|
}
|
||
|
|
||
|
updateValueFromProps() {
|
||
|
this.refs.input.value = this.props.initialValue || this.props.value || '';
|
||
|
}
|
||
|
|
||
|
componentDidMount () {
|
||
|
this.updateValueFromProps()
|
||
|
}
|
||
|
|
||
|
componentDidUpdate () {
|
||
|
this.updateValueFromProps()
|
||
|
}
|
||
|
|
||
|
render () {
|
||
|
const {initialValue, value} = this.props;
|
||
|
return (
|
||
|
<input
|
||
|
ref="input"
|
||
|
type="text"
|
||
|
initialValue={initialValue || value}
|
||
|
onChange={this.valueChanged.bind(this)}
|
||
|
placeholder="https://google.com"
|
||
|
/>
|
||
|
)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Input.propTypes = {
|
||
|
onChange: PropTypes.func.isRequired,
|
||
|
initialValue: PropTypes.string,
|
||
|
debounceMillis: PropTypes.number,
|
||
|
value: PropTypes.string
|
||
|
};
|
||
|
|
||
|
export default Input;
|