jsketcher/modules/ui/bind.js
Val Erastov (xibyte) e68aa71c95 react housekeeping
2020-03-24 01:03:15 -07:00

41 lines
1,011 B
JavaScript

import React from 'react';
import context from 'context';
export default function bind(streamProvider) {
return function (Component) {
return class Connected extends React.Component {
state = {hasError: false, value: null};
onChange = value => streamProvider(context.streams, this.props).next(value);
UNSAFE_componentWillMount() {
this.stream = streamProvider(context.streams, this.props);
this.detacher = this.stream.attach(value => {
this.setState({
hasError: false,
value
});
});
}
componentWillUnmount() {
this.detacher();
}
render() {
if (this.state.hasError) {
return null;
}
return <Component value={this.state.value}
onChange={this.onChange}
{...this.props} />;
}
componentDidCatch() {
this.setState({hasError: true});
}
};
};
}