@rdlabo/ionic-angular-kit 22.0.0-3 → 22.0.0-4
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/docs/forms.md
CHANGED
|
@@ -14,7 +14,33 @@ import { KitIonicFormField } from '@rdlabo/ionic-angular-kit/forms';
|
|
|
14
14
|
export class ProfilePage {}
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
The adapter copies the first non-empty
|
|
17
|
+
The adapter copies the first non-empty explicit validation message to Ionic's `errorText` property for `ion-input`, `ion-textarea`, `ion-select`, `ion-checkbox`, `ion-radio-group`, and `ion-toggle`. When Angular's validator does not provide a message, the adapter derives a generic English message from the validation error `kind` and its constraint metadata. Built-in validators therefore need no message configuration:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
readonly profileForm = form(this.profile, (path) => {
|
|
21
|
+
required(path.name);
|
|
22
|
+
email(path.email);
|
|
23
|
+
maxLength(path.name, 200);
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Unknown custom error kinds fall back to `Enter a valid value.`. Keep business-rule failures outside field validation. An explicit validation message still takes precedence for compatibility, and an explicit `errorText` or `[errorText]` binding prevents the adapter from being instantiated.
|
|
28
|
+
|
|
29
|
+
Applications may replace the fallback resolver for localization without changing validator definitions:
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import type { ValidationError } from '@angular/forms/signals';
|
|
33
|
+
import { KIT_SIGNAL_FORM_ERROR_MESSAGE_RESOLVER } from '@rdlabo/ionic-angular-kit/forms';
|
|
34
|
+
|
|
35
|
+
export const appConfig: ApplicationConfig = {
|
|
36
|
+
providers: [
|
|
37
|
+
{
|
|
38
|
+
provide: KIT_SIGNAL_FORM_ERROR_MESSAGE_RESOLVER,
|
|
39
|
+
useValue: (error: ValidationError) => localizedMessageFor(error),
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
};
|
|
43
|
+
```
|
|
18
44
|
|
|
19
45
|
Install the state-class configuration once at application bootstrap:
|
|
20
46
|
|
|
@@ -1,16 +1,56 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
|
-
import { inject, ElementRef, Renderer2, effect, Directive } from '@angular/core';
|
|
2
|
+
import { InjectionToken, inject, ElementRef, Renderer2, effect, Directive } from '@angular/core';
|
|
3
3
|
import { FORM_FIELD, provideSignalFormsConfig } from '@angular/forms/signals';
|
|
4
4
|
|
|
5
|
+
const formatDate = (value) => (Number.isNaN(value.getTime()) ? 'a valid date' : value.toISOString().slice(0, 10));
|
|
6
|
+
/** Resolves Angular Signal Forms built-in validation errors to generic English messages. */
|
|
7
|
+
const kitDefaultSignalFormErrorMessage = (error) => {
|
|
8
|
+
switch (error.kind) {
|
|
9
|
+
case 'required':
|
|
10
|
+
return 'This field is required.';
|
|
11
|
+
case 'email':
|
|
12
|
+
return 'Enter a valid email address.';
|
|
13
|
+
case 'min':
|
|
14
|
+
return `Enter a value of at least ${error.min}.`;
|
|
15
|
+
case 'max':
|
|
16
|
+
return `Enter a value of no more than ${error.max}.`;
|
|
17
|
+
case 'minLength':
|
|
18
|
+
return `Enter at least ${error.minLength} characters.`;
|
|
19
|
+
case 'maxLength':
|
|
20
|
+
return `Enter no more than ${error.maxLength} characters.`;
|
|
21
|
+
case 'minDate':
|
|
22
|
+
return `Enter a date on or after ${formatDate(error.minDate)}.`;
|
|
23
|
+
case 'maxDate':
|
|
24
|
+
return `Enter a date on or before ${formatDate(error.maxDate)}.`;
|
|
25
|
+
case 'pattern':
|
|
26
|
+
return 'Enter a value in the required format.';
|
|
27
|
+
case 'parse':
|
|
28
|
+
return 'Enter a valid value.';
|
|
29
|
+
default:
|
|
30
|
+
return 'Enter a valid value.';
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
/** Override to localize or otherwise customize Signal Forms fallback messages. */
|
|
34
|
+
const KIT_SIGNAL_FORM_ERROR_MESSAGE_RESOLVER = new InjectionToken('KIT_SIGNAL_FORM_ERROR_MESSAGE_RESOLVER', {
|
|
35
|
+
providedIn: 'root',
|
|
36
|
+
factory: () => kitDefaultSignalFormErrorMessage,
|
|
37
|
+
});
|
|
38
|
+
|
|
5
39
|
class KitIonicFormField {
|
|
6
40
|
#field = inject(FORM_FIELD, { self: true });
|
|
7
41
|
#element = inject((ElementRef));
|
|
8
42
|
#renderer = inject(Renderer2);
|
|
43
|
+
#resolveErrorMessage = inject(KIT_SIGNAL_FORM_ERROR_MESSAGE_RESOLVER);
|
|
9
44
|
constructor() {
|
|
10
45
|
effect(() => {
|
|
11
46
|
const message = this.#field
|
|
12
47
|
.errors()
|
|
13
|
-
.map((error) =>
|
|
48
|
+
.map((error) => {
|
|
49
|
+
if (typeof error.message === 'string' && error.message.trim().length > 0) {
|
|
50
|
+
return error.message;
|
|
51
|
+
}
|
|
52
|
+
return this.#resolveErrorMessage(error);
|
|
53
|
+
})
|
|
14
54
|
.find((candidate) => typeof candidate === 'string' && candidate.trim().length > 0);
|
|
15
55
|
this.#renderer.setProperty(this.#element.nativeElement, 'errorText', message);
|
|
16
56
|
});
|
|
@@ -53,5 +93,5 @@ const provideKitIonicSignalForms = () => provideSignalFormsConfig({
|
|
|
53
93
|
* Generated bundle index. Do not edit.
|
|
54
94
|
*/
|
|
55
95
|
|
|
56
|
-
export { KitIonicFormField, provideKitIonicSignalForms };
|
|
96
|
+
export { KIT_SIGNAL_FORM_ERROR_MESSAGE_RESOLVER, KitIonicFormField, kitDefaultSignalFormErrorMessage, provideKitIonicSignalForms };
|
|
57
97
|
//# sourceMappingURL=rdlabo-ionic-angular-kit-forms.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rdlabo-ionic-angular-kit-forms.mjs","sources":["../../../projects/kit/forms/src/kit-ionic-form-field.ts","../../../projects/kit/forms/src/provide-kit-ionic-signal-forms.ts","../../../projects/kit/forms/src/rdlabo-ionic-angular-kit-forms.ts"],"sourcesContent":["import { Directive, effect, ElementRef, inject, Renderer2 } from '@angular/core';\nimport { FORM_FIELD } from '@angular/forms/signals';\n\n@Directive({\n // Angular's FormField owns [formField]. This sibling directive only adapts its validation message to Ionic.\n selector: `\n ion-input[formField]:not([errorText]),\n ion-textarea[formField]:not([errorText]),\n ion-select[formField]:not([errorText]),\n ion-checkbox[formField]:not([errorText]),\n ion-radio-group[formField]:not([errorText]),\n ion-toggle[formField]:not([errorText])\n `,\n})\nexport class KitIonicFormField {\n readonly #field = inject(FORM_FIELD, { self: true });\n readonly #element = inject(ElementRef<HTMLElement>);\n readonly #renderer = inject(Renderer2);\n\n constructor() {\n effect(() => {\n const message = this.#field\n .errors()\n .map((error) => error.message)\n .find((candidate): candidate is string => typeof candidate === 'string' && candidate.trim().length > 0);\n this.#renderer.setProperty(this.#element.nativeElement, 'errorText', message);\n });\n }\n}\n","import { provideSignalFormsConfig } from '@angular/forms/signals';\n\n/** Adds Angular and Ionic validity state classes to Signal Forms controls. */\nexport const provideKitIonicSignalForms = () =>\n provideSignalFormsConfig({\n classes: {\n 'ng-dirty': ({ state }) => state().dirty(),\n 'ng-invalid': ({ state }) => state().invalid(),\n 'ng-pending': ({ state }) => state().pending(),\n 'ng-pristine': ({ state }) => !state().dirty(),\n 'ng-touched': ({ state }) => state().touched(),\n 'ng-untouched': ({ state }) => !state().touched(),\n 'ng-valid': ({ state }) => state().valid(),\n 'ion-invalid': ({ state }) => state().invalid(),\n 'ion-touched': ({ state }) => state().touched(),\n 'ion-valid': ({ state }) => state().valid(),\n },\n });\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"rdlabo-ionic-angular-kit-forms.mjs","sources":["../../../projects/kit/forms/src/kit-signal-form-error-message.ts","../../../projects/kit/forms/src/kit-ionic-form-field.ts","../../../projects/kit/forms/src/provide-kit-ionic-signal-forms.ts","../../../projects/kit/forms/src/rdlabo-ionic-angular-kit-forms.ts"],"sourcesContent":["import { InjectionToken } from '@angular/core';\nimport type {\n MaxLengthValidationError,\n MaxValidationError,\n MinLengthValidationError,\n MinValidationError,\n ValidationError,\n} from '@angular/forms/signals';\n\nexport type KitSignalFormErrorMessageResolver = (error: ValidationError) => string | undefined;\n\nconst formatDate = (value: Date): string => (Number.isNaN(value.getTime()) ? 'a valid date' : value.toISOString().slice(0, 10));\n\n/** Resolves Angular Signal Forms built-in validation errors to generic English messages. */\nexport const kitDefaultSignalFormErrorMessage: KitSignalFormErrorMessageResolver = (error) => {\n switch (error.kind) {\n case 'required':\n return 'This field is required.';\n case 'email':\n return 'Enter a valid email address.';\n case 'min':\n return `Enter a value of at least ${(error as MinValidationError).min}.`;\n case 'max':\n return `Enter a value of no more than ${(error as MaxValidationError).max}.`;\n case 'minLength':\n return `Enter at least ${(error as MinLengthValidationError).minLength} characters.`;\n case 'maxLength':\n return `Enter no more than ${(error as MaxLengthValidationError).maxLength} characters.`;\n case 'minDate':\n return `Enter a date on or after ${formatDate((error as ValidationError & { minDate: Date }).minDate)}.`;\n case 'maxDate':\n return `Enter a date on or before ${formatDate((error as ValidationError & { maxDate: Date }).maxDate)}.`;\n case 'pattern':\n return 'Enter a value in the required format.';\n case 'parse':\n return 'Enter a valid value.';\n default:\n return 'Enter a valid value.';\n }\n};\n\n/** Override to localize or otherwise customize Signal Forms fallback messages. */\nexport const KIT_SIGNAL_FORM_ERROR_MESSAGE_RESOLVER = new InjectionToken<KitSignalFormErrorMessageResolver>(\n 'KIT_SIGNAL_FORM_ERROR_MESSAGE_RESOLVER',\n {\n providedIn: 'root',\n factory: () => kitDefaultSignalFormErrorMessage,\n },\n);\n","import { Directive, effect, ElementRef, inject, Renderer2 } from '@angular/core';\nimport { FORM_FIELD } from '@angular/forms/signals';\nimport { KIT_SIGNAL_FORM_ERROR_MESSAGE_RESOLVER } from './kit-signal-form-error-message';\n\n@Directive({\n // Angular's FormField owns [formField]. This sibling directive only adapts its validation message to Ionic.\n selector: `\n ion-input[formField]:not([errorText]),\n ion-textarea[formField]:not([errorText]),\n ion-select[formField]:not([errorText]),\n ion-checkbox[formField]:not([errorText]),\n ion-radio-group[formField]:not([errorText]),\n ion-toggle[formField]:not([errorText])\n `,\n})\nexport class KitIonicFormField {\n readonly #field = inject(FORM_FIELD, { self: true });\n readonly #element = inject(ElementRef<HTMLElement>);\n readonly #renderer = inject(Renderer2);\n readonly #resolveErrorMessage = inject(KIT_SIGNAL_FORM_ERROR_MESSAGE_RESOLVER);\n\n constructor() {\n effect(() => {\n const message = this.#field\n .errors()\n .map((error) => {\n if (typeof error.message === 'string' && error.message.trim().length > 0) {\n return error.message;\n }\n return this.#resolveErrorMessage(error);\n })\n .find((candidate): candidate is string => typeof candidate === 'string' && candidate.trim().length > 0);\n this.#renderer.setProperty(this.#element.nativeElement, 'errorText', message);\n });\n }\n}\n","import { provideSignalFormsConfig } from '@angular/forms/signals';\n\n/** Adds Angular and Ionic validity state classes to Signal Forms controls. */\nexport const provideKitIonicSignalForms = () =>\n provideSignalFormsConfig({\n classes: {\n 'ng-dirty': ({ state }) => state().dirty(),\n 'ng-invalid': ({ state }) => state().invalid(),\n 'ng-pending': ({ state }) => state().pending(),\n 'ng-pristine': ({ state }) => !state().dirty(),\n 'ng-touched': ({ state }) => state().touched(),\n 'ng-untouched': ({ state }) => !state().touched(),\n 'ng-valid': ({ state }) => state().valid(),\n 'ion-invalid': ({ state }) => state().invalid(),\n 'ion-touched': ({ state }) => state().touched(),\n 'ion-valid': ({ state }) => state().valid(),\n },\n });\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;AAWA,MAAM,UAAU,GAAG,CAAC,KAAW,MAAc,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,GAAG,cAAc,GAAG,KAAK,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AAE/H;AACO,MAAM,gCAAgC,GAAsC,CAAC,KAAK,KAAI;AAC3F,IAA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,OAAO,EAAE;AAC9C,QAAA,YAAY,EAAE,CAAC,EAAE,KAAK,EAAE,KAAK,KAAK,EAAE,CAAC,OAAO,EAAE;AAC9C,QAAA,aAAa,EAAE,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE;AAC9C,QAAA,YAAY,EAAE,CAAC,EAAE,KAAK,EAAE,KAAK,KAAK,EAAE,CAAC,OAAO,EAAE;AAC9C,QAAA,cAAc,EAAE,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,CAAC,OAAO,EAAE;AACjD,QAAA,UAAU,EAAE,CAAC,EAAE,KAAK,EAAE,KAAK,KAAK,EAAE,CAAC,KAAK,EAAE;AAC1C,QAAA,aAAa,EAAE,CAAC,EAAE,KAAK,EAAE,KAAK,KAAK,EAAE,CAAC,OAAO,EAAE;AAC/C,QAAA,aAAa,EAAE,CAAC,EAAE,KAAK,EAAE,KAAK,KAAK,EAAE,CAAC,OAAO,EAAE;AAC/C,QAAA,WAAW,EAAE,CAAC,EAAE,KAAK,EAAE,KAAK,KAAK,EAAE,CAAC,KAAK,EAAE;AAC5C,KAAA;AACF,CAAA;;ACjBH;;AAEG;;;;"}
|
package/package.json
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
|
+
import { InjectionToken } from '@angular/core';
|
|
3
|
+
import { ValidationError } from '@angular/forms/signals';
|
|
2
4
|
|
|
3
5
|
declare class KitIonicFormField {
|
|
4
6
|
#private;
|
|
@@ -7,7 +9,14 @@ declare class KitIonicFormField {
|
|
|
7
9
|
static ɵdir: i0.ɵɵDirectiveDeclaration<KitIonicFormField, " ion-input[formField]:not([errorText]), ion-textarea[formField]:not([errorText]), ion-select[formField]:not([errorText]), ion-checkbox[formField]:not([errorText]), ion-radio-group[formField]:not([errorText]), ion-toggle[formField]:not([errorText]) ", never, {}, {}, never, never, true, never>;
|
|
8
10
|
}
|
|
9
11
|
|
|
12
|
+
type KitSignalFormErrorMessageResolver = (error: ValidationError) => string | undefined;
|
|
13
|
+
/** Resolves Angular Signal Forms built-in validation errors to generic English messages. */
|
|
14
|
+
declare const kitDefaultSignalFormErrorMessage: KitSignalFormErrorMessageResolver;
|
|
15
|
+
/** Override to localize or otherwise customize Signal Forms fallback messages. */
|
|
16
|
+
declare const KIT_SIGNAL_FORM_ERROR_MESSAGE_RESOLVER: InjectionToken<KitSignalFormErrorMessageResolver>;
|
|
17
|
+
|
|
10
18
|
/** Adds Angular and Ionic validity state classes to Signal Forms controls. */
|
|
11
19
|
declare const provideKitIonicSignalForms: () => i0.Provider[];
|
|
12
20
|
|
|
13
|
-
export { KitIonicFormField, provideKitIonicSignalForms };
|
|
21
|
+
export { KIT_SIGNAL_FORM_ERROR_MESSAGE_RESOLVER, KitIonicFormField, kitDefaultSignalFormErrorMessage, provideKitIonicSignalForms };
|
|
22
|
+
export type { KitSignalFormErrorMessageResolver };
|