maestral 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +127 -2
- package/fesm2022/maestral-button.mjs +83 -0
- package/fesm2022/maestral-button.mjs.map +1 -0
- package/fesm2022/maestral-error-summary.mjs +187 -0
- package/fesm2022/maestral-error-summary.mjs.map +1 -0
- package/fesm2022/maestral-field.mjs +201 -0
- package/fesm2022/maestral-field.mjs.map +1 -0
- package/fesm2022/maestral-input.mjs +90 -0
- package/fesm2022/maestral-input.mjs.map +1 -0
- package/fesm2022/maestral.mjs +4 -0
- package/fesm2022/maestral.mjs.map +1 -0
- package/package.json +58 -3
- package/styles/base.css +13 -0
- package/styles/components/button.css +104 -0
- package/styles/components/error-summary.css +57 -0
- package/styles/components/field.css +31 -0
- package/styles/components/input.css +68 -0
- package/styles/maestral.css +11 -0
- package/styles/tokens.css +100 -0
- package/types/maestral-button.d.ts +20 -0
- package/types/maestral-error-summary.d.ts +56 -0
- package/types/maestral-field.d.ts +101 -0
- package/types/maestral-input.d.ts +34 -0
- package/types/maestral.d.ts +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Anamaria Orsulic
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,128 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/AnamariaMarjanovic/maestral/main/docs/assets/logo.svg" width="72" height="72" alt="">
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
<h1 align="center">Maestral</h1>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
Accessible Angular components on native elements, with forms and theming at the core.
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
The maestral is the steady summer wind of the Adriatic: reliable, pleasant and
|
|
14
|
+
never in the way. That is the goal for this library too.
|
|
15
|
+
|
|
16
|
+
- **Native elements first.** `<button msButton>` is a real button, so keyboard
|
|
17
|
+
support, forms and screen readers work without extra code.
|
|
18
|
+
- **Forms that help people.** Labels, hints and errors are wired up for you.
|
|
19
|
+
Errors appear when the user leaves a field, never while they are typing,
|
|
20
|
+
and an error summary takes them to each problem after a failed submit.
|
|
21
|
+
- **Theming through tokens.** Every color, radius and spacing value is a CSS
|
|
22
|
+
custom property. Light and dark mode are built in, and every color pair
|
|
23
|
+
meets WCAG 2.2 AA.
|
|
24
|
+
|
|
25
|
+
## Requirements
|
|
26
|
+
|
|
27
|
+
Angular 22.2 or later, with Signal Forms (`@angular/forms/signals`) for
|
|
28
|
+
automatic validation errors.
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
npm install maestral
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Add the stylesheet to your app's global styles in `angular.json`:
|
|
37
|
+
|
|
38
|
+
```json
|
|
39
|
+
"styles": [
|
|
40
|
+
"node_modules/maestral/styles/maestral.css",
|
|
41
|
+
"src/styles.css"
|
|
42
|
+
]
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
Each component has its own entry point, so your app only ships what it uses.
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { MsButton } from 'maestral/button';
|
|
51
|
+
import { MsErrorSummary } from 'maestral/error-summary';
|
|
52
|
+
import { MsField } from 'maestral/field';
|
|
53
|
+
import { MsInput } from 'maestral/input';
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
A complete accessible form:
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
readonly contact = signal({ name: '', email: '' });
|
|
60
|
+
|
|
61
|
+
readonly contactForm = form(this.contact, (path) => {
|
|
62
|
+
required(path.name, { message: 'Enter your full name' });
|
|
63
|
+
required(path.email);
|
|
64
|
+
email(path.email);
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
```html
|
|
69
|
+
<form novalidate (submit)="onSubmit($event)">
|
|
70
|
+
<ms-error-summary [form]="contactForm" />
|
|
71
|
+
|
|
72
|
+
<ms-field label="Full name" hint="As it appears on your ID.">
|
|
73
|
+
<input msInput [formField]="contactForm.name" autocomplete="name" />
|
|
74
|
+
</ms-field>
|
|
75
|
+
|
|
76
|
+
<ms-field label="Email">
|
|
77
|
+
<input msInput type="email" [formField]="contactForm.email" autocomplete="email" />
|
|
78
|
+
</ms-field>
|
|
79
|
+
|
|
80
|
+
<button msButton type="submit">Send</button>
|
|
81
|
+
</form>
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Labels, `aria-describedby`, `aria-invalid`, `aria-required`, error messages,
|
|
85
|
+
the error summary and focus management all follow from this markup.
|
|
86
|
+
|
|
87
|
+
## Components
|
|
88
|
+
|
|
89
|
+
| Entry point | Exports |
|
|
90
|
+
| ----------------------- | ----------------------------------------------- |
|
|
91
|
+
| `maestral/button` | `MsButton` |
|
|
92
|
+
| `maestral/field` | `MsField`, `provideMsErrorMessages()` |
|
|
93
|
+
| `maestral/input` | `MsInput` |
|
|
94
|
+
| `maestral/error-summary`| `MsErrorSummary` |
|
|
95
|
+
|
|
96
|
+
## Theming
|
|
97
|
+
|
|
98
|
+
Override any semantic token. Maestral's styles live in cascade layers, so your
|
|
99
|
+
styles always win without `!important`:
|
|
100
|
+
|
|
101
|
+
```css
|
|
102
|
+
:root {
|
|
103
|
+
--ms-color-primary: oklch(52% 0.14 160);
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Dark mode follows the operating system. Force a scheme with
|
|
108
|
+
`data-ms-theme="light"` or `data-ms-theme="dark"` on any element.
|
|
109
|
+
|
|
110
|
+
## Translating error messages
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
providers: [
|
|
114
|
+
provideMsErrorMessages({
|
|
115
|
+
required: () => 'Polje je obavezno',
|
|
116
|
+
}),
|
|
117
|
+
];
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Links
|
|
121
|
+
|
|
122
|
+
- [Source and documentation](https://github.com/AnamariaMarjanovic/maestral)
|
|
123
|
+
- [Changelog](https://github.com/AnamariaMarjanovic/maestral/blob/main/CHANGELOG.md)
|
|
124
|
+
- [Design decisions](https://github.com/AnamariaMarjanovic/maestral/tree/main/docs/decisions)
|
|
125
|
+
|
|
126
|
+
## License
|
|
127
|
+
|
|
128
|
+
MIT © Anamaria Oršulić
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import * as i0 from "@angular/core";
|
|
2
|
+
import { Directive, input } from "@angular/core";
|
|
3
|
+
var MsButton = class MsButton {
|
|
4
|
+
variant = input("primary", ...ngDevMode ? [{ debugName: "variant" }] : /* istanbul ignore next */ []);
|
|
5
|
+
size = input("md", ...ngDevMode ? [{ debugName: "size" }] : /* istanbul ignore next */ []);
|
|
6
|
+
static ɵfac = i0.ɵɵngDeclareFactory({
|
|
7
|
+
minVersion: "12.0.0",
|
|
8
|
+
version: "22.2.4",
|
|
9
|
+
ngImport: i0,
|
|
10
|
+
type: MsButton,
|
|
11
|
+
deps: [],
|
|
12
|
+
target: i0.ɵɵFactoryTarget.Directive
|
|
13
|
+
});
|
|
14
|
+
static ɵdir = i0.ɵɵngDeclareDirective({
|
|
15
|
+
minVersion: "17.1.0",
|
|
16
|
+
version: "22.2.4",
|
|
17
|
+
type: MsButton,
|
|
18
|
+
isStandalone: true,
|
|
19
|
+
selector: "button[msButton], a[msButton]",
|
|
20
|
+
inputs: {
|
|
21
|
+
variant: {
|
|
22
|
+
classPropertyName: "variant",
|
|
23
|
+
publicName: "variant",
|
|
24
|
+
isSignal: true,
|
|
25
|
+
isRequired: false,
|
|
26
|
+
transformFunction: null
|
|
27
|
+
},
|
|
28
|
+
size: {
|
|
29
|
+
classPropertyName: "size",
|
|
30
|
+
publicName: "size",
|
|
31
|
+
isSignal: true,
|
|
32
|
+
isRequired: false,
|
|
33
|
+
transformFunction: null
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
host: {
|
|
37
|
+
properties: {
|
|
38
|
+
"attr.data-variant": "variant()",
|
|
39
|
+
"attr.data-size": "size()"
|
|
40
|
+
},
|
|
41
|
+
classAttribute: "ms-button"
|
|
42
|
+
},
|
|
43
|
+
ngImport: i0
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
i0.ɵɵngDeclareClassMetadata({
|
|
47
|
+
minVersion: "12.0.0",
|
|
48
|
+
version: "22.2.4",
|
|
49
|
+
ngImport: i0,
|
|
50
|
+
type: MsButton,
|
|
51
|
+
decorators: [{
|
|
52
|
+
type: Directive,
|
|
53
|
+
args: [{
|
|
54
|
+
selector: "button[msButton], a[msButton]",
|
|
55
|
+
host: {
|
|
56
|
+
class: "ms-button",
|
|
57
|
+
"[attr.data-variant]": "variant()",
|
|
58
|
+
"[attr.data-size]": "size()"
|
|
59
|
+
}
|
|
60
|
+
}]
|
|
61
|
+
}],
|
|
62
|
+
propDecorators: {
|
|
63
|
+
variant: [{
|
|
64
|
+
type: i0.Input,
|
|
65
|
+
args: [{
|
|
66
|
+
isSignal: true,
|
|
67
|
+
alias: "variant",
|
|
68
|
+
required: false
|
|
69
|
+
}]
|
|
70
|
+
}],
|
|
71
|
+
size: [{
|
|
72
|
+
type: i0.Input,
|
|
73
|
+
args: [{
|
|
74
|
+
isSignal: true,
|
|
75
|
+
alias: "size",
|
|
76
|
+
required: false
|
|
77
|
+
}]
|
|
78
|
+
}]
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
export { MsButton };
|
|
82
|
+
|
|
83
|
+
//# sourceMappingURL=maestral-button.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"maestral-button.mjs","names":[],"sources":["../../../projects/maestral/button/button.ts"],"sourcesContent":["import { Directive, input } from '@angular/core';\n\nexport type MsButtonVariant = 'primary' | 'secondary' | 'ghost' | 'danger';\nexport type MsButtonSize = 'sm' | 'md' | 'lg';\n\n@Directive({\n selector: 'button[msButton], a[msButton]',\n host: {\n class: 'ms-button',\n '[attr.data-variant]': 'variant()',\n '[attr.data-size]': 'size()',\n },\n})\nexport class MsButton {\n readonly variant = input<MsButtonVariant>('primary');\n readonly size = input<MsButtonSize>('md');\n}"],"mappings":";;AAaA,IAAa,WAAb,MAAa,SAAQ;CACV,UAAU,MAAuB,WAAU,GAAA,YAAA,CAAA,EAAA,WAAA,UAAA,CAAA,+BAAA,CAAA,CAAA;CAC3C,OAAO,MAAoB,MAAK,GAAA,YAAA,CAAA,EAAA,WAAA,OAAA,CAAA,+BAAA,CAAA,CAAA;CAF9B,OAAA,OAAA,GAAA,mBAAA;EAAA,YAAA;EAAA,SAAA;EAAA,UAAA;EAAA,MAAA;EAAQ,MAAA,CAAA;EAAA,QAAA,GAAA,gBAAA;CAAA,CAAA;CAAR,OAAA,OAAA,GAAA,qBAAA;EAAA,YAAA;EAAA,SAAA;EAAA,MAAA;EAAQ,cAAA;EAAA,UAAA;EAAA,QAAA;GAAA,SAAA;IAAA,mBAAA;IAAA,YAAA;IAAA,UAAA;IAAA,YAAA;IAAA,mBAAA;GAAA;GAAA,MAAA;IAAA,mBAAA;IAAA,YAAA;IAAA,UAAA;IAAA,YAAA;IAAA,mBAAA;GAAA;EAAA;EAAA,MAAA;GAAA,YAAA;IAAA,qBAAA;IAAA,kBAAA;GAAA;GAAA,gBAAA;EAAA;EAAA,UAAA;CAAA,CAAA;;AAAR,GAAA,yBAAA;CAAA,YAAA;CAAA,SAAA;CAAA,UAAA;CAAA,MAAA;CAAQ,YAAA,CAAA;EARpB,MAAA;EAAU,MAAA,CAAA;GACT,UAAU;GACV,MAAM;IACJ,OAAO;IACP,uBAAuB;IACvB,oBAAoB;GACrB;EACF,CAAA"}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import * as i0 from "@angular/core";
|
|
2
|
+
import { Component, DestroyRef, ElementRef, Injector, afterNextRender, computed, inject, input, signal, viewChild } from "@angular/core";
|
|
3
|
+
import { MS_ERROR_MESSAGES, msResolveErrorMessage } from "maestral/field";
|
|
4
|
+
let nextId = 0;
|
|
5
|
+
var MsErrorSummary = class MsErrorSummary {
|
|
6
|
+
form = input.required(...ngDevMode ? [{ debugName: "form" }] : /* istanbul ignore next */ []);
|
|
7
|
+
title = input("There is a problem", ...ngDevMode ? [{ debugName: "title" }] : /* istanbul ignore next */ []);
|
|
8
|
+
messages = inject(MS_ERROR_MESSAGES);
|
|
9
|
+
host = inject(ElementRef);
|
|
10
|
+
injector = inject(Injector);
|
|
11
|
+
destroyRef = inject(DestroyRef);
|
|
12
|
+
titleId = `ms-error-summary-${nextId++}-title`;
|
|
13
|
+
summary = viewChild("summary", ...ngDevMode ? [{ debugName: "summary" }] : /* istanbul ignore next */ []);
|
|
14
|
+
attempted = signal(false, ...ngDevMode ? [{ debugName: "attempted" }] : /* istanbul ignore next */ []);
|
|
15
|
+
items = computed(() => {
|
|
16
|
+
const errors = this.form()().errorSummary();
|
|
17
|
+
const seen = /* @__PURE__ */ new Set();
|
|
18
|
+
const items = [];
|
|
19
|
+
for (const error of errors) {
|
|
20
|
+
if (seen.has(error.fieldTree)) continue;
|
|
21
|
+
seen.add(error.fieldTree);
|
|
22
|
+
const element = error.fieldTree().formFieldBindings()[0]?.element ?? null;
|
|
23
|
+
items.push({
|
|
24
|
+
key: items.length,
|
|
25
|
+
message: msResolveErrorMessage(error, this.messages),
|
|
26
|
+
targetId: element?.id || null,
|
|
27
|
+
element
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
return items;
|
|
31
|
+
}, ...ngDevMode ? [{ debugName: "items" }] : /* istanbul ignore next */ []);
|
|
32
|
+
visible = computed(() => this.attempted() && this.items().length > 0, ...ngDevMode ? [{ debugName: "visible" }] : /* istanbul ignore next */ []);
|
|
33
|
+
constructor() {
|
|
34
|
+
afterNextRender(() => {
|
|
35
|
+
const formElement = this.host.nativeElement.closest("form");
|
|
36
|
+
if (!formElement) return;
|
|
37
|
+
const onSubmit = () => this.onSubmitAttempt();
|
|
38
|
+
formElement.addEventListener("submit", onSubmit);
|
|
39
|
+
this.destroyRef.onDestroy(() => formElement.removeEventListener("submit", onSubmit));
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
onSubmitAttempt() {
|
|
43
|
+
this.attempted.set(true);
|
|
44
|
+
afterNextRender(() => this.summary()?.nativeElement.focus(), { injector: this.injector });
|
|
45
|
+
}
|
|
46
|
+
focusField(event, item) {
|
|
47
|
+
const element = item.element;
|
|
48
|
+
if (!element) return;
|
|
49
|
+
event.preventDefault();
|
|
50
|
+
(element.closest(".ms-field") ?? element).scrollIntoView({ block: "start" });
|
|
51
|
+
element.focus({ preventScroll: true });
|
|
52
|
+
}
|
|
53
|
+
static ɵfac = i0.ɵɵngDeclareFactory({
|
|
54
|
+
minVersion: "12.0.0",
|
|
55
|
+
version: "22.2.4",
|
|
56
|
+
ngImport: i0,
|
|
57
|
+
type: MsErrorSummary,
|
|
58
|
+
deps: [],
|
|
59
|
+
target: i0.ɵɵFactoryTarget.Component
|
|
60
|
+
});
|
|
61
|
+
static ɵcmp = i0.ɵɵngDeclareComponent({
|
|
62
|
+
minVersion: "17.0.0",
|
|
63
|
+
version: "22.2.4",
|
|
64
|
+
type: MsErrorSummary,
|
|
65
|
+
isStandalone: true,
|
|
66
|
+
selector: "ms-error-summary",
|
|
67
|
+
inputs: {
|
|
68
|
+
form: {
|
|
69
|
+
classPropertyName: "form",
|
|
70
|
+
publicName: "form",
|
|
71
|
+
isSignal: true,
|
|
72
|
+
isRequired: true,
|
|
73
|
+
transformFunction: null
|
|
74
|
+
},
|
|
75
|
+
title: {
|
|
76
|
+
classPropertyName: "title",
|
|
77
|
+
publicName: "title",
|
|
78
|
+
isSignal: true,
|
|
79
|
+
isRequired: false,
|
|
80
|
+
transformFunction: null
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
viewQueries: [{
|
|
84
|
+
propertyName: "summary",
|
|
85
|
+
first: true,
|
|
86
|
+
predicate: ["summary"],
|
|
87
|
+
descendants: true,
|
|
88
|
+
isSignal: true
|
|
89
|
+
}],
|
|
90
|
+
ngImport: i0,
|
|
91
|
+
template: `
|
|
92
|
+
@if (visible()) {
|
|
93
|
+
<section
|
|
94
|
+
#summary
|
|
95
|
+
class="ms-error-summary"
|
|
96
|
+
tabindex="-1"
|
|
97
|
+
[attr.aria-labelledby]="titleId"
|
|
98
|
+
>
|
|
99
|
+
<h2 class="ms-error-summary__title" [id]="titleId">{{ title() }}</h2>
|
|
100
|
+
<ul class="ms-error-summary__list">
|
|
101
|
+
@for (item of items(); track item.key) {
|
|
102
|
+
<li>
|
|
103
|
+
@if (item.targetId) {
|
|
104
|
+
<a
|
|
105
|
+
class="ms-error-summary__link"
|
|
106
|
+
[href]="'#' + item.targetId"
|
|
107
|
+
(click)="focusField($event, item)"
|
|
108
|
+
>{{ item.message }}</a
|
|
109
|
+
>
|
|
110
|
+
} @else {
|
|
111
|
+
{{ item.message }}
|
|
112
|
+
}
|
|
113
|
+
</li>
|
|
114
|
+
}
|
|
115
|
+
</ul>
|
|
116
|
+
</section>
|
|
117
|
+
}
|
|
118
|
+
`,
|
|
119
|
+
isInline: true
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
i0.ɵɵngDeclareClassMetadata({
|
|
123
|
+
minVersion: "12.0.0",
|
|
124
|
+
version: "22.2.4",
|
|
125
|
+
ngImport: i0,
|
|
126
|
+
type: MsErrorSummary,
|
|
127
|
+
decorators: [{
|
|
128
|
+
type: Component,
|
|
129
|
+
args: [{
|
|
130
|
+
selector: "ms-error-summary",
|
|
131
|
+
template: `
|
|
132
|
+
@if (visible()) {
|
|
133
|
+
<section
|
|
134
|
+
#summary
|
|
135
|
+
class="ms-error-summary"
|
|
136
|
+
tabindex="-1"
|
|
137
|
+
[attr.aria-labelledby]="titleId"
|
|
138
|
+
>
|
|
139
|
+
<h2 class="ms-error-summary__title" [id]="titleId">{{ title() }}</h2>
|
|
140
|
+
<ul class="ms-error-summary__list">
|
|
141
|
+
@for (item of items(); track item.key) {
|
|
142
|
+
<li>
|
|
143
|
+
@if (item.targetId) {
|
|
144
|
+
<a
|
|
145
|
+
class="ms-error-summary__link"
|
|
146
|
+
[href]="'#' + item.targetId"
|
|
147
|
+
(click)="focusField($event, item)"
|
|
148
|
+
>{{ item.message }}</a
|
|
149
|
+
>
|
|
150
|
+
} @else {
|
|
151
|
+
{{ item.message }}
|
|
152
|
+
}
|
|
153
|
+
</li>
|
|
154
|
+
}
|
|
155
|
+
</ul>
|
|
156
|
+
</section>
|
|
157
|
+
}
|
|
158
|
+
`
|
|
159
|
+
}]
|
|
160
|
+
}],
|
|
161
|
+
ctorParameters: () => [],
|
|
162
|
+
propDecorators: {
|
|
163
|
+
form: [{
|
|
164
|
+
type: i0.Input,
|
|
165
|
+
args: [{
|
|
166
|
+
isSignal: true,
|
|
167
|
+
alias: "form",
|
|
168
|
+
required: true
|
|
169
|
+
}]
|
|
170
|
+
}],
|
|
171
|
+
title: [{
|
|
172
|
+
type: i0.Input,
|
|
173
|
+
args: [{
|
|
174
|
+
isSignal: true,
|
|
175
|
+
alias: "title",
|
|
176
|
+
required: false
|
|
177
|
+
}]
|
|
178
|
+
}],
|
|
179
|
+
summary: [{
|
|
180
|
+
type: i0.ViewChild,
|
|
181
|
+
args: ["summary", { isSignal: true }]
|
|
182
|
+
}]
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
export { MsErrorSummary };
|
|
186
|
+
|
|
187
|
+
//# sourceMappingURL=maestral-error-summary.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"maestral-error-summary.mjs","names":[],"sources":["../../../projects/maestral/error-summary/error-summary.ts"],"sourcesContent":["import {\n Component,\n DestroyRef,\n ElementRef,\n Injector,\n afterNextRender,\n computed,\n inject,\n input,\n signal,\n viewChild,\n} from '@angular/core';\nimport type { FieldTree } from '@angular/forms/signals';\nimport { MS_ERROR_MESSAGES, msResolveErrorMessage } from 'maestral/field';\n\ninterface MsErrorSummaryItem {\n readonly key: number;\n readonly message: string;\n readonly targetId: string | null;\n /** The element the field is bound to with `[formField]`, if any. */\n readonly element: HTMLElement | null;\n}\n\nlet nextId = 0;\n\n/**\n * Lists every invalid field after the user tries to submit a form, and\n * moves focus to the list so screen reader users hear about the problem\n * straight away. Each item links to its field.\n *\n * Place it at the top of the `<form>` it summarises:\n *\n * ```html\n * <form novalidate (submit)=\"onSubmit($event)\">\n * <ms-error-summary [form]=\"contactForm\" />\n * ...\n * </form>\n * ```\n */\n@Component({\n selector: 'ms-error-summary',\n template: `\n @if (visible()) {\n <section\n #summary\n class=\"ms-error-summary\"\n tabindex=\"-1\"\n [attr.aria-labelledby]=\"titleId\"\n >\n <h2 class=\"ms-error-summary__title\" [id]=\"titleId\">{{ title() }}</h2>\n <ul class=\"ms-error-summary__list\">\n @for (item of items(); track item.key) {\n <li>\n @if (item.targetId) {\n <a\n class=\"ms-error-summary__link\"\n [href]=\"'#' + item.targetId\"\n (click)=\"focusField($event, item)\"\n >{{ item.message }}</a\n >\n } @else {\n {{ item.message }}\n }\n </li>\n }\n </ul>\n </section>\n }\n `,\n})\nexport class MsErrorSummary {\n /** The Signal Forms field tree of the form to summarise. */\n readonly form = input.required<FieldTree<unknown>>();\n readonly title = input('There is a problem');\n\n private readonly messages = inject(MS_ERROR_MESSAGES);\n private readonly host = inject<ElementRef<HTMLElement>>(ElementRef);\n private readonly injector = inject(Injector);\n private readonly destroyRef = inject(DestroyRef);\n\n protected readonly titleId = `ms-error-summary-${nextId++}-title`;\n private readonly summary = viewChild<ElementRef<HTMLElement>>('summary');\n\n /** Becomes true on the first submit attempt. */\n private readonly attempted = signal(false);\n\n /** One item per invalid field, in form order, with that field's first error. */\n protected readonly items = computed<MsErrorSummaryItem[]>(() => {\n const errors = this.form()().errorSummary();\n const seen = new Set<unknown>();\n const items: MsErrorSummaryItem[] = [];\n for (const error of errors) {\n if (seen.has(error.fieldTree)) {\n continue;\n }\n seen.add(error.fieldTree);\n const element = error.fieldTree().formFieldBindings()[0]?.element ?? null;\n items.push({\n key: items.length,\n message: msResolveErrorMessage(error, this.messages),\n targetId: element?.id || null,\n element,\n });\n }\n return items;\n });\n\n protected readonly visible = computed(() => this.attempted() && this.items().length > 0);\n\n constructor() {\n afterNextRender(() => {\n const formElement = this.host.nativeElement.closest('form');\n if (!formElement) {\n return;\n }\n const onSubmit = () => this.onSubmitAttempt();\n formElement.addEventListener('submit', onSubmit);\n this.destroyRef.onDestroy(() => formElement.removeEventListener('submit', onSubmit));\n });\n }\n\n private onSubmitAttempt(): void {\n this.attempted.set(true);\n // Wait until the summary is rendered, then move focus to it.\n afterNextRender(() => this.summary()?.nativeElement.focus(), { injector: this.injector });\n }\n\n protected focusField(event: Event, item: MsErrorSummaryItem): void {\n const element = item.element;\n if (!element) {\n return;\n }\n event.preventDefault();\n // Scroll to the whole field so the label and hint are visible too.\n (element.closest('.ms-field') ?? element).scrollIntoView({ block: 'start' });\n element.focus({ preventScroll: true });\n }\n}\n"],"mappings":";;;AAuBA,IAAI,SAAS;AA+Cb,IAAa,iBAAb,MAAa,eAAc;CAEhB,OAAO,MAAM,SAA8B,GAAA,YAAA,CAAA,EAAA,WAAA,OAAA,CAAA,+BAAA,CAAA,CAAA;CAC3C,QAAQ,MAAM,sBAAqB,GAAA,YAAA,CAAA,EAAA,WAAA,QAAA,CAAA,+BAAA,CAAA,CAAA;CAE3B,WAAW,OAAO,iBAAiB;CACnC,OAAO,OAAgC,UAAU;CACjD,WAAW,OAAO,QAAQ;CAC1B,aAAa,OAAO,UAAU;CAE5B,UAAU,oBAAoB,SAAQ;CACxC,UAAU,UAAmC,WAAU,GAAA,YAAA,CAAA,EAAA,WAAA,UAAA,CAAA,+BAAA,CAAA,CAAA;CAGvD,YAAY,OAAO,OAAM,GAAA,YAAA,CAAA,EAAA,WAAA,YAAA,CAAA,+BAAA,CAAA,CAAA;CAGvB,QAAQ,eAAoC;EAC7D,MAAM,SAAS,KAAK,KAAI,CAAE,CAAA,CAAE,CAAC,aAAY;EACzC,MAAM,uBAAO,IAAI,IAAG;EACpB,MAAM,QAA8B,CAAA;EACpC,KAAK,MAAM,SAAS,QAAQ;GAC1B,IAAI,KAAK,IAAI,MAAM,SAAS,GAC1B;GAEF,KAAK,IAAI,MAAM,SAAS;GACxB,MAAM,UAAU,MAAM,UAAS,CAAE,CAAC,kBAAiB,CAAE,CAAC,EAAE,EAAE,WAAW;GACrE,MAAM,KAAK;IACT,KAAK,MAAM;IACX,SAAS,sBAAsB,OAAO,KAAK,QAAQ;IACnD,UAAU,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AA,YAAA;IAAA,mBAAA;GAAA;EAAA;EAAA,aAAA,CAAA;GAAA,cAAA;GAAA,OAAA;GAAA,WAAA,CAAA,SAAA;GAAA,aAAA;GAAA,UAAA;EAAA,CAAA;EAAA,UAAA;EAAA,UA7Bf;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA2BT,UAAA;CAAA,CAAA;;AAEU,GAAA,yBAAA;CAAA,YAAA;CAAA,SAAA;CAAA,UAAA;CAAA,MAAA;CAAc,YAAA,CAAA;EA/B1B,MAAA;EAAU,MAAA,CAAA;GACT,UAAU;GACV,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA4BX,CAAA;CAY+D,CAAA;CAAA,sBAAA,CAAA;CAAA,gBAAA;EAAA,MAAA,CAAA;GAAA,MAAA,GAAA;GAAA,MAAA,CAAA;IAAA,UAAA;IAAA,OAAA;IAAA,UAAA;GAAA,CAAA;EAAA,CAAA;EAAA,OAAA,CAAA;GAAA,MAAA,GAAA;GAAA,MAAA,CAAA;IAAA,UAAA;IAAA,OAAA;IAAA,UAAA;GAAA,CAAA;EAAA,CAAA;EAAA,SAAA,CAAA;GAAA,MAAA,GAAA;GAAA,MAAA,CAAA,WAAS,EAAA,UAAA,KAAA,CAAA;EAAA,CAAA;CAAA;AAAA,CAAA"}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import * as i0 from "@angular/core";
|
|
2
|
+
import { Component, InjectionToken, booleanAttribute, computed, inject, input, signal } from "@angular/core";
|
|
3
|
+
function detail(error, key) {
|
|
4
|
+
return error[key];
|
|
5
|
+
}
|
|
6
|
+
const MS_DEFAULT_ERROR_MESSAGES = {
|
|
7
|
+
required: () => "This field is required",
|
|
8
|
+
email: () => "Enter an email address in the correct format, like name@example.com",
|
|
9
|
+
minLength: (e) => `Enter at least ${detail(e, "minLength")} characters`,
|
|
10
|
+
maxLength: (e) => `Enter ${detail(e, "maxLength")} characters or fewer`,
|
|
11
|
+
min: (e) => `Enter a value of ${detail(e, "min")} or more`,
|
|
12
|
+
max: (e) => `Enter a value of ${detail(e, "max")} or less`,
|
|
13
|
+
pattern: () => "Enter a value in the correct format"
|
|
14
|
+
};
|
|
15
|
+
const MS_FALLBACK_ERROR_MESSAGE = "Check this field";
|
|
16
|
+
const MS_ERROR_MESSAGES = new InjectionToken("MS_ERROR_MESSAGES", {
|
|
17
|
+
providedIn: "root",
|
|
18
|
+
factory: () => MS_DEFAULT_ERROR_MESSAGES
|
|
19
|
+
});
|
|
20
|
+
function provideMsErrorMessages(messages) {
|
|
21
|
+
return {
|
|
22
|
+
provide: MS_ERROR_MESSAGES,
|
|
23
|
+
useValue: {
|
|
24
|
+
...MS_DEFAULT_ERROR_MESSAGES,
|
|
25
|
+
...messages
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function msResolveErrorMessage(error, messages) {
|
|
30
|
+
return error.message ?? messages[error.kind]?.(error) ?? "Check this field";
|
|
31
|
+
}
|
|
32
|
+
let nextId = 0;
|
|
33
|
+
var MsField = class MsField {
|
|
34
|
+
label = input.required(...ngDevMode ? [{ debugName: "label" }] : /* istanbul ignore next */ []);
|
|
35
|
+
hint = input(...ngDevMode ? [void 0, { debugName: "hint" }] : /* istanbul ignore next */ []);
|
|
36
|
+
error = input(...ngDevMode ? [void 0, { debugName: "error" }] : /* istanbul ignore next */ []);
|
|
37
|
+
required = input(false, {
|
|
38
|
+
...ngDevMode ? { debugName: "required" } : /* istanbul ignore next */ {},
|
|
39
|
+
transform: booleanAttribute
|
|
40
|
+
});
|
|
41
|
+
messages = inject(MS_ERROR_MESSAGES);
|
|
42
|
+
baseId = `ms-field-${nextId++}`;
|
|
43
|
+
hintId = `${this.baseId}-hint`;
|
|
44
|
+
errorId = `${this.baseId}-error`;
|
|
45
|
+
fallbackControlId = `${this.baseId}-control`;
|
|
46
|
+
control = signal(null, ...ngDevMode ? [{ debugName: "control" }] : /* istanbul ignore next */ []);
|
|
47
|
+
controlId = computed(() => this.control()?.id() ?? this.fallbackControlId, ...ngDevMode ? [{ debugName: "controlId" }] : /* istanbul ignore next */ []);
|
|
48
|
+
isRequired = computed(() => this.required() || !!this.control()?.required?.(), ...ngDevMode ? [{ debugName: "isRequired" }] : /* istanbul ignore next */ []);
|
|
49
|
+
errorMessage = computed(() => {
|
|
50
|
+
const explicit = this.error();
|
|
51
|
+
if (explicit) return explicit;
|
|
52
|
+
const [first] = this.control()?.errors?.() ?? [];
|
|
53
|
+
return first ? msResolveErrorMessage(first, this.messages) : null;
|
|
54
|
+
}, ...ngDevMode ? [{ debugName: "errorMessage" }] : /* istanbul ignore next */ []);
|
|
55
|
+
invalid = computed(() => this.errorMessage() !== null, ...ngDevMode ? [{ debugName: "invalid" }] : /* istanbul ignore next */ []);
|
|
56
|
+
describedBy = computed(() => {
|
|
57
|
+
const ids = [this.hint() ? this.hintId : null, this.invalid() ? this.errorId : null].filter((id) => id !== null);
|
|
58
|
+
return ids.length ? ids.join(" ") : null;
|
|
59
|
+
}, ...ngDevMode ? [{ debugName: "describedBy" }] : /* istanbul ignore next */ []);
|
|
60
|
+
registerControl(control) {
|
|
61
|
+
this.control.set(control);
|
|
62
|
+
}
|
|
63
|
+
static ɵfac = i0.ɵɵngDeclareFactory({
|
|
64
|
+
minVersion: "12.0.0",
|
|
65
|
+
version: "22.2.4",
|
|
66
|
+
ngImport: i0,
|
|
67
|
+
type: MsField,
|
|
68
|
+
deps: [],
|
|
69
|
+
target: i0.ɵɵFactoryTarget.Component
|
|
70
|
+
});
|
|
71
|
+
static ɵcmp = i0.ɵɵngDeclareComponent({
|
|
72
|
+
minVersion: "17.0.0",
|
|
73
|
+
version: "22.2.4",
|
|
74
|
+
type: MsField,
|
|
75
|
+
isStandalone: true,
|
|
76
|
+
selector: "ms-field",
|
|
77
|
+
inputs: {
|
|
78
|
+
label: {
|
|
79
|
+
classPropertyName: "label",
|
|
80
|
+
publicName: "label",
|
|
81
|
+
isSignal: true,
|
|
82
|
+
isRequired: true,
|
|
83
|
+
transformFunction: null
|
|
84
|
+
},
|
|
85
|
+
hint: {
|
|
86
|
+
classPropertyName: "hint",
|
|
87
|
+
publicName: "hint",
|
|
88
|
+
isSignal: true,
|
|
89
|
+
isRequired: false,
|
|
90
|
+
transformFunction: null
|
|
91
|
+
},
|
|
92
|
+
error: {
|
|
93
|
+
classPropertyName: "error",
|
|
94
|
+
publicName: "error",
|
|
95
|
+
isSignal: true,
|
|
96
|
+
isRequired: false,
|
|
97
|
+
transformFunction: null
|
|
98
|
+
},
|
|
99
|
+
required: {
|
|
100
|
+
classPropertyName: "required",
|
|
101
|
+
publicName: "required",
|
|
102
|
+
isSignal: true,
|
|
103
|
+
isRequired: false,
|
|
104
|
+
transformFunction: null
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
host: {
|
|
108
|
+
properties: { "attr.data-invalid": "invalid() ? \"\" : null" },
|
|
109
|
+
classAttribute: "ms-field"
|
|
110
|
+
},
|
|
111
|
+
ngImport: i0,
|
|
112
|
+
template: `
|
|
113
|
+
<label class="ms-field__label" [attr.for]="controlId()">
|
|
114
|
+
{{ label() }}
|
|
115
|
+
@if (isRequired()) {
|
|
116
|
+
<span class="ms-field__required" aria-hidden="true">*</span>
|
|
117
|
+
}
|
|
118
|
+
</label>
|
|
119
|
+
@if (hint()) {
|
|
120
|
+
<p class="ms-field__hint" [id]="hintId">{{ hint() }}</p>
|
|
121
|
+
}
|
|
122
|
+
@if (errorMessage(); as message) {
|
|
123
|
+
<p class="ms-field__error" [id]="errorId">
|
|
124
|
+
<span class="ms-visually-hidden">Error: </span>{{ message }}
|
|
125
|
+
</p>
|
|
126
|
+
}
|
|
127
|
+
<ng-content />
|
|
128
|
+
`,
|
|
129
|
+
isInline: true
|
|
130
|
+
});
|
|
131
|
+
};
|
|
132
|
+
i0.ɵɵngDeclareClassMetadata({
|
|
133
|
+
minVersion: "12.0.0",
|
|
134
|
+
version: "22.2.4",
|
|
135
|
+
ngImport: i0,
|
|
136
|
+
type: MsField,
|
|
137
|
+
decorators: [{
|
|
138
|
+
type: Component,
|
|
139
|
+
args: [{
|
|
140
|
+
selector: "ms-field",
|
|
141
|
+
host: {
|
|
142
|
+
class: "ms-field",
|
|
143
|
+
"[attr.data-invalid]": "invalid() ? \"\" : null"
|
|
144
|
+
},
|
|
145
|
+
template: `
|
|
146
|
+
<label class="ms-field__label" [attr.for]="controlId()">
|
|
147
|
+
{{ label() }}
|
|
148
|
+
@if (isRequired()) {
|
|
149
|
+
<span class="ms-field__required" aria-hidden="true">*</span>
|
|
150
|
+
}
|
|
151
|
+
</label>
|
|
152
|
+
@if (hint()) {
|
|
153
|
+
<p class="ms-field__hint" [id]="hintId">{{ hint() }}</p>
|
|
154
|
+
}
|
|
155
|
+
@if (errorMessage(); as message) {
|
|
156
|
+
<p class="ms-field__error" [id]="errorId">
|
|
157
|
+
<span class="ms-visually-hidden">Error: </span>{{ message }}
|
|
158
|
+
</p>
|
|
159
|
+
}
|
|
160
|
+
<ng-content />
|
|
161
|
+
`
|
|
162
|
+
}]
|
|
163
|
+
}],
|
|
164
|
+
propDecorators: {
|
|
165
|
+
label: [{
|
|
166
|
+
type: i0.Input,
|
|
167
|
+
args: [{
|
|
168
|
+
isSignal: true,
|
|
169
|
+
alias: "label",
|
|
170
|
+
required: true
|
|
171
|
+
}]
|
|
172
|
+
}],
|
|
173
|
+
hint: [{
|
|
174
|
+
type: i0.Input,
|
|
175
|
+
args: [{
|
|
176
|
+
isSignal: true,
|
|
177
|
+
alias: "hint",
|
|
178
|
+
required: false
|
|
179
|
+
}]
|
|
180
|
+
}],
|
|
181
|
+
error: [{
|
|
182
|
+
type: i0.Input,
|
|
183
|
+
args: [{
|
|
184
|
+
isSignal: true,
|
|
185
|
+
alias: "error",
|
|
186
|
+
required: false
|
|
187
|
+
}]
|
|
188
|
+
}],
|
|
189
|
+
required: [{
|
|
190
|
+
type: i0.Input,
|
|
191
|
+
args: [{
|
|
192
|
+
isSignal: true,
|
|
193
|
+
alias: "required",
|
|
194
|
+
required: false
|
|
195
|
+
}]
|
|
196
|
+
}]
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
export { MS_DEFAULT_ERROR_MESSAGES, MS_ERROR_MESSAGES, MS_FALLBACK_ERROR_MESSAGE, MsField, msResolveErrorMessage, provideMsErrorMessages };
|
|
200
|
+
|
|
201
|
+
//# sourceMappingURL=maestral-field.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"maestral-field.mjs","names":[],"sources":["../../../projects/maestral/field/error-messages.ts","../../../projects/maestral/field/field.ts"],"sourcesContent":["import { InjectionToken, Provider } from '@angular/core';\n\n/** The parts of a validation error that Maestral needs to describe it. */\nexport interface MsValidationError {\n readonly kind: string;\n readonly message?: string;\n}\n\n/** Reads a detail such as `minLength` from a built-in validation error. */\nfunction detail(error: MsValidationError, key: string): unknown {\n return (error as unknown as Record<string, unknown>)[key];\n}\n\n/** Builds a human-readable message for one kind of validation error. */\nexport type MsErrorMessageFn = (error: MsValidationError) => string;\n\n/** Messages keyed by validation error kind (`required`, `email`, ...). */\nexport type MsErrorMessages = Readonly<Record<string, MsErrorMessageFn>>;\n\n/**\n * Default English messages for Angular's built-in validators. Wording follows\n * plain-language guidance: say what to do, not what went wrong.\n */\nexport const MS_DEFAULT_ERROR_MESSAGES: MsErrorMessages = {\n required: () => 'This field is required',\n email: () => 'Enter an email address in the correct format, like name@example.com',\n minLength: (e) => `Enter at least ${detail(e, 'minLength')} characters`,\n maxLength: (e) => `Enter ${detail(e, 'maxLength')} characters or fewer`,\n min: (e) => `Enter a value of ${detail(e, 'min')} or more`,\n max: (e) => `Enter a value of ${detail(e, 'max')} or less`,\n pattern: () => 'Enter a value in the correct format',\n};\n\n/** Fallback when no message exists for an error kind. */\nexport const MS_FALLBACK_ERROR_MESSAGE = 'Check this field';\n\nexport const MS_ERROR_MESSAGES = new InjectionToken<MsErrorMessages>('MS_ERROR_MESSAGES', {\n providedIn: 'root',\n factory: () => MS_DEFAULT_ERROR_MESSAGES,\n});\n\n/**\n * Overrides or translates error messages. Messages not listed keep their\n * defaults.\n *\n * ```ts\n * providers: [provideMsErrorMessages({ required: () => 'Polje je obavezno' })]\n * ```\n */\nexport function provideMsErrorMessages(messages: MsErrorMessages): Provider {\n return {\n provide: MS_ERROR_MESSAGES,\n useValue: { ...MS_DEFAULT_ERROR_MESSAGES, ...messages },\n };\n}\n\n/**\n * Resolves the text for an error. A message set in the form schema wins over\n * the configured messages.\n */\nexport function msResolveErrorMessage(error: MsValidationError, messages: MsErrorMessages): string {\n return error.message ?? messages[error.kind]?.(error) ?? MS_FALLBACK_ERROR_MESSAGE;\n}\n","import {\n Component,\n Signal,\n booleanAttribute,\n computed,\n inject,\n input,\n signal,\n} from '@angular/core';\nimport { MS_ERROR_MESSAGES, MsValidationError, msResolveErrorMessage } from './error-messages';\n\n/** A control that can be placed inside `<ms-field>`, such as `MsInput`. */\nexport interface MsFieldControl {\n /** The control's effective id, used for the label's `for` attribute. */\n readonly id: Signal<string>;\n /**\n * Validation errors the field should show now. The control decides the\n * timing (for example only after the user has left the field).\n */\n readonly errors?: Signal<readonly MsValidationError[]>;\n /** Whether the control's validation rules make it required. */\n readonly required?: Signal<boolean>;\n}\n\nlet nextId = 0;\n\n/**\n * Wraps a form control with its label, hint and error message, and wires\n * up the ids and ARIA attributes between them.\n *\n * Order follows proven public-sector patterns: label, hint, error, control,\n * so screen readers and sighted users meet them in the same order.\n *\n * The error comes from the `error` input when set, otherwise from the first\n * validation error the control reports (see `MsFieldControl.errors`).\n */\n@Component({\n selector: 'ms-field',\n host: {\n class: 'ms-field',\n '[attr.data-invalid]': 'invalid() ? \"\" : null',\n },\n template: `\n <label class=\"ms-field__label\" [attr.for]=\"controlId()\">\n {{ label() }}\n @if (isRequired()) {\n <span class=\"ms-field__required\" aria-hidden=\"true\">*</span>\n }\n </label>\n @if (hint()) {\n <p class=\"ms-field__hint\" [id]=\"hintId\">{{ hint() }}</p>\n }\n @if (errorMessage(); as message) {\n <p class=\"ms-field__error\" [id]=\"errorId\">\n <span class=\"ms-visually-hidden\">Error: </span>{{ message }}\n </p>\n }\n <ng-content />\n `,\n})\nexport class MsField {\n readonly label = input.required<string>();\n readonly hint = input<string>();\n /** Explicit error message. Takes precedence over validation errors. */\n readonly error = input<string>();\n /** Marks the field as required even without a `required` validator. */\n readonly required = input(false, { transform: booleanAttribute });\n\n private readonly messages = inject(MS_ERROR_MESSAGES);\n\n private readonly baseId = `ms-field-${nextId++}`;\n readonly hintId = `${this.baseId}-hint`;\n readonly errorId = `${this.baseId}-error`;\n /** Id a control should use when it has no id of its own. */\n readonly fallbackControlId = `${this.baseId}-control`;\n\n private readonly control = signal<MsFieldControl | null>(null);\n\n readonly controlId = computed(() => this.control()?.id() ?? this.fallbackControlId);\n\n readonly isRequired = computed(() => this.required() || !!this.control()?.required?.());\n\n /** One message at a time: the first error is the one to fix first. */\n readonly errorMessage = computed(() => {\n const explicit = this.error();\n if (explicit) {\n return explicit;\n }\n const [first] = this.control()?.errors?.() ?? [];\n return first ? msResolveErrorMessage(first, this.messages) : null;\n });\n\n readonly invalid = computed(() => this.errorMessage() !== null);\n\n readonly describedBy = computed(() => {\n const ids = [this.hint() ? this.hintId : null, this.invalid() ? this.errorId : null].filter(\n (id): id is string => id !== null,\n );\n return ids.length ? ids.join(' ') : null;\n });\n\n /** Called by a control inside the field to connect itself. */\n registerControl(control: MsFieldControl): void {\n this.control.set(control);\n }\n}\n"],"mappings":";;AASA,SAAS,OAAO,OAA0B,KAAW;CACnD,OAAQ,MAA6C;AACvD;AAYA,MAAa,4BAA6C;CACxD,gBAAgB;CAChB,aAAa;CACb,YAAY,MAAM,kBAAkB,OAAO,GAAG,WAAW,EAAC;CAC1D,YAAY,MAAM,SAAS,OAAO,GAAG,WAAW,EAAC;CACjD,MAAM,MAAM,oBAAoB,OAAO,GAAG,KAAK,EAAC;CAChD,MAAM,MAAM,oBAAoB,OAAO,GAAG,KAAK,EAAC;CAChD,eAAe;AAChB;AAGD,MAAa,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