Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit 1f84a9b

Browse files
author
Dominik Sumer
committed
v0.1.1 - for changes see CHANGELOG.md
1 parent 13c8240 commit 1f84a9b

File tree

8 files changed

+79
-13
lines changed

8 files changed

+79
-13
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 0.1.1 (November 21, 2017)
2+
- added possibility to add props to form element via [formProps](https://github.com/cat-react/form/blob/master/docs/api.md#formprops) prop
3+
- the validation rule `isRequired` now checks for `undefined`, `null` or an empty string. everything else is valid
4+
15
### 0.1.0 (October 3, 2017)
26
- first official release!
37
- added api documentation

docs/api.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Welcome to the `@cat-react/form` API documentation.
1010
- [onValidChanged](#onvalidchangedvalid-values-isvalidating)
1111
- [onValid](#onvalidvalues)
1212
- [onInvalid](#oninvalidvalues-isvalidating)
13+
- [className](#classname)
14+
- [formProps](#formprops)
1315
- [Input](#input) (HOC for building input fields)
1416
- Retrieves
1517
- [value](#value)
@@ -290,6 +292,38 @@ render() {
290292
```
291293
---
292294

295+
### className
296+
CSS ClassName which will be passed directly to the <form> html element.
297+
298+
For example:
299+
```jsx
300+
<Form className="test" />
301+
```
302+
303+
will result in:
304+
305+
```jsx
306+
<form class="test" />
307+
```
308+
309+
---
310+
311+
### formProps
312+
Props which will be passed directly to the <form> html element.
313+
314+
For example:
315+
```jsx
316+
<Form formProps={{autocomplete: 'off'}} />
317+
```
318+
319+
will result in:
320+
321+
```jsx
322+
<form autocomplete="off" />
323+
```
324+
325+
---
326+
293327
## Input
294328
Higher-Order Component for building input fields.
295329

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cat-react/form",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "A simple yet powerful library which helps creating validated forms in react.",
55
"main": "index.umd.min.js",
66
"jsnext:main": "index.es.js",

src/Form.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,10 @@ export default class Form extends React.Component {
205205
}
206206

207207
render() {
208-
const {children, className} = this.props;
209-
210-
const formProps = {
211-
className
212-
};
208+
const {children, className, formProps} = this.props;
213209

214210
return (
215-
<form {...formProps} onSubmit={this.onSubmit}>
211+
<form className={className} {...formProps} onSubmit={this.onSubmit}>
216212
{children}
217213
</form>
218214
);

src/validationRules.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const rules = {
33
return regexp.test(value);
44
},
55
isRequired: (values, value) => {
6-
return !!value;
6+
const isEmpty = (typeof value === 'undefined' || value === null || value === '');
7+
return !isEmpty;
78
},
89
isEmail: function (values, value) {
910
return rules.matchRegexp(values, value, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i);
@@ -31,11 +32,11 @@ const rules = {
3132
},
3233
isNumber: (values, value) => {
3334
switch (typeof value) {
34-
case 'string':
35+
case 'string':
3536
return rules.matchRegexp(values, value, /^-?\d+\.?\d*$/);
36-
case 'number':
37+
case 'number':
3738
return true;
38-
default:
39+
default:
3940
return false;
4041
}
4142
}

tests/Form.spec.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,20 @@ class SpecialForm extends React.Component {
3535

3636
describe('Form', () => {
3737
it('should render correctly', () => {
38-
let wrapper = shallow(<Form className="myForm"><span className="abc">abc</span></Form>);
38+
const onInvalid = jest.fn();
39+
let wrapper = shallow(<Form className="myForm" onInvalid={onInvalid}><span className="abc">abc</span></Form>);
40+
wrapper.instance().validate();
3941
expect(wrapper.contains(<span className="abc">abc</span>)).toBe(true);
4042
expect(wrapper.is('.myForm')).toBe(true);
43+
expect(onInvalid).not.toHaveBeenCalled();
44+
});
45+
46+
it('should pass all props', () => {
47+
let wrapper = shallow(<Form className="myForm" formProps={{autocomplete: 'off', fantasy: true}}><span className="abc">abc</span></Form>);
48+
const props = wrapper.props();
49+
expect(props.autocomplete).toBe('off');
50+
expect(props.className).toBe('myForm');
51+
expect(props.fantasy).toBe(true);
4152
});
4253

4354
it('should add the vaidationRule', () => {
@@ -58,12 +69,14 @@ describe('Form', () => {
5869
const onSubmit = jest.fn();
5970
const onValidSubmit = jest.fn();
6071
const onInvalidSubmit = jest.fn();
72+
const onValidChanged = jest.fn();
6173
const onValid = jest.fn();
6274
const onInvalid = jest.fn();
6375
let wrapper = mount(<Form className="myForm"
6476
onSubmit={onSubmit}
6577
onValidSubmit={onValidSubmit}
6678
onInvalidSubmit={onInvalidSubmit}
79+
onValidChanged={onValidChanged}
6780
onValid={onValid}
6881
onInvalid={onInvalid}>
6982
<button type="submit"/>
@@ -78,6 +91,9 @@ describe('Form', () => {
7891
expect(onInvalid).toHaveBeenCalledWith({}, false);
7992
expect(onValid).toHaveBeenCalledTimes(1);
8093
expect(onValid).toHaveBeenCalledWith({});
94+
expect(onValidChanged.mock.calls.length).toBe(2);
95+
expect(onValidChanged.mock.calls[0]).toEqual([false, {}, false]);
96+
expect(onValidChanged.mock.calls[1]).toEqual([true, {}, false]);
8197
});
8298

8399
it('should submit without triggering the events', () => {

tests/Input.spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ describe('Input', () => {
4949
wrapper.instance().touch();
5050
wrapper.update();
5151
expect(instance.isPristine()).toBe(false);
52+
wrapper.instance().touch();
53+
wrapper.update();
54+
expect(instance.isPristine()).toBe(false);
5255
});
5356

5457
it('should change the value correctly', (done) => {

tests/validationRules.spec.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,19 @@ import rules from '../src/validationRules';
33
describe('Validation Rules', () => {
44
describe('isRequired', () => {
55
it('should successfully validate the value', () => {
6-
const valid = rules.isRequired(null, 'abc');
6+
let valid = rules.isRequired(null, 'abc');
7+
expect(valid).toBe(true);
8+
valid = rules.isRequired(null, true);
9+
expect(valid).toBe(true);
10+
valid = rules.isRequired(null, false);
11+
expect(valid).toBe(true);
12+
valid = rules.isRequired(null, 0);
13+
expect(valid).toBe(true);
14+
valid = rules.isRequired(null, '0');
15+
expect(valid).toBe(true);
16+
valid = rules.isRequired(null, {});
17+
expect(valid).toBe(true);
18+
valid = rules.isRequired(null, []);
719
expect(valid).toBe(true);
820
});
921
it('should invalidate the value', () => {

0 commit comments

Comments
 (0)