Skip to content

Instantly share code, notes, and snippets.

@tricoder42
Created November 23, 2015 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tricoder42/bcf88dc5ce8b76ae79df to your computer and use it in GitHub Desktop.
Save tricoder42/bcf88dc5ce8b76ae79df to your computer and use it in GitHub Desktop.
Usage of `uncontrollable`
const PureInput = React.createClass({
propTypes: {
name: React.PropTypes.string.isRequired,
label: React.PropTypes.string,
type: React.PropTypes.string,
placeholder: React.PropTypes.string,
// value/callback pair
value: React.PropTypes.string,
onChange: React.PropTypes.func.isRequired,
focused: React.PropTypes.bool,
onFocus: React.PropTypes.func.isRequired,
},
getDefaultProps(): Object {
return {
type: "text",
focused: false,
};
},
render(): React.Element {
const fieldClass = cx(
"field", {
"field-focus": this.props.focused || !!this.props.value
}
);
return (
<div className={fieldClass}>
<label className="field__label">
{this.props.label}
</label>
<input
className="field__input"
type={this.props.type}
name={this.props.name}
value={this.props.value}
placeholder={this.props.placeholder}
onChange={(event) => this.props.onChange(event.target.value)}
onFocus={() => this.props.onFocus(true)}
onBlur={() => this.props.onFocus(false)}
/>
</div>
);
}
});
const Input = uncontrollable(
PureInput,
{
value: "onChange",
focused: "onFocus",
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment