Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Commit

Permalink
add support for maybe structs and maybe lists #236
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Oct 25, 2015
1 parent e674ef2 commit 3e91db6
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 10 deletions.
68 changes: 58 additions & 10 deletions lib/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ var Component = (function (_React$Component) {
};
};

Component.prototype.isValueNully = function isValueNully() {
return Nil.is(this.getTransformer().parse(this.state.value));
};

Component.prototype.removeErrors = function removeErrors() {
this.setState({ hasError: false });
};

Component.prototype.validate = function validate() {
var value = this.getTransformer().parse(this.state.value);
var result = _tcombValidation2['default'].validate(value, this.props.type, this.getValidationOptions());
Expand Down Expand Up @@ -567,12 +575,34 @@ var Struct = (function (_Component6) {
_Component6.apply(this, arguments);
}

Struct.prototype.isValueNully = function isValueNully() {
var _this2 = this;

return Object.keys(this.refs).every(function (ref) {
return _this2.refs[ref].isValueNully();
});
};

Struct.prototype.removeErrors = function removeErrors() {
var _this3 = this;

this.setState({ hasError: false });
Object.keys(this.refs).forEach(function (ref) {
return _this3.refs[ref].removeErrors();
});
};

Struct.prototype.validate = function validate() {
var value = {};
var errors = [];
var hasError = false;
var result = undefined;

if (this.typeInfo.isMaybe && this.isValueNully()) {
this.removeErrors();
return new _tcombValidation2['default'].ValidationResult({ errors: [], value: null });
}

for (var ref in this.refs) {
if (this.refs.hasOwnProperty(ref)) {
result = this.refs[ref].validate();
Expand All @@ -596,12 +626,12 @@ var Struct = (function (_Component6) {
};

Struct.prototype.onChange = function onChange(fieldName, fieldValue, path, kind) {
var _this2 = this;
var _this4 = this;

var value = _tcombValidation2['default'].mixin({}, this.state.value);
value[fieldName] = fieldValue;
this.setState({ value: value }, function () {
_this2.props.onChange(value, path, kind);
_this4.props.onChange(value, path, kind);
});
};

Expand Down Expand Up @@ -734,12 +764,30 @@ var List = (function (_Component7) {
});
};

List.prototype.isValueNully = function isValueNully() {
return this.state.value.length === 0;
};

List.prototype.removeErrors = function removeErrors() {
var _this5 = this;

this.setState({ hasError: false });
Object.keys(this.refs).forEach(function (ref) {
return _this5.refs[ref].removeErrors();
});
};

List.prototype.validate = function validate() {
var value = [];
var errors = [];
var hasError = false;
var result = undefined;

if (this.typeInfo.isMaybe && this.isValueNully()) {
this.removeErrors();
return new _tcombValidation2['default'].ValidationResult({ errors: [], value: null });
}

for (var i = 0, len = this.state.value.length; i < len; i++) {
result = this.refs[i].validate();
errors = errors.concat(result.errors);
Expand All @@ -758,11 +806,11 @@ var List = (function (_Component7) {
};

List.prototype.onChange = function onChange(value, keys, path, kind) {
var _this3 = this;
var _this6 = this;

keys = toSameLength(value, keys, this.props.ctx.uidGenerator);
this.setState({ value: value, keys: keys }, function () {
_this3.props.onChange(value, path, kind);
_this6.props.onChange(value, path, kind);
});
};

Expand Down Expand Up @@ -807,7 +855,7 @@ var List = (function (_Component7) {
};

List.prototype.getItems = function getItems() {
var _this4 = this;
var _this7 = this;

var _props2 = this.props;
var options = _props2.options;
Expand All @@ -823,21 +871,21 @@ var List = (function (_Component7) {
return value.map(function (value, i) {
var buttons = [];
if (!options.disableRemove) {
buttons.push({ label: i18n.remove, click: _this4.removeItem.bind(_this4, i) });
buttons.push({ label: i18n.remove, click: _this7.removeItem.bind(_this7, i) });
}
if (!options.disableOrder) {
buttons.push({ label: i18n.up, click: _this4.moveUpItem.bind(_this4, i) });
buttons.push({ label: i18n.up, click: _this7.moveUpItem.bind(_this7, i) });
}
if (!options.disableOrder) {
buttons.push({ label: i18n.down, click: _this4.moveDownItem.bind(_this4, i) });
buttons.push({ label: i18n.down, click: _this7.moveDownItem.bind(_this7, i) });
}
return {
input: _react2['default'].createElement(Component, {
ref: i,
type: type,
options: options.item || noobj,
value: value,
onChange: _this4.onItemChange.bind(_this4, i),
onChange: _this7.onItemChange.bind(_this7, i),
ctx: {
context: ctx.context,
uidGenerator: ctx.uidGenerator,
Expand All @@ -849,7 +897,7 @@ var List = (function (_Component7) {
path: ctx.path.concat(i)
}
}),
key: _this4.state.keys[i],
key: _this7.state.keys[i],
buttons: buttons
};
});
Expand Down
36 changes: 36 additions & 0 deletions src/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ export class Component extends React.Component {
};
}

isValueNully() {
return Nil.is(this.getTransformer().parse(this.state.value));
}

removeErrors() {
this.setState({hasError: false});
}

validate() {
const value = this.getTransformer().parse(this.state.value);
const result = t.validate(value, this.props.type, this.getValidationOptions());
Expand Down Expand Up @@ -438,12 +446,26 @@ export class Struct extends Component {
parse: value => value
};

isValueNully() {
return Object.keys(this.refs).every((ref) => this.refs[ref].isValueNully());
}

removeErrors() {
this.setState({hasError: false});
Object.keys(this.refs).forEach((ref) => this.refs[ref].removeErrors());
}

validate() {
let value = {};
let errors = [];
let hasError = false;
let result;

if (this.typeInfo.isMaybe && this.isValueNully()) {
this.removeErrors();
return new t.ValidationResult({errors: [], value: null});
}

for (let ref in this.refs) {
if (this.refs.hasOwnProperty(ref)) {
result = this.refs[ref].validate();
Expand Down Expand Up @@ -570,12 +592,26 @@ export class List extends Component {
});
}

isValueNully() {
return this.state.value.length === 0;
}

removeErrors() {
this.setState({hasError: false});
Object.keys(this.refs).forEach((ref) => this.refs[ref].removeErrors());
}

validate() {
const value = [];
let errors = [];
let hasError = false;
let result;

if (this.typeInfo.isMaybe && this.isValueNully()) {
this.removeErrors();
return new t.ValidationResult({errors: [], value: null});
}

for (let i = 0, len = this.state.value.length; i < len; i++ ) {
result = this.refs[i].validate();
errors = errors.concat(result.errors);
Expand Down

0 comments on commit 3e91db6

Please sign in to comment.