@lyra/form-builder
Version:
Lyra form builder
71 lines (58 loc) • 1.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/*:: import type {Node} from 'react'*/
/*:: import type {ObservableI, Subscription} from '../typedefs/observable'*/
/*:: type Props = {
documentId: string,
materialize: string => ObservableI<Object>,
children: Object => null | Node
}*/
/*:: type State = {
materialized: ?Object
}*/
class WithMaterializedDocument extends _react2.default.Component /*:: <
Props,
State
>*/ {
constructor(...args) {
var _temp;
return _temp = super(...args), this.state = {
materialized: null
}, _temp;
}
componentDidMount() {
this.setDocId(this.props.documentId);
}
componentWillReceiveProps(nextProps /*: Props*/) {
if (this.props.documentId !== nextProps.documentId) {
this.setDocId(nextProps.documentId);
}
}
componentWillUnmount() {
this.unsubscribe();
}
unsubscribe() {
if (this.subscription) {
this.subscription.unsubscribe();
}
}
setDocId(docId /*: string*/) {
this.unsubscribe();
if (!docId) {
this.setState({ materialized: null });
return;
}
this.subscription = this.props.materialize(docId).subscribe(materialized => this.setState({ materialized }));
}
render() {
const materialized = this.state.materialized;
const children = this.props.children;
return materialized ? children(materialized) : null;
}
}
exports.default = WithMaterializedDocument;