From e70fd0deaf5e5dd1eb5e98ec491541180a3a114c Mon Sep 17 00:00:00 2001 From: Steve Hu Date: Tue, 17 Jul 2018 21:10:21 -0400 Subject: [PATCH] update after running prepublish --- lib/Checkbox.js | 40 ++++++++++++++++++++++++++++----- lib/ComposedComponent.js | 2 +- lib/Number.js | 2 +- lib/SchemaForm.js | 48 +++++++++++++++++++++++++++++++--------- lib/Select.js | 19 ++++++++++++++-- lib/Text.js | 18 +++++++++++---- lib/TextArea.js | 2 +- lib/TextSuggest.js | 10 +++++---- lib/utils.js | 4 ++-- 9 files changed, 115 insertions(+), 30 deletions(-) diff --git a/lib/Checkbox.js b/lib/Checkbox.js index 4e98939b..22ddd6a6 100644 --- a/lib/Checkbox.js +++ b/lib/Checkbox.js @@ -29,13 +29,36 @@ function _inherits(subClass, superClass) { if (typeof superClass !== "function" */ +var styles = { + error: { + color: "rgb(244, 67, 54)", + fontSize: "12px", + lineHeight: "12px" + }, + checkbox: { + marginTop: "14px", + marginBottom: "0px", + height: "72px" + } +}; + var Checkbox2 = function (_React$Component) { _inherits(Checkbox2, _React$Component); - function Checkbox2() { + function Checkbox2(props) { _classCallCheck(this, Checkbox2); - return _possibleConstructorReturn(this, (Checkbox2.__proto__ || Object.getPrototypeOf(Checkbox2)).apply(this, arguments)); + var _this = _possibleConstructorReturn(this, (Checkbox2.__proto__ || Object.getPrototypeOf(Checkbox2)).call(this, props)); + + var _this$props = _this.props, + model = _this$props.model, + form = _this$props.form; + var key = form.key; + //If a boolean is stored, use it; if not, if a boolean is defined as schema's default, use it. + + var value = _.isBoolean(_this.props.value) ? _this.props.value : _.isBoolean(form.schema.default) ? form.schema.default : undefined; + _this.props.setDefault(key, model, form, value); + return _this; } _createClass(Checkbox2, [{ @@ -45,17 +68,22 @@ var Checkbox2 = function (_React$Component) { return _react2.default.createElement( 'div', - { className: this.props.form.className }, + { className: this.props.form.className, style: styles.checkbox }, _react2.default.createElement(_Checkbox2.default, { name: this.props.form.key.slice(-1)[0], value: this.props.form.key.slice(-1)[0], - checked: this.props.value || false, + checked: this.props.value, label: this.props.form.title, disabled: this.props.form.readonly, onCheck: function onCheck(e, checked) { _this2.props.onChangeValidate(e); } - }) + }), + _react2.default.createElement( + 'span', + { style: styles.error }, + this.props.errorText + ) ); } }]); @@ -73,6 +101,8 @@ var _temp = function () { return; } + __REACT_HOT_LOADER__.register(styles, 'styles', 'src/Checkbox.js'); + __REACT_HOT_LOADER__.register(Checkbox2, 'Checkbox2', 'src/Checkbox.js'); __REACT_HOT_LOADER__.register(_default, 'default', 'src/Checkbox.js'); diff --git a/lib/ComposedComponent.js b/lib/ComposedComponent.js index a9f8eafd..7deb72a1 100755 --- a/lib/ComposedComponent.js +++ b/lib/ComposedComponent.js @@ -38,7 +38,7 @@ var _default = function _default(ComposedComponent) { _this.state = { value: value, valid: !!(validationResult.valid || !value), - error: !validationResult.valid && value ? validationResult.error.message : null + error: !validationResult.valid && (value ? validationResult.error.message : null) || _this.props.errorText }; return _this; } diff --git a/lib/Number.js b/lib/Number.js index 84ed989f..3f37e944 100755 --- a/lib/Number.js +++ b/lib/Number.js @@ -98,7 +98,7 @@ var Number = function (_React$Component) { type: this.props.form.type, floatingLabelText: this.props.form.title, hintText: this.props.form.placeholder, - errorText: this.props.error, + errorText: this.props.error || this.props.errorText, onChange: this.preValidationCheck, value: this.state.lastSuccessfulValue, ref: 'numberField', diff --git a/lib/SchemaForm.js b/lib/SchemaForm.js index cd71e911..679bc364 100644 --- a/lib/SchemaForm.js +++ b/lib/SchemaForm.js @@ -92,44 +92,72 @@ var SchemaForm = function (_React$Component) { 'fieldset': _FieldSet2.default }; - _this.onChange = _this.onChange.bind(_this); + _this.setDefault = function () { + return _this.__setDefault__REACT_HOT_LOADER__.apply(_this, arguments); + }; + return _this; } + // Assign default values and save it to the model + + _createClass(SchemaForm, [{ - key: 'onChange', - value: function onChange(key, val, type) { - //console.log('SchemaForm.onChange', key, val); - this.props.onModelChange(key, val, type); + key: '__setDefault__REACT_HOT_LOADER__', + value: function __setDefault__REACT_HOT_LOADER__() { + return this.__setDefault__REACT_HOT_LOADER__.apply(this, arguments); + } + }, { + key: '__setDefault__REACT_HOT_LOADER__', + value: function __setDefault__REACT_HOT_LOADER__(key, model, form, value) { + var currentValue = _utils2.default.selectOrSet(key, model); + + // If current value is not setted and exist a default, apply the default over the model + if (_lodash2.default.isNil(currentValue) && !_lodash2.default.isNil(value)) this.props.onModelChange(key, value, form.type, form); } }, { key: 'builder', - value: function builder(form, model, index, onChange, mapper) { - var type = form.type; - var Field = this.mapper[type]; + value: function builder(form, model, index, mapper) { + var Field = this.mapper[form.type]; if (!Field) { console.log('Invalid field: \"' + form.key[0] + '\"!'); return null; } + // Apply conditionals to review if this field must be rendered if (form.condition && _utils2.default.safeEval(form.condition, { model: model }) === false) { return null; } var key = form.key && form.key.join(".") || index; - return _react2.default.createElement(Field, { model: model, form: form, key: key, onChange: onChange, mapper: mapper, builder: this.builder }); + + var errors = this.props.errors; + + var error = key in errors ? errors[key] : null; + + return _react2.default.createElement(Field, { + model: model, + form: form, + key: key, + onChange: this.props.onModelChange, + setDefault: this.setDefault, + mapper: mapper, + builder: this.builder, + errorText: error + }); } }, { key: 'render', value: function render() { var merged = _utils2.default.merge(this.props.schema, this.props.form, this.props.ignore, this.props.option); + //console.log('SchemaForm merged = ', JSON.stringify(merged, undefined, 2)); var mapper = this.mapper; if (this.props.mapper) { mapper = _lodash2.default.merge(this.mapper, this.props.mapper); } var forms = merged.map(function (form, index) { - return this.builder(form, this.props.model, index, this.onChange, mapper); + return this.builder(form, this.props.model, index, mapper); }.bind(this)); return _react2.default.createElement( diff --git a/lib/Select.js b/lib/Select.js index 3ced087d..7eeaf3b2 100644 --- a/lib/Select.js +++ b/lib/Select.js @@ -22,6 +22,10 @@ var _SelectField = require('material-ui/SelectField'); var _SelectField2 = _interopRequireDefault(_SelectField); +var _lodash = require('lodash'); + +var _lodash2 = _interopRequireDefault(_lodash); + function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } @@ -42,9 +46,20 @@ var Select = function (_React$Component) { var _this = _possibleConstructorReturn(this, (Select.__proto__ || Object.getPrototypeOf(Select)).call(this, props)); _this.onSelected = _this.onSelected.bind(_this); - var possibleValue = _this.getModelKey(_this.props.model, _this.props.form.key); + + var _this$props = _this.props, + model = _this$props.model, + form = _this$props.form; + var key = form.key; + + + var storedValue = model && _this.getModelKey(model, key) || false; + var defaultValue = form.schema.default || false; + var value = !_lodash2.default.isEmpty(storedValue) && storedValue || defaultValue; + + _this.props.setDefault(key, model, form, value); _this.state = { - currentValue: _this.props.model !== undefined && possibleValue ? possibleValue : _this.props.form.titleMap != null ? _this.props.form.titleMap[0].value : '' + currentValue: value }; return _this; } diff --git a/lib/Text.js b/lib/Text.js index 58b9b3f0..a3f7bba3 100755 --- a/lib/Text.js +++ b/lib/Text.js @@ -32,16 +32,26 @@ function _inherits(subClass, superClass) { if (typeof superClass !== "function" var Text = function (_React$Component) { _inherits(Text, _React$Component); - function Text() { + function Text(props) { _classCallCheck(this, Text); - return _possibleConstructorReturn(this, (Text.__proto__ || Object.getPrototypeOf(Text)).apply(this, arguments)); + var _this = _possibleConstructorReturn(this, (Text.__proto__ || Object.getPrototypeOf(Text)).call(this, props)); + + var _this$props = _this.props, + model = _this$props.model, + form = _this$props.form, + value = _this$props.value; + var key = form.key; + + + _this.props.setDefault(key, model, form, value); + return _this; } _createClass(Text, [{ key: 'render', value: function render() { - //console.log('Text props', this.props.form.readonly); + //console.log('Text props', this.props); return _react2.default.createElement( 'div', { className: this.props.form.htmlClass }, @@ -49,7 +59,7 @@ var Text = function (_React$Component) { type: this.props.form.type, floatingLabelText: this.props.form.title, hintText: this.props.form.placeholder, - errorText: this.props.error, + errorText: this.props.error || this.props.errorText, onChange: this.props.onChangeValidate, defaultValue: this.props.value, disabled: this.props.form.readonly, diff --git a/lib/TextArea.js b/lib/TextArea.js index 7e9bce80..f3ab66c5 100644 --- a/lib/TextArea.js +++ b/lib/TextArea.js @@ -51,7 +51,7 @@ var TextArea = function (_React$Component) { floatingLabelText: this.props.form.title, hintText: this.props.form.placeholder, onChange: this.props.onChangeValidate, - errorText: this.props.error, + errorText: this.props.error || this.props.errorText, defaultValue: this.props.value, multiLine: true, rows: this.props.form.rows, diff --git a/lib/TextSuggest.js b/lib/TextSuggest.js index 7755bf10..ead29719 100644 --- a/lib/TextSuggest.js +++ b/lib/TextSuggest.js @@ -75,7 +75,7 @@ var TextSuggest = function (_React$Component) { var key = this.props.form.key; var type = this.props.form.schema.type; - return this.props.onChange(key, newValue[dataSourceConfig['value']], type); + return this.props.onChange(key, newValue[dataSourceConfig['value']], type, this.props.form); } }, { key: '__handleInit__REACT_HOT_LOADER__', @@ -85,9 +85,9 @@ var TextSuggest = function (_React$Component) { var names = this.props.form.schema.enumNames || this.props.form.schema.enum; var values = this.props.form.schema.enum; - console.log(names, values); - console.log("indexOf", values.indexOf(init_value)); - console.log("names[values.indexOf(init_value)]", names[values.indexOf(init_value)]); + // console.log(names, values); + // console.log("indexOf", values.indexOf(init_value)); + // console.log("names[values.indexOf(init_value)]", names[values.indexOf(init_value)]); var init_value_name = names[values.indexOf(init_value)]; // this.handleUpdate({[dataSourceConfig['value']]: init_value, [dataSourceConfig['text']]: init_value_name}) @@ -110,6 +110,8 @@ var TextSuggest = function (_React$Component) { } }(this.props.form.filter); + // console.log("TEXTSUG", this.props); + var value = this.props.value && this.handleInit(this.props.value); return _react2.default.createElement( diff --git a/lib/utils.js b/lib/utils.js index 5117195b..f92fb6aa 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -431,9 +431,9 @@ function merge(schema, form, ignore, options, readonly) { } } - //If it has a titleMap make sure it's a list + //If it has a titleMap make sure to update it with latest enum and names if (obj.titleMap) { - obj.titleMap = canonicalTitleMap(obj.titleMap); + obj.titleMap = canonicalTitleMap(obj.schema.enumNames, obj.schema.enum); } //