Skip to content

Commit 4656813

Browse files
committed
Add resetFields() and validateFieldsWithoutFeedback()
1 parent 5e995be commit 4656813

36 files changed

+941
-658
lines changed

CHANGELOG.md

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
1-
## 0.9.3 (2018/09/12)
1+
## v0.10.0 (2018/09/18)
22

33
### Features
44

5-
- Use PropTypes.instanceOf() instead of PropTypes.object
5+
- `resetFields()`
6+
- `validateFieldsWithoutFeedback()`
7+
8+
### Breaking Changes
9+
10+
- `reset()` renamed to `resetFields()`
11+
12+
### Fixes
13+
14+
- Ignore HTML elements without `ValidityState` instead of `type`
15+
16+
17+
## v0.9.3 (2018/09/12)
18+
19+
### Features
20+
21+
- Use `PropTypes.instanceOf()` instead of `PropTypes.object`
622
- Improve typings
723

824
### Fixes
925

10-
- Ignore HTML elements without type
26+
- Ignore HTML elements without `type`
27+
1128

12-
## 0.9.2 (2018/07/13)
29+
## v0.9.2 (2018/07/13)
1330

1431
### Features
1532

1633
- Upgrade to Bootstrap 4.1.2
1734
- Add a README.md for every npm package
1835

19-
## 0.9.1 (2018/07/06)
36+
37+
## v0.9.1 (2018/07/06)
2038

2139
### Features
2240

@@ -31,7 +49,8 @@
3149
- React Native: styling done on `FieldFeedback` with `theme` props instead of `FormWithConstraints.fieldFeedbackStyles`
3250
- Rename TypeScript `Input` to `InputElement`
3351

34-
## 0.8.0 (2018/04/26)
52+
53+
## v0.8.0 (2018/04/26)
3554

3655
### Features
3756

@@ -52,14 +71,16 @@
5271
- Fix `computeFieldFeedbackKey()` implementation
5372
- Fix possible crash with React Native, see 03d72e1
5473

55-
## 0.7.1 (2017/11/27)
74+
75+
## v0.7.1 (2017/11/27)
5676

5777
### Fixes
5878

5979
- Fix [CodeClimate coverage report](https://codeclimate.com/github/tkrotoff/react-form-with-constraints), see 4704370
6080
- Expose 'react-form-with-constraints-bootstrap4/lib/Enzyme', see c4ce710
6181

62-
## 0.7.0 (2017/11/26)
82+
83+
## v0.7.0 (2017/11/26)
6384

6485
### Features
6586

README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ Resources:
3838

3939
- Minimal API and footprint
4040
- Unobtrusive: easy to adapt regular [React code](https://reactjs.org/docs/forms.html)
41-
- Control HTML5 error messages: `<FieldFeedback when="valueMissing">My custom error message</FieldFeedback>`
41+
- HTML5 error messages personalization: `<FieldFeedback when="valueMissing">My custom error message</FieldFeedback>`
4242
- Custom constraints: `<FieldFeedback when={value => ...}>`
4343
- Warnings and infos: `<FieldFeedback ... warning>`, `<FieldFeedback ... info>`
4444
- Async validation
4545
- No dependency beside React (no Redux, MobX...)
4646
- Re-render only what's necessary
4747
- Easily extendable
48-
- Support for [React Native](examples/ReactNative) with npm package `react-form-with-constraints-native`
4948
- [Bootstrap 4](examples/Bootstrap4) styling with npm package `react-form-with-constraints-bootstrap4`
5049
- [Material-UI](examples/MaterialUI) integration with npm package `react-form-with-constraints-material-ui`
50+
- Support for [React Native](examples/ReactNative) with npm package `react-form-with-constraints-native`
5151
- ...
5252

5353
```JSX
@@ -106,7 +106,7 @@ It is also inspired by [AngularJS ngMessages](https://docs.angularjs.org/api/ngM
106106

107107
If you had to implement validation yourself, you would end up with [a global object that tracks errors for each field](examples/NoFramework/App.tsx).
108108
react-form-with-constraints [works similarly](packages/react-form-with-constraints/src/FieldsStore.ts).
109-
It uses [React context](https://github.com/reactjs/reactjs.org/blob/d59c4f9116138e419812e44b0fdb56644c498d3e/content/docs/context.md) to share the [`FieldsStore`](packages/react-form-with-constraints/src/FieldsStore.ts) object across [`FieldFeedbacks`](packages/react-form-with-constraints/src/FieldFeedbacks.tsx) and [`FieldFeedback`](packages/react-form-with-constraints/src/FieldFeedback.tsx).
109+
It uses [React context](https://reactjs.org/docs/legacy-context.html) to share the [`FieldsStore`](packages/react-form-with-constraints/src/FieldsStore.ts) object across [`FieldFeedbacks`](packages/react-form-with-constraints/src/FieldFeedbacks.tsx) and [`FieldFeedback`](packages/react-form-with-constraints/src/FieldFeedback.tsx).
110110

111111
## API
112112

@@ -162,7 +162,7 @@ class MyForm extends React.Component {
162162
// Validates the non-dirty fields and returns Promise<Field[]>
163163
const fields = await this.form.validateForm();
164164

165-
// or simply this.form.isValid();
165+
// or simply use this.form.isValid()
166166
const formIsValid = fields.every(field => field.isValid());
167167

168168
if (formIsValid) console.log('The form is valid');
@@ -236,7 +236,7 @@ class MyForm extends React.Component {
236236
- `info?: boolean` => treats the feedback as an info
237237
- `children` => what to display when the constraint matches; if missing, displays the [HTML5 error message](https://www.w3.org/TR/html51/sec-forms.html#the-constraint-validation-api) if any
238238

239-
- [`Async<T>`](packages/react-form-with-constraints/src/Async.tsx) => Async version of `FieldFeedback`, similar API as [react-promise](https://github.com/capaj/react-promise)
239+
- [`Async<T>`](packages/react-form-with-constraints/src/Async.tsx) => Async version of `FieldFeedback` (similar API as [react-promise](https://github.com/capaj/react-promise))
240240
- `promise: (value: string) => Promise<T>` => a promise you want to wait for
241241
- `pending?: React.ReactNode` => runs when promise is pending
242242
- `then?: (value: T) => React.ReactNode` => runs when promise is resolved
@@ -248,15 +248,21 @@ class MyForm extends React.Component {
248248
Should be called when a `field` changes, will re-render the proper `FieldFeedback`s (and update the internal `FieldsStore`).
249249
Without arguments, all fields (`$('[name]')`) are validated.
250250

251+
- `validateFieldsWithoutFeedback(...inputsOrNames: Array<Input | string>): Promise<Field[]>` =>
252+
Validates only all non-dirty fields (won't re-validate fields that have been already validated with `validateFields()`),
253+
If you want to force re-validate all fields, use `validateFields()`.
254+
251255
- `validateForm(): Promise<Field[]>` =>
252-
Should be called before to submit the `form`. Validates only all non-dirty fields (won't re-validate fields that have been already validated with `validateFields(...)`),
253-
If you want to force re-validate all fields, use `validateFields()` without arguments.
256+
Other name for `validateFieldsWithoutFeedback()`, typically called before to submit the `form`.
257+
Might be removed in the future?
254258

255-
- `isValid(): boolean` => should be called after `validateForm()` or `validateFields()`, tells if the fields are valid
259+
- `isValid(): boolean` => should be called after `validateFields()`, `validateFieldsWithoutFeedback()` or `validateForm()`, tells if the fields are valid
256260

257261
- `hasFeedbacks(): boolean` => tells if the fields have any kind of feedback
258262

259-
- `reset(): Promise` => resets internal `FieldsStore` and re-render all `FieldFeedback`s
263+
- `resetFields(...inputsOrNames: Array<Input | string>): Promise<Field[]>` =>
264+
Resets the given fields and re-render the proper `FieldFeedback`s.
265+
Without arguments, all fields (`$('[name]')`) are reset.
260266

261267
- [`Field`](packages/react-form-with-constraints/src/Field.ts) =>
262268
```TypeScript
@@ -281,7 +287,10 @@ class MyForm extends React.Component {
281287

282288
## Browser support
283289

284-
You can use HTML5 attributes like `type="email"`, `required`, `pattern`..., in this case a [recent browser](http://caniuse.com/#feat=forms) is needed,...
290+
react-form-with-constraints needs [`ValidityState`](https://developer.mozilla.org/en-US/docs/Web/API/ValidityState) which is supported by all modern browsers and IE >= 10.
291+
It also needs a polyfill such as [core-js](https://github.com/zloirock/core-js) or [@babel/polyfill](https://babeljs.io/docs/usage/polyfill/) to support IE <= 11, see [React JavaScript Environment Requirements](https://reactjs.org/docs/javascript-environment-requirements.html).
292+
293+
You can use HTML5 attributes like `type="email"`, `required`, [`minlength`](https://caniuse.com/#feat=input-minlength)...
285294

286295
```JSX
287296
<label htmlFor="username">Username</label>
@@ -293,7 +302,7 @@ You can use HTML5 attributes like `type="email"`, `required`, `pattern`..., in t
293302
</FieldFeedbacks>
294303
```
295304

296-
...or ignore them and rely on `when` functions:
305+
...and/or rely on `when` functions:
297306

298307
```JSX
299308
<label htmlFor="username">Username</label>
@@ -307,8 +316,6 @@ You can use HTML5 attributes like `type="email"`, `required`, `pattern`..., in t
307316

308317
In the last case you will have to manage translations yourself (see SignUp example).
309318

310-
react-form-with-constraints needs a polyfill such as [core-js](https://github.com/zloirock/core-js) or [babel-polyfill](https://babeljs.io/docs/usage/polyfill/) to support IE 11 and lower. See also [React JavaScript Environment Requirements](https://reactjs.org/docs/javascript-environment-requirements.html).
311-
312319
## Notes
313320

314321
- A [`type="hidden"`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/hidden#Validation), [`readonly`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-readonly) or [`disabled`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-disabled) input won't trigger any HTML5 form constraint validation like [`required`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-required), see https://codepen.io/tkrotoff/pen/gdjVNv

examples/Bootstrap4/App.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class Form extends React.Component {
8484

8585
handleReset = () => {
8686
this.setState(this.getInitialState());
87-
this.form.reset();
87+
this.form.resetFields();
8888
this.setState({resetButtonDisabled: true});
8989
}
9090

examples/MaterialUI/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class Form extends React.Component<PropsWithStyles, State> {
120120

121121
handleReset = () => {
122122
this.setState(this.getInitialState());
123-
this.form!.reset();
123+
this.form!.resetFields();
124124
this.setState({resetButtonDisabled: true});
125125
}
126126

examples/MaterialUI/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
"lodash": "latest",
2020

21-
"@material-ui/core": "latest",
21+
"@material-ui/core": "~3.0.3",
2222

2323
"react-form-with-constraints": "^0.9.3",
2424
"react-form-with-constraints-material-ui": "^0.9.3",

examples/Password/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class Form extends React.Component<Props, State> {
101101
// Validates the non-dirty fields and returns the related FieldValidation structures
102102
const fields = await this.form!.validateForm();
103103

104-
// or simply this.form.isValid();
104+
// or simply use this.form.isValid()
105105
const formIsValid = fields.every(field => field.isValid());
106106

107107
if (formIsValid) console.log('The form is valid');

examples/ReactNative/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export default class App extends React.Component<Props, State> {
109109

110110
handleReset = () => {
111111
this.setState(this.getInitialState());
112-
this.form!.reset();
112+
this.form!.resetFields();
113113
this.setState({resetButtonDisabled: true});
114114
}
115115

examples/ServerSideRendering/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class Form extends React.Component<Props, State> {
9999
// Validates the non-dirty fields and returns the related FieldValidation structures
100100
const fields = await this.form!.validateForm();
101101

102-
// or simply this.form.isValid();
102+
// or simply use this.form.isValid()
103103
const formIsValid = fields.every(field => field.isValid());
104104

105105
if (formIsValid) console.log('The form is valid');

0 commit comments

Comments
 (0)