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

Commit 54bd4e0

Browse files
author
Dominik Sumer
committed
added touch method to Form
added Timer for detaching Inputs
1 parent 213fd71 commit 54bd4e0

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/Form.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import PropTypes from 'prop-types';
33
import autoBind from 'react-autobind';
44
import validationRules from './validationRules';
55

6+
const DETACH_INPUT_TIMEOUT = 200;
7+
68
export default class Form extends React.Component {
79
static validationRules = Object.assign({}, validationRules);
810

@@ -19,6 +21,7 @@ export default class Form extends React.Component {
1921
this.valid = false;
2022
this.validationQueue = [];
2123
this.validating = false;
24+
this.detachInputTimer = null;
2225

2326
autoBind(this);
2427
}
@@ -47,9 +50,7 @@ export default class Form extends React.Component {
4750
onSubmit(event) {
4851
event.preventDefault();
4952

50-
for (let input of this.inputs) {
51-
input.touch();
52-
}
53+
this.touch();
5354

5455
const valid = this.isValid();
5556
if (this.props.onSubmit) {
@@ -98,8 +99,9 @@ export default class Form extends React.Component {
9899
}
99100

100101
detachInput(input) {
102+
clearTimeout(this.detachInputTimer);
101103
this.inputs.splice(this.inputs.indexOf(input), 1);
102-
this.validate();
104+
this.detachInputTimer = setTimeout(this.validate, DETACH_INPUT_TIMEOUT);
103105
}
104106

105107
addToValidationQueue(input) {
@@ -190,6 +192,12 @@ export default class Form extends React.Component {
190192
}
191193
}
192194

195+
touch() {
196+
for (let input of this.inputs) {
197+
input.touch();
198+
}
199+
}
200+
193201
render() {
194202
const {children, className} = this.props;
195203

src/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Form from './Form';
2+
import Input from './Input';
3+
4+
Form.Input = Input;
5+
6+
export default Form;

0 commit comments

Comments
 (0)