UNPKG

7.85 kBJavaScriptView Raw
1"use strict";
2
3function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports["default"] = void 0;
9
10var _react = _interopRequireWildcard(require("react"));
11
12var _propTypes = _interopRequireDefault(require("prop-types"));
13
14var _fastDeepEqual = _interopRequireDefault(require("fast-deep-equal"));
15
16var _reactLifecyclesCompat = require("react-lifecycles-compat");
17
18var _components = require("@storybook/components");
19
20function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
21
22function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
23
24function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
25
26function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
27
28function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
29
30function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
31
32function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
33
34function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
35
36function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
37
38function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
39
40function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
41
42function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
43
44function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
45
46function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
47
48var ObjectType = /*#__PURE__*/function (_Component) {
49 _inherits(ObjectType, _Component);
50
51 var _super = _createSuper(ObjectType);
52
53 function ObjectType() {
54 var _this;
55
56 _classCallCheck(this, ObjectType);
57
58 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
59 args[_key] = arguments[_key];
60 }
61
62 _this = _super.call.apply(_super, [this].concat(args));
63
64 _defineProperty(_assertThisInitialized(_this), "state", {
65 value: '',
66 failed: false,
67 json: {}
68 });
69
70 _defineProperty(_assertThisInitialized(_this), "handleChange", function (e) {
71 var value = e.target.value;
72 var stateJson = _this.state.json;
73 var _this$props = _this.props,
74 knob = _this$props.knob,
75 onChange = _this$props.onChange;
76
77 try {
78 var json = JSON.parse(value.trim());
79
80 _this.setState({
81 value: value,
82 json: json,
83 failed: false
84 });
85
86 if ((0, _fastDeepEqual["default"])(knob.value, stateJson)) {
87 onChange(json);
88 }
89 } catch (err) {
90 _this.setState({
91 value: value,
92 failed: true
93 });
94 }
95 });
96
97 return _this;
98 }
99
100 _createClass(ObjectType, [{
101 key: "render",
102 value: function render() {
103 var _this$state = this.state,
104 value = _this$state.value,
105 failed = _this$state.failed;
106 var knob = this.props.knob;
107 return /*#__PURE__*/_react["default"].createElement(_components.Form.Textarea, {
108 name: knob.name,
109 valid: failed ? 'error' : undefined,
110 value: value,
111 onChange: this.handleChange,
112 size: "flex"
113 });
114 }
115 }], [{
116 key: "getDerivedStateFromProps",
117 value: function getDerivedStateFromProps(props, state) {
118 if (!(0, _fastDeepEqual["default"])(props.knob.value, state.json)) {
119 try {
120 return {
121 value: JSON.stringify(props.knob.value, null, 2),
122 failed: false,
123 json: props.knob.value
124 };
125 } catch (e) {
126 return {
127 value: 'Object cannot be stringified',
128 failed: true
129 };
130 }
131 }
132
133 return null;
134 }
135 }]);
136
137 return ObjectType;
138}(_react.Component);
139
140_defineProperty(ObjectType, "propTypes", {
141 knob: _propTypes["default"].shape({
142 name: _propTypes["default"].string,
143 value: _propTypes["default"].oneOfType([_propTypes["default"].object, _propTypes["default"].array])
144 }).isRequired,
145 onChange: _propTypes["default"].func.isRequired
146});
147
148_defineProperty(ObjectType, "defaultProps", {
149 knob: {},
150 onChange: function onChange(value) {
151 return value;
152 }
153});
154
155_defineProperty(ObjectType, "serialize", function (object) {
156 return JSON.stringify(object);
157});
158
159_defineProperty(ObjectType, "deserialize", function (value) {
160 return value ? JSON.parse(value) : {};
161});
162
163(0, _reactLifecyclesCompat.polyfill)(ObjectType);
164var _default = ObjectType;
165exports["default"] = _default;
\No newline at end of file