Skip to content

Commit b1ca0d6

Browse files
authored
Merge v2.0 branch (#135)
1 parent 1c5cc12 commit b1ca0d6

24 files changed

+103
-351
lines changed

lib/Wrapper.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -232,23 +232,23 @@ exports.default = function (Component) {
232232
value: function render() {
233233
var innerRef = this.props.innerRef;
234234

235-
var propsForElement = _extends({
236-
getErrorMessage: this.getErrorMessage,
237-
getErrorMessages: this.getErrorMessages,
238-
getValue: this.getValue,
239-
hasValue: this.hasValue,
240-
isFormDisabled: this.isFormDisabled,
241-
isValid: this.isValid,
242-
isPristine: this.isPristine,
243-
isFormSubmitted: this.isFormSubmitted,
244-
isRequired: this.isRequired,
235+
var propsForElement = _extends({}, this.props, {
236+
errorMessage: this.getErrorMessage(),
237+
errorMessages: this.getErrorMessages(),
238+
hasValue: this.hasValue(),
239+
isFormDisabled: this.isFormDisabled(),
240+
isFormSubmitted: this.isFormSubmitted(),
241+
isPristine: this.isPristine(),
242+
isRequired: this.isRequired(),
243+
isValid: this.isValid(),
245244
isValidValue: this.isValidValue,
246245
resetValue: this.resetValue,
247246
setValidations: this.setValidations,
248247
setValue: this.setValue,
249-
showRequired: this.showRequired,
250-
showError: this.showError
251-
}, this.props);
248+
showError: this.showError(),
249+
showRequired: this.showRequired(),
250+
value: this.getValue()
251+
});
252252

253253
if (innerRef) {
254254
propsForElement.ref = innerRef;

lib/index.js

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
6-
exports.Wrapper = exports.withFormsy = exports.validationRules = exports.propTypes = exports.addValidationRule = undefined;
6+
exports.withFormsy = exports.validationRules = exports.propTypes = exports.addValidationRule = undefined;
77

88
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; };
99

@@ -300,7 +300,7 @@ var Formsy = function (_React$Component) {
300300
_this.validate = function (component) {
301301
// Trigger onChange
302302
if (_this.state.canChange) {
303-
_this.props.onChange(_this.getCurrentValues(), _this.isChanged());
303+
_this.props.onChange(_this.getModel(), _this.isChanged());
304304
}
305305

306306
var validation = _this.runValidation(component);
@@ -516,23 +516,8 @@ var addValidationRule = function addValidationRule(name, func) {
516516
_validationRules2.default[name] = func;
517517
};
518518

519-
var withFormsy = _Wrapper2.default;
520-
521-
var didWarnAboutWrapperDeprecation = false;
522-
523-
var deprecatedWrapper = function deprecatedWrapper(Component) {
524-
if (!didWarnAboutWrapperDeprecation) {
525-
// eslint-disable-next-line no-console
526-
console.warn('Wrapper has been renamed to withFormsy. Importing Wrapper from formsy-react is depreacted and will be removed in the future. Please rename your Wrapper imports to withFormsy.');
527-
didWarnAboutWrapperDeprecation = true;
528-
}
529-
530-
return withFormsy(Component);
531-
};
532-
533519
exports.addValidationRule = addValidationRule;
534520
exports.propTypes = _Wrapper.propTypes;
535521
exports.validationRules = _validationRules2.default;
536-
exports.withFormsy = withFormsy;
537-
exports.Wrapper = deprecatedWrapper;
522+
exports.withFormsy = _Wrapper2.default;
538523
exports.default = Formsy;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "formsy-react",
3-
"version": "1.1.5",
3+
"version": "2.0.0-beta.0",
44
"description": "A form input builder and validator for React",
55
"keywords": [
66
"form",

src/Wrapper.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,22 +175,22 @@ export default (Component) => {
175175
render() {
176176
const { innerRef } = this.props;
177177
const propsForElement = {
178-
getErrorMessage: this.getErrorMessage,
179-
getErrorMessages: this.getErrorMessages,
180-
getValue: this.getValue,
181-
hasValue: this.hasValue,
182-
isFormDisabled: this.isFormDisabled,
183-
isValid: this.isValid,
184-
isPristine: this.isPristine,
185-
isFormSubmitted: this.isFormSubmitted,
186-
isRequired: this.isRequired,
178+
...this.props,
179+
errorMessage: this.getErrorMessage(),
180+
errorMessages: this.getErrorMessages(),
181+
hasValue: this.hasValue(),
182+
isFormDisabled: this.isFormDisabled(),
183+
isFormSubmitted: this.isFormSubmitted(),
184+
isPristine: this.isPristine(),
185+
isRequired: this.isRequired(),
186+
isValid: this.isValid(),
187187
isValidValue: this.isValidValue,
188188
resetValue: this.resetValue,
189189
setValidations: this.setValidations,
190190
setValue: this.setValue,
191-
showRequired: this.showRequired,
192-
showError: this.showError,
193-
...this.props,
191+
showError: this.showError(),
192+
showRequired: this.showRequired(),
193+
value: this.getValue(),
194194
};
195195

196196
if (innerRef) {

src/index.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ class Formsy extends React.Component {
272272
validate = (component) => {
273273
// Trigger onChange
274274
if (this.state.canChange) {
275-
this.props.onChange(this.getCurrentValues(), this.isChanged());
275+
this.props.onChange(this.getModel(), this.isChanged());
276276
}
277277

278278
const validation = this.runValidation(component);
@@ -451,26 +451,11 @@ const addValidationRule = (name, func) => {
451451
validationRules[name] = func;
452452
};
453453

454-
const withFormsy = Wrapper;
455-
456-
let didWarnAboutWrapperDeprecation = false;
457-
458-
const deprecatedWrapper = (Component) => {
459-
if (!didWarnAboutWrapperDeprecation) {
460-
// eslint-disable-next-line no-console
461-
console.warn('Wrapper has been renamed to withFormsy. Importing Wrapper from formsy-react is depreacted and will be removed in the future. Please rename your Wrapper imports to withFormsy.');
462-
didWarnAboutWrapperDeprecation = true;
463-
}
464-
465-
return withFormsy(Component);
466-
};
467-
468454
export {
469455
addValidationRule,
470456
propTypes,
471457
validationRules,
472-
withFormsy,
473-
deprecatedWrapper as Wrapper,
458+
Wrapper as withFormsy,
474459
};
475460

476461
export default Formsy;

tests/Element-spec.js

Lines changed: 21 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import TestUtils from 'react-dom/test-utils';
33
import PureRenderMixin from 'react-addons-pure-render-mixin';
44
import sinon from 'sinon';
55

6-
import Formsy, { withFormsy, Wrapper } from './..';
6+
import Formsy, { withFormsy } from './..';
77
import TestInput, { InputFactory } from './utils/TestInput';
88
import immediate from './utils/immediate';
99

1010
export default {
1111

12-
'should return passed and setValue() value when using getValue()': function (test) {
12+
'should pass down correct value prop after using setValue()': function (test) {
1313

1414
const form = TestUtils.renderIntoDocument(
1515
<Formsy>
@@ -33,34 +33,7 @@ export default {
3333
this.props.setValue(event.target.value, false);
3434
}
3535
render() {
36-
return <input type="text" value={this.props.getValue()} onChange={this.updateValue}/>;
37-
}
38-
})
39-
const form = TestUtils.renderIntoDocument(
40-
<Formsy>
41-
<Input name="foo" value="foo" innerRef="comp" />
42-
</Formsy>
43-
);
44-
const inputComponent = TestUtils.findRenderedComponentWithType(form, Input);
45-
const setStateSpy = sinon.spy(inputComponent, 'setState');
46-
const inputElement = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
47-
48-
test.equal(setStateSpy.called, false);
49-
TestUtils.Simulate.change(inputElement, {target: {value: 'foobar'}});
50-
test.equal(setStateSpy.calledOnce, true);
51-
test.equal(setStateSpy.calledWithExactly({ value: 'foobar' }), true);
52-
test.done();
53-
54-
},
55-
56-
'Wrapper: should only set the value and not validate when calling setValue(val, false)': function (test) {
57-
58-
const Input = Wrapper(class TestInput extends React.Component {
59-
updateValue = (event) => {
60-
this.props.setValue(event.target.value, false);
61-
}
62-
render() {
63-
return <input type="text" value={this.props.getValue()} onChange={this.updateValue}/>;
36+
return <input type="text" value={this.props.value} onChange={this.updateValue}/>;
6437
}
6538
})
6639
const form = TestUtils.renderIntoDocument(
@@ -105,10 +78,10 @@ export default {
10578

10679
'should return error message passed when calling getErrorMessage()': function (test) {
10780

108-
let getErrorMessage = null;
81+
let errorMessage = null;
10982
const Input = InputFactory({
11083
componentDidMount: function() {
111-
getErrorMessage = this.props.getErrorMessage;
84+
errorMessage = this.props.errorMessage;
11285
}
11386
});
11487
TestUtils.renderIntoDocument(
@@ -117,7 +90,7 @@ export default {
11790
</Formsy>
11891
);
11992

120-
test.equal(getErrorMessage(), 'Has to be email');
93+
test.equal(errorMessage, 'Has to be email');
12194

12295
test.done();
12396

@@ -127,8 +100,8 @@ export default {
127100

128101
let isValid = null;
129102
const Input = InputFactory({
130-
componentDidMount: function() {
131-
isValid = this.props.isValid;
103+
componentWillReceiveProps: function(nextProps) {
104+
isValid = nextProps.isValid;
132105
}
133106
});
134107
const form = TestUtils.renderIntoDocument(
@@ -137,10 +110,10 @@ export default {
137110
</Formsy>
138111
);
139112

140-
test.equal(isValid(), false);
113+
test.equal(isValid, false);
141114
const input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
142115
TestUtils.Simulate.change(input, {target: {value: '[email protected]'}});
143-
test.equal(isValid(), true);
116+
test.equal(isValid, true);
144117

145118
test.done();
146119

@@ -162,9 +135,9 @@ export default {
162135
</Formsy>
163136
);
164137

165-
test.equal(isRequireds[0](), false);
166-
test.equal(isRequireds[1](), true);
167-
test.equal(isRequireds[2](), true);
138+
test.equal(isRequireds[0], false);
139+
test.equal(isRequireds[1], true);
140+
test.equal(isRequireds[2], true);
168141

169142
test.done();
170143

@@ -186,9 +159,9 @@ export default {
186159
</Formsy>
187160
);
188161

189-
test.equal(showRequireds[0](), false);
190-
test.equal(showRequireds[1](), true);
191-
test.equal(showRequireds[2](), false);
162+
test.equal(showRequireds[0], false);
163+
test.equal(showRequireds[1], true);
164+
test.equal(showRequireds[2], false);
192165

193166
test.done();
194167

@@ -198,8 +171,8 @@ export default {
198171

199172
let isPristine = null;
200173
const Input = InputFactory({
201-
componentDidMount: function() {
202-
isPristine = this.props.isPristine;
174+
componentWillReceiveProps: function(nextProps) {
175+
isPristine = nextProps.isPristine;
203176
}
204177
});
205178
const form = TestUtils.renderIntoDocument(
@@ -208,10 +181,10 @@ export default {
208181
</Formsy>
209182
);
210183

211-
test.equal(isPristine(), true);
184+
test.equal(isPristine, true);
212185
const input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
213186
TestUtils.Simulate.change(input, {target: {value: 'foo'}});
214-
test.equal(isPristine(), false);
187+
test.equal(isPristine, false);
215188

216189
test.done();
217190

@@ -618,7 +591,7 @@ export default {
618591
shouldComponentUpdate: function() { return false },
619592
render: function() {
620593
renderSpy();
621-
return <input type={this.props.type} value={this.props.getValue()} onChange={this.updateValue}/>;
594+
return <input type={this.props.type} value={this.props.value} onChange={this.updateValue}/>;
622595
}
623596
});
624597

@@ -637,21 +610,10 @@ export default {
637610
'binds all necessary methods': function (test) {
638611
const onInputRef = input => {
639612
[
640-
'getErrorMessage',
641-
'getErrorMessages',
642-
'getValue',
643-
'hasValue',
644-
'isFormDisabled',
645-
'isValid',
646-
'isPristine',
647-
'isFormSubmitted',
648-
'isRequired',
649613
'isValidValue',
650614
'resetValue',
651615
'setValidations',
652616
'setValue',
653-
'showRequired',
654-
'showError',
655617
].forEach(fnName => {
656618
const fn = input[fnName];
657619
try {

tests/Formsy-spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,40 @@ export default {
415415

416416
},
417417

418+
'onChange should honor dot notation transformations': function (test) {
419+
420+
const hasChanged = sinon.spy();
421+
class TestForm extends React.Component {
422+
state = {
423+
showInput: false
424+
}
425+
addInput() {
426+
this.setState({
427+
showInput: true
428+
})
429+
}
430+
render() {
431+
return (
432+
<Formsy onChange={hasChanged}>
433+
{
434+
this.state.showInput ?
435+
<TestInput name="parent.child" value="test"/>
436+
:
437+
null
438+
}
439+
</Formsy>);
440+
}
441+
}
442+
443+
const form = TestUtils.renderIntoDocument(<TestForm/>);
444+
form.addInput();
445+
immediate(() => {
446+
test.deepEqual(hasChanged.args[0][0], {parent: {child: 'test'}});
447+
test.done();
448+
});
449+
450+
},
451+
418452
'Update a form': {
419453

420454
'should allow elements to check if the form is disabled': function (test) {

tests/Rules-equals-spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { InputFactory } from './utils/TestInput';
77

88
const TestInput = InputFactory({
99
render: function() {
10-
return <input value={this.props.getValue()} readOnly />;
10+
return <input value={this.props.value} readOnly />;
1111
}
1212
});
1313

tests/Rules-isAlpha-spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { InputFactory } from './utils/TestInput';
66

77
const TestInput = InputFactory({
88
render: function() {
9-
return <input value={this.props.getValue()} readOnly />;
9+
return <input value={this.props.value} readOnly />;
1010
}
1111
});
1212

0 commit comments

Comments
 (0)