Skip to content

Commit 99b45e9

Browse files
committed
removed erros and ajv
1 parent 10a4aaa commit 99b45e9

File tree

10 files changed

+374
-310
lines changed

10 files changed

+374
-310
lines changed

README.md

Lines changed: 303 additions & 83 deletions
Large diffs are not rendered by default.

__tests__/ErrorAlert.jsx

Lines changed: 0 additions & 87 deletions
This file was deleted.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
"input",
2121
"select",
2222
"react-select",
23+
"textarea",
24+
"redux-form",
2325
"redux"
2426
],
2527
"private": false,
@@ -36,10 +38,8 @@
3638
},
3739
"dependencies": {
3840
"diacritics": "^1.2.3",
39-
"ajv": "^4.10.0",
4041
"lodash": "^4.17.2",
41-
"core-decorators": "^0.15.0",
42-
"react-addons-shallow-compare": "^15.4.1"
42+
"core-decorators": "^0.15.0"
4343
},
4444
"devDependencies": {
4545
"babel-cli": "^6.18.0",

src/Element.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export default class Element extends Component {
1313
fieldset: PropTypes.object.isRequired,
1414
};
1515

16+
state = {};
17+
1618
constructor(props, context) {
1719
super(props, context);
1820

src/ErrorAlert.jsx

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/Fieldset.jsx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,22 @@ export default class Fieldset extends Element {
5858
super.setValue(value, component, notifyChildren);
5959

6060
if (notifyChildren) {
61-
const { children } = this;
62-
children.forEach(child => child.originalValueChanged());
61+
this.notifyChildren();
6362
}
6463
}
6564

6665
originalValueChanged() {
6766
super.originalValueChanged();
67+
this.notifyChildren();
68+
}
6869

70+
notifyChildren() {
6971
const { children } = this;
7072
children.forEach(child => child.originalValueChanged());
7173
}
7274

7375
registerChild(child, name) {
74-
if (typeof name === 'undefined') {
75-
return;
76-
}
77-
78-
if (name[0] === '.') {
76+
if (name && name[0] === '.') {
7977
const parent = this.getParent();
8078
parent.registerChild(child, name.substr(1));
8179
return;
@@ -85,11 +83,7 @@ export default class Fieldset extends Element {
8583
}
8684

8785
unregisterChild(child, name) {
88-
if (typeof name === 'undefined') {
89-
return;
90-
}
91-
92-
if (name[0] === '.') {
86+
if (name && name[0] === '.') {
9387
const parent = this.getParent();
9488
parent.unregisterChild(child, name.substr(1));
9589
return;

src/Form.jsx

Lines changed: 25 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
11
import { PropTypes, createElement } from 'react';
2-
import Ajv from 'ajv';
32
import { autobind } from 'core-decorators';
43
import Fieldset from './Fieldset';
54

6-
function errorToProperty(err) {
7-
const { params = {} } = err;
8-
9-
if (params.missingProperty) {
10-
return params.missingProperty;
11-
}
12-
13-
return undefined;
14-
}
15-
165
export default class Form extends Fieldset {
17-
static isForm = true;
18-
196
static propTypes = {
207
onSubmit: PropTypes.func,
218
onChange: PropTypes.func,
@@ -28,6 +15,7 @@ export default class Form extends Fieldset {
2815
children: PropTypes.node,
2916
tagName: PropTypes.string.isRequired,
3017
debounce: PropTypes.number,
18+
validate: PropTypes.func,
3119
};
3220

3321
static defaultProps = {
@@ -63,56 +51,6 @@ export default class Form extends Fieldset {
6351
: this;
6452
}
6553

66-
getStateFromProps(props, context) {
67-
const { schema } = props;
68-
69-
let validator = this.validator;
70-
if (schema && (!validator || validator.schema !== schema)) {
71-
const ajv = this.ajv = this.ajv || Ajv({
72-
allErrors: true,
73-
async: true,
74-
});
75-
validator = ajv.compile({
76-
$async: true,
77-
...schema,
78-
});
79-
validator.schema = schema;
80-
}
81-
82-
return {
83-
...super.getStateFromProps(props, context),
84-
validator,
85-
};
86-
}
87-
88-
async validate(value) {
89-
const { validator } = this.state;
90-
if (!validator) {
91-
return [];
92-
}
93-
94-
try {
95-
await validator(value);
96-
return [];
97-
} catch (e) {
98-
const { errors } = e;
99-
100-
return errors.map((err) => {
101-
const prop = errorToProperty(err);
102-
const path = err.dataPath ? err.dataPath.substr(1) : null;
103-
104-
const fullPath = path && prop
105-
? `${path}.${prop}`
106-
: path || prop;
107-
108-
return {
109-
...err,
110-
path: fullPath,
111-
};
112-
});
113-
}
114-
}
115-
11654
getErrors(path, exactMatch) {
11755
const errors = this.errors || [];
11856
if (!path) {
@@ -150,15 +88,34 @@ export default class Form extends Fieldset {
15088
async onSubmit(evn) {
15189
evn.preventDefault();
15290

153-
const { onSubmit, onError } = this.props;
91+
if (this.working) {
92+
return;
93+
}
94+
95+
this.working = true;
96+
this.notifyChildren();
97+
98+
const { onSubmit, onError, validate } = this.props;
15499
const value = this.getValue();
155-
const errors = this.errors = await this.validate(value);
100+
101+
this.errors = validate
102+
? await validate(value)
103+
: [];
104+
105+
const errors = this.errors;
156106

157107
if (!errors.length && onSubmit) {
158-
onSubmit(value);
159-
} else if (errors.length && onError) {
160-
onError(errors);
108+
await onSubmit(value);
109+
} else if (errors && errors.length && onError) {
110+
await onError(errors);
161111
}
112+
113+
this.working = false;
114+
this.notifyChildren();
115+
}
116+
117+
isWorking() {
118+
return !!this.working;
162119
}
163120

164121
setValue(value, component, notifyChildren) {

src/Working.jsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import ProvideProps from './ProvideProps';
2+
3+
export default class Working extends ProvideProps {
4+
componentDidMount() {
5+
const form = this.getForm();
6+
form.registerChild(this);
7+
}
8+
9+
componentWillUnmount() {
10+
const form = this.getForm();
11+
form.unregisterChild(this);
12+
}
13+
14+
originalValueChanged() {
15+
const form = this.getForm();
16+
17+
this.setState({
18+
isWorking: form.isWorking(),
19+
});
20+
}
21+
22+
renderEmptyChildren({ isWorking }) {
23+
return (
24+
<span>{isWorking ? 'working' : 'idle'}</span>
25+
);
26+
}
27+
28+
getProps() {
29+
return {
30+
isWorking: this.state.isWorking,
31+
};
32+
}
33+
}

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ export default from './Form';
22
export Input from './Input';
33
export Select from './Select';
44
export Textarea from './Textarea';
5-
export ErrorAlert from './ErrorAlert';
65
export Fieldset from './Fieldset';
76
export Tbody from './Tbody';
87
export Word from './Word';
@@ -18,3 +17,4 @@ export Value from './Value';
1817
export ProvideProps from './ProvideProps';
1918
export ProvideIndexes from './ProvideIndexes';
2019
export Button from './Button';
20+
export Working from './Working';

src/utils/set.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default function set(currentValue, originalPath, value) {
1313
}
1414

1515
let current = start;
16-
for (let i = 0; i < parts.length; i++) {
16+
for (let i = 0; i < parts.length; i += 1) {
1717
const key = parts[i];
1818
if (!key) {
1919
throw new Error('Path is not complete');

0 commit comments

Comments
 (0)