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

Commit 53093bd

Browse files
feat: 🎸 new config options (#6)
* feat: 🎸 new config options
1 parent ff629fc commit 53093bd

File tree

12 files changed

+507
-218
lines changed

12 files changed

+507
-218
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ speed-measure-plugin*.json
3737
/coverage
3838
/libpeerconnection.log
3939
npm-debug.log
40+
debug.log
4041
yarn-error.log
4142
testem.log
4243
/typings

README.md

Lines changed: 87 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,26 @@ The design of this library promotes less boilerplate code, which keeps your temp
2828

2929
## Table of Contents
3030

31+
- [How it works](#how_it_works)
3132
- [Installation](#installation)
3233
- [Usage](#usage)
3334
- [Advanced configuration](#configuration)
35+
- [Handling form submission](#handling_form_submission)
36+
- [Getting error details](#getting_error_details)
3437
- [Styling](#styling)
3538
- [Development](#development)
3639

40+
## How it works
41+
42+
There are a few rules that the library follows to determine when to display errors:
43+
44+
- Errors will be shown no matter what configuration you're using after form is submitted.
45+
- If no configuration is provided, the errors will be shown when control is `touched`.
46+
- This is chosen due to the UX guidelines. Read (1) [How to Report Errors in Forms](https://www.nngroup.com/articles/errors-forms-design-guidelines/) and (2) [Designing More Efficient Forms](https://uxplanet.org/designing-more-efficient-forms-assistance-and-validation-f26a5241199d) for more info.
47+
- If you configured errors to be shown when `formIsSubmitted`, but dealing with a control that does not have a parent _form_, the config for this control will fall back to `touched`.
48+
49+
For more info about this see [Advanced configuration](#configuration).
50+
3751
## Installation
3852

3953
### NPM
@@ -119,9 +133,9 @@ export class MyAppModule {}
119133
Alternatively, use dependency injection to provide configuration at a component level:
120134

121135
```ts
122-
import { ErrorsConfiguration } from '@ngspot/ngx-errors';
136+
import { ErrorsConfiguration, IErrorsConfiguration } from '@ngspot/ngx-errors';
123137

124-
const myConfig = { ... }; // <- specify config
138+
const myConfig: IErrorsConfiguration = { ... }; // <- specify config
125139

126140
@Component({
127141
...
@@ -135,26 +149,83 @@ export class MyComponent { }
135149
Here's the configuration object interface:
136150

137151
```ts
138-
interface IErrorsConfiguration {
152+
export interface IErrorsConfiguration {
139153
/**
140-
* Configure errors to show only when the corresponding input is dirty.
154+
* Configures when to display an error for an invalid control. Available options are:
141155
*
142-
* Default is `true`.
143-
*/
144-
showErrorsOnlyIfInputDirty?: boolean;
145-
146-
/**
147-
* Configure errors to show only when form is submitted.
148-
* Upon form submission shows errors even if `showErrorsOnlyIfInputDirty = true`
149-
* and some of the inputs aren't dirty.
150-
* Takes effect only when ngxErrors directive is a child of a form.
156+
* `'touched'` - *[default]* shows an error when control is marked as touched. For example, user focused on the input and clicked away or tabbed through the input.
157+
*
158+
* `'dirty'` - shows an error when control is marked as dirty. For example, when user has typed something in.
151159
*
152-
* Default is `false`.
160+
* `'touchedAndDirty'` - shows an error when control is marked as both - touched and dirty.
161+
*
162+
* `'formIsSubmitted'` - shows an error when parent form was submitted.
153163
*/
154-
showErrorsWhenFormSubmitted?: boolean;
164+
showErrorsWhenInput: ShowErrorWhen;
155165
}
166+
167+
export type ShowErrorWhen =
168+
| 'touched'
169+
| 'dirty'
170+
| 'touchedAndDirty'
171+
| 'formIsSubmitted';
172+
```
173+
174+
### Overriding global config
175+
176+
You can override the configuration specified at the module level by using `[showWhen]` input on `[ngxErrors]` and on `[ngxError]` directives:
177+
178+
```html
179+
<div ngxErrors="control" showWhen="touchedAndDirty">
180+
<div ngxError="required" showWhen="dirty">
181+
This will be shown when control is dirty
182+
</div>
183+
184+
<div ngxError="min">
185+
This will be shown when control is touched and dirty
186+
</div>
187+
</div>
156188
```
157189

190+
## Handling form submission
191+
192+
Often there's a requirement to submit a form when user presses _Enter_. Under the hood ngxError relies on form submit event to display errors. That is why it's important to trigger form submission properly rather than binding `(keyup.enter)` event to the method in your component class directly. Here's how to do that:
193+
194+
```html
195+
<form
196+
[formGroup]="form"
197+
(ngSubmit)="yourMethod()"
198+
(keyup.enter)="submitBtn.click()"
199+
>
200+
...
201+
202+
<button #submitBtn>Submit</button>
203+
</form>
204+
```
205+
206+
## Getting error details
207+
208+
Each control error in Angular may contain additional details. For example, here's what `min` error looks like:
209+
210+
```ts
211+
const control = new FormControl(3, Validators.min(10));
212+
const error = control.getError('min');
213+
console.log(error); // prints: { min: 10, actual: 3 }
214+
```
215+
216+
You can easily get access to these details in the template:
217+
218+
```html
219+
<div ngxErrors="control">
220+
<div ngxError="min" #myMin="ngxError">
221+
Number should be greater than {{myMin.err.min}}. You've typed
222+
{{myMin.err.actual}}.
223+
</div>
224+
</div>
225+
```
226+
227+
In the example above we're assigning a variable `myMin` (can be anything you want) to the directive `ngxError`. Using this variable we can access the context of the directive. The directive has property `err` that contains all the error details.
228+
158229
## Styling
159230

160231
Include something similar to the following in global CSS file:
@@ -207,6 +278,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
207278

208279
<!-- markdownlint-enable -->
209280
<!-- prettier-ignore-end -->
281+
210282
<!-- ALL-CONTRIBUTORS-LIST:END -->
211283

212284
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

projects/ngx-errors/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ngspot/ngx-errors",
3-
"version": "1.0.1",
3+
"version": "2.0.0",
44
"description": "",
55
"peerDependencies": {
66
"@angular/core": "^9.0.0",

0 commit comments

Comments
 (0)