@klippa/ngx-enhancy-forms 1.2.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +179 -23
- package/bundles/klippa-ngx-enhancy-forms.umd.js +288 -97
- package/bundles/klippa-ngx-enhancy-forms.umd.js.map +1 -1
- package/bundles/klippa-ngx-enhancy-forms.umd.min.js +2 -2
- package/bundles/klippa-ngx-enhancy-forms.umd.min.js.map +1 -1
- package/esm2015/klippa-ngx-enhancy-forms.js +2 -17
- package/esm2015/lib/elements/button/button.component.js +3 -3
- package/esm2015/lib/elements/checkbox/checkbox.component.js +1 -1
- package/esm2015/lib/elements/datepicker/datepicker.component.js +111 -0
- package/esm2015/lib/elements/email/email-input.component.js +6 -2
- package/esm2015/lib/elements/loading-indicator/loading-indicator.component.js +2 -2
- package/esm2015/lib/elements/number-input/number-input.component.js +1 -1
- package/esm2015/lib/elements/password-field/password-field.component.js +11 -4
- package/esm2015/lib/elements/select/select.component.js +8 -12
- package/esm2015/lib/elements/sortable-items/sortable-items.component.js +1 -1
- package/esm2015/lib/elements/text-input/text-input.component.js +1 -1
- package/esm2015/lib/elements/toggle/toggle.component.js +1 -1
- package/esm2015/lib/elements/value-accessor-base/value-accessor-base.component.js +1 -1
- package/esm2015/lib/form/form-caption/form-caption.component.js +1 -1
- package/esm2015/lib/form/form-element/form-element.component.js +29 -5
- package/esm2015/lib/form/form-error/form-error.component.js +1 -1
- package/esm2015/lib/form/form-submit-button/form-submit-button.component.js +18 -7
- package/esm2015/lib/form/form.component.js +21 -7
- package/esm2015/lib/material.module.js +17 -0
- package/esm2015/lib/ngx-enhancy-forms.module.js +11 -6
- package/esm2015/lib/types.js +2 -0
- package/esm2015/lib/util/values.js +1 -1
- package/esm2015/lib/validators/dateValidator.js +6 -0
- package/esm2015/public-api.js +20 -1
- package/fesm2015/klippa-ngx-enhancy-forms.js +255 -70
- package/fesm2015/klippa-ngx-enhancy-forms.js.map +1 -1
- package/klippa-ngx-enhancy-forms.d.ts +1 -16
- package/klippa-ngx-enhancy-forms.metadata.json +1 -1
- package/lib/elements/datepicker/datepicker.component.d.ts +26 -0
- package/lib/elements/password-field/password-field.component.d.ts +1 -0
- package/lib/elements/select/select.component.d.ts +3 -3
- package/lib/form/form-element/form-element.component.d.ts +9 -2
- package/lib/form/form-error/form-error.component.d.ts +2 -1
- package/lib/form/form.component.d.ts +2 -0
- package/lib/material.module.d.ts +2 -0
- package/lib/types.d.ts +15 -0
- package/lib/validators/dateValidator.d.ts +3 -0
- package/package.json +8 -9
- package/public-api.d.ts +19 -0
package/README.md
CHANGED
|
@@ -1,43 +1,199 @@
|
|
|
1
1
|
# NgxEnhancyForms
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Enhancy Forms is the forms framework used for klippa frontend applications.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Known Issues:
|
|
6
|
+
- ngx-date-fns-adapter is not compatible with Angular 12. The date picker can be used, but cannot be internationalised.
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
`yarn add @klippa/ngx-enhancy-forms`
|
|
8
|
+
# How to install
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
`npm install @klippa/ngx-enhancy-forms`
|
|
12
|
-
|
|
13
|
-
## Import
|
|
14
|
-
|
|
15
|
-
TODO
|
|
16
|
-
|
|
17
|
-
EXAMPLE:
|
|
10
|
+
Install using npm/yarn:
|
|
18
11
|
|
|
12
|
+
```bash
|
|
13
|
+
$ yarn add @klippa/ngx-enhancy-forms
|
|
19
14
|
```
|
|
20
|
-
I recommend importing the module in a generic ui module and then export it from there, so you can use the ngx-async-template everywhere in your app.
|
|
21
15
|
|
|
22
|
-
|
|
23
|
-
|
|
16
|
+
Import the `NgxEnhancyFormsModule` into your module.
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
// my.module.js
|
|
20
|
+
/* ... */
|
|
21
|
+
import {NgxEnhancyFormsModule} from "@klippa/ngx-enhancy-forms";
|
|
24
22
|
|
|
25
23
|
@NgModule({
|
|
24
|
+
declarations: [/* ... */],
|
|
26
25
|
imports: [
|
|
27
|
-
|
|
26
|
+
/* ... */,
|
|
27
|
+
NgxEnhancyFormsModule,
|
|
28
28
|
],
|
|
29
|
+
|
|
29
30
|
exports: [
|
|
30
|
-
|
|
31
|
+
/* ... */
|
|
32
|
+
NgxEnhancyFormsModule, // Export it if you wish to use it in parent modules.
|
|
31
33
|
],
|
|
32
34
|
})
|
|
33
|
-
export class
|
|
34
|
-
|
|
35
|
+
export class MyModule {
|
|
36
|
+
}
|
|
35
37
|
```
|
|
36
38
|
|
|
37
|
-
#
|
|
39
|
+
# Usage
|
|
38
40
|
|
|
39
|
-
|
|
41
|
+
## Example form
|
|
40
42
|
|
|
41
|
-
|
|
43
|
+
```html
|
|
44
|
+
<klp-form>
|
|
45
|
+
<klp-form-element caption="Username">
|
|
46
|
+
<klp-form-text-input formControlName='username'></klp-form-text-input>
|
|
47
|
+
</klp-form-element>
|
|
48
|
+
<klp-form-element caption="Password">
|
|
49
|
+
<klp-form-password-field formControlName="password"></klp-form-password-field>
|
|
50
|
+
</klp-form-element>
|
|
51
|
+
<klp-form-submit-button fullWidth [submitCallback]="onSubmit">
|
|
52
|
+
Login
|
|
53
|
+
</klp-form-submit-button>
|
|
54
|
+
</klp-form>
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
@Component({
|
|
59
|
+
selector: 'app-login-form',
|
|
60
|
+
templateUrl: './login-form.component.html',
|
|
61
|
+
styleUrls: ['./login-form.component.scss'],
|
|
62
|
+
})
|
|
63
|
+
export class LoginFormComponent {
|
|
64
|
+
loginForm: FormGroup = this.fb.group({
|
|
65
|
+
username: [null, Validators.required],
|
|
66
|
+
password: [null, [Validators.required, Validators.minLength(8)]],
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
constructor( private fb: FormBuilder ) {}
|
|
70
|
+
|
|
71
|
+
/** onSubmit must be a lambda function **/
|
|
72
|
+
onSubmit = (value: any): Promise<void> => console.log(value);
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Notable elements and features
|
|
77
|
+
|
|
78
|
+
### `<klp-form-submit-button>`
|
|
79
|
+
|
|
80
|
+
The `submitCallback` is an input rather than a output. As such it _must_ be a lambda function to maintain the correct scope.
|
|
81
|
+
|
|
82
|
+
### Providing alternative error messages
|
|
83
|
+
|
|
84
|
+
The `<klp-form-element>` uses some default error messages for the internal validations. If you wish to use different messages,
|
|
85
|
+
for example if you wish to translate them, you can do so by providing `FORM_ERROR_MESSAGES` somewhere in your application.
|
|
86
|
+
|
|
87
|
+
The `FORM_ERROR_MESSAGES` should be of type `CustomErrorMessages` as exported from [types.ts](./src/lib/types.ts).
|
|
88
|
+
|
|
89
|
+
The `min`, `max`, `minLength`, and `maxLength` messages will substitute `%min%`, `%max%`, `%minLength%`, and `%maxLength%`
|
|
90
|
+
with their respective limits.
|
|
91
|
+
|
|
92
|
+
Here is an example of how you might provide translations for the example login form:
|
|
93
|
+
|
|
94
|
+
```js
|
|
95
|
+
@Component({
|
|
96
|
+
selector: 'app-login-form',
|
|
97
|
+
templateUrl: './login-form.component.html',
|
|
98
|
+
styleUrls: ['./login-form.component.scss'],
|
|
99
|
+
providers: [
|
|
100
|
+
{
|
|
101
|
+
provide: FORM_ERROR_MESSAGES,
|
|
102
|
+
deps: [TranslateService],
|
|
103
|
+
useFactory: (translate: TranslateService) => {
|
|
104
|
+
return {
|
|
105
|
+
min: () => translate.instant(_("use a number larger than %min%")),
|
|
106
|
+
/* ... */
|
|
107
|
+
};
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
],
|
|
111
|
+
})
|
|
112
|
+
export class LoginFormComponent {
|
|
113
|
+
/* ... */
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Providing locale preferences for the date picker
|
|
118
|
+
|
|
119
|
+
The DatePicker uses the @Angular/material date picker, but uses standard `Date`s internally
|
|
120
|
+
so is only compatible with `DateAdapters` that also use `Date`.
|
|
121
|
+
|
|
122
|
+
#### Angular 10/11
|
|
123
|
+
|
|
124
|
+
For Angular 10 and 11, the `Date` restriction means that only the `MatNativeDateAdapter` or a custom adapter
|
|
125
|
+
such as [NgxMatDatefnsDateAdapter](https://www.npmjs.com/package/ngx-mat-datefns-date-adapter) can be used.
|
|
126
|
+
The former provides no localization capabilities and so is not very useful.
|
|
127
|
+
|
|
128
|
+
Here is an example of how to provide a customised NgxMatDatefnsDateAdapter that accepts multiple date formats for input
|
|
129
|
+
|
|
130
|
+
```js
|
|
131
|
+
// custom DateAdapter
|
|
132
|
+
class MultiParseDateFnsDateAdapter extends NgxDateFnsDateAdapter {
|
|
133
|
+
// override the parse function to take arrays of parseFormats and try each in turn.
|
|
134
|
+
parse(value: any, parseFormat: any) {
|
|
135
|
+
if (typeof value === 'string') {
|
|
136
|
+
if (value.length === 0) {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
// replace all '/' and '.' with '-' for locales that use '/' or '.' seperated dates.
|
|
140
|
+
value = value.replace(/[\/.]/g, '-');
|
|
141
|
+
}
|
|
142
|
+
if (Array.isArray(parseFormat)) {
|
|
143
|
+
for (const format of parseFormat) {
|
|
144
|
+
if (isMatch(value, format)) {
|
|
145
|
+
return super.parse(value, format);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
git
|
|
154
|
+
```ts
|
|
155
|
+
@Component({
|
|
156
|
+
selector: 'app-login-form',
|
|
157
|
+
templateUrl: './login-form.component.html',
|
|
158
|
+
styleUrls: ['./login-form.component.scss'],
|
|
159
|
+
providers: [
|
|
160
|
+
{ provide: DateAdapter, useClass: MultiParseDateFnsDateAdapter, deps: [MAT_DATE_LOCALE] },
|
|
161
|
+
{
|
|
162
|
+
provide: KLP_DATE_FORMATS,
|
|
163
|
+
useValue: (format?: string) => { // format is the format provided as an input to the date field.
|
|
164
|
+
const preferred = format ?? 'dd-MM-yyyy';
|
|
165
|
+
return {
|
|
166
|
+
parse: { // formats to parse.
|
|
167
|
+
// if the selected format fails to parse try all supported and all long locale formats.
|
|
168
|
+
dateInput: [preferred, 'dd-MM-yyyy', 'MM-dd-yyyy', 'PP', 'PPP', 'PPPP'],
|
|
169
|
+
},
|
|
170
|
+
display: { // format to display.
|
|
171
|
+
dateInput: preferred, // Display as prefered format.
|
|
172
|
+
monthLabel: 'MMM', // Month label in the date picker.
|
|
173
|
+
monthYearLabel: 'MMM yyyy', // month and year label in the date picker.
|
|
174
|
+
dateA11yLabel: 'MMM dd, yyyy', // Accessability variant for screen readers etc.
|
|
175
|
+
monthYearA11yLabel: 'MMMM yyyy', // same as above.
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
},
|
|
179
|
+
},
|
|
180
|
+
],
|
|
181
|
+
})
|
|
182
|
+
export class LoginFormComponent {
|
|
183
|
+
/** **/
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
Here we provide a function for `KLP_DATE_FORMATS` to the LoginFormComponent, you can also do this in a module for project wide config.
|
|
189
|
+
|
|
190
|
+
#### Angular 12
|
|
191
|
+
|
|
192
|
+
[NgxMatDatefnsDateAdapter](https://www.npmjs.com/package/ngx-mat-datefns-date-adapter) currently does not
|
|
193
|
+
support Angular 12, so localization is not possible.
|
|
194
|
+
|
|
195
|
+
#### Angular 13
|
|
196
|
+
|
|
197
|
+
Angular 13 introduces a new date adapter, @angular/material-date-fns-adapter.
|
|
42
198
|
|
|
43
|
-
TODO
|
|
199
|
+
TODO: work out how to use the `MatDateFnsAdapter`.
|