UNPKG

@sanity/form-builder

Version:
67 lines (66 loc) 3.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = withDocument; var _propTypes = _interopRequireDefault(require("prop-types")); var _react = _interopRequireDefault(require("react")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } function getDisplayName(component) { return component.displayName || component.name || '<Anonymous>'; } function warnMissingFocusMethod(ComposedComponent) { console.warn("withDocument(".concat(getDisplayName(ComposedComponent), "): The passed component did not expose a \".focus()\" method. Either implement an imperative focus method on the component instance, or forward it's received ref to an element that exposes a .focus() method. The component passed to withDocument was: %O"), ComposedComponent); } function withDocument(ComposedComponent) { var _class; return _class = class WithDocument extends _react.default.PureComponent { constructor(props, context) { super(props); _defineProperty(this, "_input", void 0); _defineProperty(this, "_didShowFocusWarning", false); _defineProperty(this, "state", void 0); _defineProperty(this, "unsubscribe", void 0); _defineProperty(this, "setRef", input => { this._input = input; }); var formBuilder = context.formBuilder; this.state = { document: formBuilder.getDocument() }; this.unsubscribe = formBuilder.onPatch(_ref => { var snapshot = _ref.snapshot; // we will also receive "delete"-patches, with {snapshot: null}. Don't pass null documents. if (snapshot) { this.setState({ document: snapshot }); } }); } componentWillUnmount() { this.unsubscribe(); } focus() { var _this$_input; if (typeof ((_this$_input = this._input) === null || _this$_input === void 0 ? void 0 : _this$_input.focus) === 'function') { this._input.focus(); } else if (!this._didShowFocusWarning) { warnMissingFocusMethod(ComposedComponent); this._didShowFocusWarning = true; } } render() { return /*#__PURE__*/_react.default.createElement(ComposedComponent, _extends({ ref: this.setRef, document: this.state.document }, this.props)); } }, _defineProperty(_class, "displayName", "withDocument(".concat(ComposedComponent.displayName || ComposedComponent.name, ")")), _defineProperty(_class, "contextTypes", { formBuilder: _propTypes.default.any }), _class; }