Skip to content

Commit

Permalink
debounce text input
Browse files Browse the repository at this point in the history
  • Loading branch information
danielduan committed Apr 12, 2018
1 parent 62243b7 commit 8819761
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions addons/knobs/src/components/types/Text.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import PropTypes from 'prop-types';
import React from 'react';
import Textarea from 'react-textarea-autosize';
import debounce from 'lodash.debounce';

const styles = {
display: 'table-cell',
Expand All @@ -16,14 +17,32 @@ const styles = {
color: '#555',
};

const TextType = ({ knob, onChange }) => (
<Textarea
id={knob.name}
style={styles}
value={knob.value}
onChange={e => onChange(e.target.value)}
/>
);
class TextType extends React.Component {
constructor(props, context) {
super(props, context);

this.state = {
value: props.knob.value,
};

this.onChange = debounce(props.onChange, 200);
}

handleChange = event => {
const { value } = event.target;

this.setState({ value });

this.onChange(value);
};

render() {
const { knob } = this.props;
const { value } = this.state;

return <Textarea id={knob.name} style={styles} value={value} onChange={this.handleChange} />;
}
}

TextType.defaultProps = {
knob: {},
Expand Down

0 comments on commit 8819761

Please sign in to comment.