Skip to content

Commit

Permalink
update lib before publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
stevehu committed Dec 31, 2018
1 parent ef01752 commit 87e3589
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 79 deletions.
68 changes: 47 additions & 21 deletions lib/Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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; }; }();
Expand Down Expand Up @@ -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"
}
};
};
Expand Down Expand Up @@ -158,7 +167,7 @@ var Array = function (_Component) {
},
_react2.default.createElement(
"div",
null,
{ className: classes.elementsContainer },
forms
),
_react2.default.createElement(
Expand All @@ -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
}
Expand All @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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));
59 changes: 25 additions & 34 deletions lib/ComposedComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}

Expand All @@ -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":
Expand Down Expand Up @@ -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,
Expand All @@ -151,21 +134,29 @@ 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,
valid: true,
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
};
}
}]);
Expand Down
6 changes: 5 additions & 1 deletion lib/FieldSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
7 changes: 5 additions & 2 deletions lib/MultiSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 20 additions & 2 deletions lib/Number.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}));
};

Expand Down
15 changes: 4 additions & 11 deletions lib/SchemaForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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(
Expand Down
7 changes: 5 additions & 2 deletions lib/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion lib/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}]);
Expand Down
Loading

0 comments on commit 87e3589

Please sign in to comment.