UNPKG

@aappddeevv/dynamics-client-ui

Version:

## What is it? A library to help you create great dynamics applications.

96 lines 3.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const React = require("react"); const PropTypes = require("prop-types"); const recompose_1 = require("recompose"); const Utils = require("./Utils"); const NotificationManager_1 = require("./NotificationManager"); const ErrorHandler_1 = require("./ErrorHandler"); tslib_1.__exportStar(require("./NotificationManager"), exports); /** Not used yet. */ exports.dynamicsShape = PropTypes.shape({ notifier: PropTypes.object, xrm: PropTypes.object, errorHandler: PropTypes.object }); /** * Render the first child only. Places Xrm and a NotificationManager into the context. * Declare a child's use of the context: * ``` * class Foo extends React.Component { * public static contextTypes = { * Xrm: PropTypes.object, // can use isRequired * notifier: PropTypes.instanceOf(Object) // can use isRequired * } * constructor(props, context) { * super(props, context); * ... * } * ... * ``` * You can access the context using `this.context.notifier.add(..)`. Instead of retyping * the contextTypes in the child, you can use `public static contextTypes { ...Dynamics.childContextTypes }`. * * The component is stateless as only the children define the render. * * TODO: Notifier only works on forms, make more general so it works in non-forms. */ class Dynamics extends React.Component { constructor(props, context) { super(props, context); this.defaultErrorHandler = new ErrorHandler_1.ConsoleErrorHandler(); this.defaultNotifier = new NotificationManager_1.NotificationManager(() => this.getXrm()); /** Get Xrm from the props or the global environment window.parent. */ this.getXrm = () => { //console.log("Dyanmics.getXrm()", this) if (this.props && typeof this.props.xrm !== "undefined" && this.props.xrm !== null) return this.props.xrm; return Utils.getXrm(); }; } getChildContext() { return { notifier: this.notifier, xrm: this.getXrm(), errorHandler: this.errorHandler, }; } get notifier() { return (this.props && this.props.notifier) ? this.props.notifier : this.defaultNotifier; } get errorHandler() { return this.props.errorHandler ? this.props.errorHandler : this.defaultErrorHandler; } render() { const { children } = this.props; return React.Children.only(children); } } Dynamics.childContextTypes = { notifier: PropTypes.object, xrm: PropTypes.object, errorHandler: PropTypes.object }; exports.Dynamics = Dynamics; /** Use this to compose your component with Xrm and NotificationManager * in the props. * * const YourComponent = ({notificationManager, Xrm, ...rest}) => { * console.log(notifier, Xrm); // to prove that it is there. * } * export default withDynamics(YourComponent) * ``` */ exports.withDynamics = recompose_1.getContext({ notifier: PropTypes.instanceOf(NotificationManager_1.NotificationManager), xrm: PropTypes.object, errorHandler: PropTypes.object }); exports.default = Dynamics; //# sourceMappingURL=Dynamics.js.map