From 87e358910845b6fb91b12e26cd7a2c2eced13703 Mon Sep 17 00:00:00 2001 From: Steve Hu Date: Mon, 31 Dec 2018 09:22:35 -0500 Subject: [PATCH] update lib before publishing --- lib/Array.js | 68 +++++++++++++++++++++++++------------ lib/ComposedComponent.js | 59 ++++++++++++++------------------ lib/FieldSet.js | 6 +++- lib/MultiSelect.js | 7 ++-- lib/Number.js | 22 ++++++++++-- lib/SchemaForm.js | 15 +++----- lib/Select.js | 7 ++-- lib/Text.js | 3 +- lib/__tests__/utils-test.js | 35 +++++++++++++++++++ lib/index.js | 9 +++++ lib/utils.js | 27 ++++++++++++--- 11 files changed, 179 insertions(+), 79 deletions(-) diff --git a/lib/Array.js b/lib/Array.js index f5d8e5c9..af033521 100644 --- a/lib/Array.js +++ b/lib/Array.js @@ -4,6 +4,8 @@ Object.defineProperty(exports, "__esModule", { value: true }); +var _extends = Object.assign || 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; }; + var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; var _createClass = function () { function 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); @@ -63,14 +65,21 @@ var styles = function styles(theme) { arrayItem: { position: "relative", padding: theme.spacing.unit, - marginBottom: theme.spacing.unit, + marginTop: theme.spacing.unit, display: "flex" }, deleteItemButton: { margin: [[-theme.spacing.unit, -theme.spacing.unit, "auto", "auto"]] }, addButton: { - marginTop: theme.spacing.unit + marginLeft: theme.spacing.unit + }, + elementsContainer: { + display: "flex", + flexWrap: "wrap" + }, + title: { + margin: "auto 0" } }; }; @@ -158,7 +167,7 @@ var Array = function (_Component) { }, _react2.default.createElement( "div", - null, + { className: classes.elementsContainer }, forms ), _react2.default.createElement( @@ -177,37 +186,33 @@ var Array = function (_Component) { } return _react2.default.createElement( "div", - null, + { className: classes.root }, _react2.default.createElement( "div", - null, + { style: { display: "flex" } }, _react2.default.createElement( _FormLabel2.default, - { variant: "h6", required: form.required }, + { + required: form.required, + className: classes.title + }, form.title && getLocalizedString(form.title) ), - _react2.default.createElement( - "div", - null, - arrays - ) + this.getAddButton() ), _react2.default.createElement( - _Button2.default, - { - className: classes.addButton, - variant: "contained", - color: "primary", - onClick: this.onAppend - }, - form.add || "Add" + "div", + null, + arrays ) ); } }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(props, state) { - var propsKey = props.form.key; + var form = props.form; + + var propsKey = form.key; if (props.form && propsKey === state.formKey && props.model && props.model[propsKey] === state.model) { return null; // nothing changed } @@ -223,7 +228,6 @@ var Array = function (_Component) { }(_react.Component); Array.ITEM_ID = "_SCHEMAFORM_ITEM_ID"; -Array.SEQUENCE = 1; Array.setIndex = function (index) { return function (form) { @@ -245,6 +249,8 @@ Array.copyWithIndex = function (form, index) { var _initialiseProps = function _initialiseProps() { var _this3 = this; + this.SEQUENCE = 1; + this.onAppend = function () { var _props3 = _this3.props, form = _props3.form, @@ -306,6 +312,26 @@ var _initialiseProps = function _initialiseProps() { onChangeValidate(model); }; }; + + this.getAddButton = function () { + var _props4 = _this3.props, + form = _props4.form, + classes = _props4.classes; + + + var AddButton = form.AddButton || function (props) { + return _react2.default.createElement(_Button2.default, _extends({ + className: classes.addButton, + variant: "contained", + color: "primary" + }, props)); + }; + return _react2.default.createElement( + AddButton, + { onClick: _this3.onAppend }, + form.add || "Add" + ); + }; }; exports.default = (0, _ComposedComponent2.default)((0, _styles.withStyles)(styles)(Array)); \ No newline at end of file diff --git a/lib/ComposedComponent.js b/lib/ComposedComponent.js index 8f13fbbc..525f5006 100755 --- a/lib/ComposedComponent.js +++ b/lib/ComposedComponent.js @@ -41,6 +41,10 @@ var defaultValue = function defaultValue(props) { return value; }; +var getDisplayName = function getDisplayName(WrappedComponent) { + return WrappedComponent.displayName || WrappedComponent.name || "Component"; +}; + exports.default = function (ComposedComponent) { var defaultProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; return function (_React$Component) { @@ -51,27 +55,10 @@ exports.default = function (ComposedComponent) { var _this = _possibleConstructorReturn(this, (Composed.__proto__ || Object.getPrototypeOf(Composed)).call(this, props)); - var _this$props = _this.props, - errorText = _this$props.errorText, - form = _this$props.form, - showErrors = _this$props.showErrors; + _this.displayName = "ComposedComponent(" + getDisplayName(ComposedComponent) + ")"; _this.onChangeValidate = _this.onChangeValidate.bind(_this); - var value = defaultValue(_this.props); - var validationResult = _utils2.default.validate(form, value); - if (!showErrors) { - _this.state = { - value: value, - valid: true, - error: "" - }; - } else { - _this.state = { - value: value, - valid: !!(validationResult.valid || !value), - error: !validationResult.valid && (value ? validationResult.error.message : null) || errorText - }; - } + _this.state = _this.constructor.getDerivedStateFromProps(_this.props); return _this; } @@ -86,21 +73,17 @@ exports.default = function (ComposedComponent) { value: function onChangeValidate(e, v) { var _props = this.props, form = _props.form, - onChange = _props.onChange; + onChange = _props.onChange, + localization = _props.localization; // eslint-disable-line + var getLocalizedString = localization && localization.getLocalizedString; var value = null; - switch (form.schema.type) { + var type = form.schema ? form.schema.type : form.type; + switch (type) { case "integer": - value = parseInt(e.target.value, 10); - break; case "number": { - var values = e.target.value.split("."); - if (values.length < 2) { - value = parseInt(e.target.value, 10); - } else if (values.length > 1) { - if (values[1].length > 0) value = parseFloat(e.target.value);else value = parseInt(values[0], 10) + "."; - } + value = e; break; } case "boolean": @@ -132,7 +115,7 @@ exports.default = function (ComposedComponent) { } - var validationResult = _utils2.default.validate(form, value); + var validationResult = _utils2.default.validate(form, value, getLocalizedString); this.setState({ value: value, valid: validationResult.valid, @@ -151,10 +134,13 @@ exports.default = function (ComposedComponent) { }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(nextProps) { - var value = defaultValue(nextProps); - var showErrors = nextProps.showErrors; + var errorText = nextProps.errorText, + form = nextProps.form, + showErrors = nextProps.showErrors, + localization = nextProps.localization; - var validationResult = _utils2.default.validate(nextProps.form, value); + var getLocalizedString = localization && localization.getLocalizedString; + var value = defaultValue(nextProps); if (!showErrors) { return { value: value, @@ -162,10 +148,15 @@ exports.default = function (ComposedComponent) { error: "" }; } + + var validationResult = _utils2.default.validate(form, value, getLocalizedString); + + var error = !validationResult.valid ? validationResult.error : undefined; + return { value: value, valid: validationResult.valid, - error: !validationResult.valid ? validationResult.error.message : null + error: (!validationResult.valid ? error.message : null) || errorText }; } }]); diff --git a/lib/FieldSet.js b/lib/FieldSet.js index e7661750..b02f5e61 100644 --- a/lib/FieldSet.js +++ b/lib/FieldSet.js @@ -49,7 +49,11 @@ var FieldSet = function FieldSet(_ref) { return _react2.default.createElement( _FormControl2.default, - { component: "fieldset", className: classes.root }, + { + component: "fieldset", + className: classes.root, + style: form.style + }, _react2.default.createElement( _FormLabel2.default, { component: "legend", required: form.required }, diff --git a/lib/MultiSelect.js b/lib/MultiSelect.js index 583766a2..080a6be3 100644 --- a/lib/MultiSelect.js +++ b/lib/MultiSelect.js @@ -166,9 +166,12 @@ var MultiSelect = function (_Component) { }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(props) { - if (props.model && props.form.key) { + var model = props.model, + form = props.form; + + if (model && form.key) { return { - currentValue: _utils2.default.getValueFromModel(props.model, props.form.key) || [] + currentValue: _utils2.default.getValueFromModel(model, form.key) || [] }; } return null; diff --git a/lib/Number.js b/lib/Number.js index 142f0af8..df94b2fc 100755 --- a/lib/Number.js +++ b/lib/Number.js @@ -31,14 +31,32 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de var NumberComponent = function NumberComponent(props) { var form = props.form, value = props.value, - getLocalizedNumber = props.localization.getLocalizedNumber; + getLocalizedNumber = props.localization.getLocalizedNumber, + onChangeValidate = props.onChangeValidate; var inputValue = value || value === 0 ? value : ""; if (form.useLocalizer) inputValue = getLocalizedNumber(inputValue); + var onChange = function onChange(e) { + var type = form.schema ? form.schema.type : form.type; + var enteredValue = null; + if (type === "integer") { + enteredValue = parseInt(e.target.value, 10); + } else if (type === "number") { + var values = e.target.value.split("."); + if (values.length < 2) { + enteredValue = parseInt(e.target.value, 10); + } else if (values.length > 1) { + if (values[1].length > 0) enteredValue = parseFloat(e.target.value);else enteredValue = parseInt(values[0], 10) + "."; + } + } + onChangeValidate(enteredValue); + }; + return _react2.default.createElement(_Text2.default, _extends({}, props, { form: Object.assign({}, form, { type: "string" }), - value: inputValue + value: inputValue, + otherProps: { onChange: onChange } })); }; diff --git a/lib/SchemaForm.js b/lib/SchemaForm.js index 0124c729..7c175b75 100644 --- a/lib/SchemaForm.js +++ b/lib/SchemaForm.js @@ -143,22 +143,14 @@ var SchemaForm = function (_Component) { }; }; - _this.onChange = _this.onChange.bind(_this); _this.builder = _this.builder.bind(_this); return _this; } - _createClass(SchemaForm, [{ - key: "onChange", - value: function onChange(key, val) { - var onModelChange = this.props.onModelChange; - - onModelChange(key, val); - } + // Assign default values and save it to the model - // Assign default values and save it to the model - }, { + _createClass(SchemaForm, [{ key: "builder", value: function builder(form, model, index, mapper, onChange, _builder) { var _props = this.props, @@ -204,6 +196,7 @@ var SchemaForm = function (_Component) { option = _props2.option, model = _props2.model, className = _props2.className, + onModelChange = _props2.onModelChange, mapper = _props2.mapper; var merged = _utils2.default.merge(schema, form, ignore, option); @@ -213,7 +206,7 @@ var SchemaForm = function (_Component) { mergedMapper = (0, _merge2.default)(this.mapper, mapper); } var forms = merged.map(function (formPart, index) { - return _this2.builder(formPart, model, index, mergedMapper, _this2.onChange, _this2.builder); + return _this2.builder(formPart, model, index, mergedMapper, onModelChange, _this2.builder); }); return _react2.default.createElement( diff --git a/lib/Select.js b/lib/Select.js index 99e43aa0..e4f1e3cd 100644 --- a/lib/Select.js +++ b/lib/Select.js @@ -109,9 +109,12 @@ var Select = function (_Component) { }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(props) { - if (props.model && props.form.key) { + var form = props.form, + model = props.model; + + if (model && form.key) { return { - currentValue: _utils2.default.getValueFromModel(props.model, props.form.key) + currentValue: _utils2.default.getValueFromModel(model, form.key) }; } return null; diff --git a/lib/Text.js b/lib/Text.js index 02562607..9b22bb0e 100755 --- a/lib/Text.js +++ b/lib/Text.js @@ -72,7 +72,8 @@ var Text = function (_React$Component) { value: value || "", disabled: form.readonly, fullWidth: true, - required: form.required + required: form.required, + style: form.style }, otherProps)); } }]); diff --git a/lib/__tests__/utils-test.js b/lib/__tests__/utils-test.js index cdd85fe4..a975f4ba 100644 --- a/lib/__tests__/utils-test.js +++ b/lib/__tests__/utils-test.js @@ -469,4 +469,39 @@ describe("utils", function () { // array items expect(merged[0].items[0].items[0].readonly).toEqual(true); }); + + it("selectOrSet test", function () { + var result = {}; + _utils2.default.selectOrSet(["test", "tt"], result, "HI"); + expect(result.test.tt).toBe("HI"); + expect(_utils2.default.selectOrSet("test.tt", result)).toBe("HI"); + expect(_utils2.default.selectOrSet(["test", "tt"], result)).toBe("HI"); + + _utils2.default.selectOrSet("my.test.fn", result, "Value"); + expect(result.my.test.fn).toBe("Value"); + expect(_utils2.default.selectOrSet("my.test.fn", result)).toBe("Value"); + expect(_utils2.default.selectOrSet(["my", "test", "fn"], result)).toBe("Value"); + + _utils2.default.selectOrSet("number.is", result, 1326); + expect(result.number.is).toBe(1326); + expect(_utils2.default.selectOrSet("number.is", result)).toBe(1326); + expect(_utils2.default.selectOrSet(["number", "is"], result)).toBe(1326); + + _utils2.default.selectOrSet("number.is", result, 1272); + expect(result.number.is).toBe(1272); + expect(_utils2.default.selectOrSet("number.is", result)).toBe(1272); + expect(_utils2.default.selectOrSet(["number", "is"], result)).toBe(1272); + }); + + it("removeEmpty test", function () { + var result = { + test: undefined, + value: null, + str: "", + nonEmptyStr: "test", + obj: { test: undefined, str: "", value: null, obj: {} } + }; + _utils2.default.removeEmpty(result); + expect(result).toMatchObject({ obj: { obj: {} }, nonEmptyStr: "test" }); + }); }); \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index df38718f..d784a937 100644 --- a/lib/index.js +++ b/lib/index.js @@ -31,4 +31,13 @@ Object.defineProperty(exports, "utils", { } }); +var _Array = require("./Array"); + +Object.defineProperty(exports, "Array", { + enumerable: true, + get: function get() { + return _interopRequireDefault(_Array).default; + } +}); + function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } \ No newline at end of file diff --git a/lib/utils.js b/lib/utils.js index 7d2678ef..52db1711 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -4,6 +4,11 @@ Object.defineProperty(exports, "__esModule", { value: true }); +var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; + +var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); /* eslint-disable no-param-reassign */ + + var _isObject = require("lodash/isObject"); var _isObject2 = _interopRequireDefault(_isObject); @@ -42,8 +47,7 @@ var safeEval = function safeEval(condition, scope) { } catch (error) { return undefined; } -}; /* eslint-disable no-param-reassign */ - +}; var stripNullType = function stripNullType(type) { if (Array.isArray(type) && type.length === 2) { @@ -202,6 +206,18 @@ var select = function select(name, schema, options) { return undefined; }; +var removeEmpty = function removeEmpty(obj) { + return Object.entries(obj).forEach(function (_ref) { + var _ref2 = _slicedToArray(_ref, 2), + key = _ref2[0], + val = _ref2[1]; + + if (val && (typeof val === "undefined" ? "undefined" : _typeof(val)) === "object") removeEmpty(val); + // eslint-disable-next-line no-param-reassign + else if (!val || val === null || val === "") delete obj[key]; + }); +}; + var checkboxes = function checkboxes(name, schema, options) { if (stripNullType(schema.type) === "array" && schema.items && schema.items.enum) { var f = stdFormObj(name, schema, options); @@ -571,7 +587,7 @@ var validateBySchema = function validateBySchema(schema, value) { return _tv2.default.validateResult(value, schema); }; -var validate = function validate(form, value) { +var validate = function validate(form, value, getLocalizedString) { if (!form) { return { valid: true }; } @@ -615,7 +631,7 @@ var validate = function validate(form, value) { var tv4Result = _tv2.default.validateResult(valueWrap, wrap); if (tv4Result != null && !tv4Result.valid && form.validationMessage != null && typeof value !== "undefined") { - tv4Result.error.message = form.validationMessage; + tv4Result.error.message = getLocalizedString ? getLocalizedString(form.validationMessage) : form.validationMessage; } return tv4Result; }; @@ -670,5 +686,6 @@ exports.default = { safeEval: safeEval, selectOrSet: selectOrSet, getValueFromModel: getValueFromModel, - getTitleByValue: getTitleByValue + getTitleByValue: getTitleByValue, + removeEmpty: removeEmpty }; \ No newline at end of file