@nanahoshi/mona-ui 0.29.0 → 0.31.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/docs/api/button-group.md +1 -0
- package/docs/api/color-gradient.md +1 -0
- package/docs/api/color-palette.md +1 -0
- package/docs/api/color-picker.md +1 -0
- package/docs/api/form-field.md +88 -0
- package/docs/api/i18n.md +44 -0
- package/docs/api/locales.md +14 -14
- package/docs/api/qr-code.md +287 -0
- package/docs/api/segmented.md +1 -0
- package/docs/api/switch.md +1 -0
- package/docs/dropdown-list.md +1 -1
- package/docs/form-field.md +425 -0
- package/docs/forms.md +12 -4
- package/docs/label.md +4 -5
- package/docs/numeric-textbox.md +1 -1
- package/docs/qr-code.md +377 -0
- package/docs/radio-button.md +2 -1
- package/fesm2022/nanahoshi-mona-ui-auto-complete.mjs +15 -4
- package/fesm2022/nanahoshi-mona-ui-button-group.mjs +25 -1
- package/fesm2022/nanahoshi-mona-ui-check-box.mjs +5 -2
- package/fesm2022/nanahoshi-mona-ui-color-gradient.mjs +29 -3
- package/fesm2022/nanahoshi-mona-ui-color-palette.mjs +25 -1
- package/fesm2022/nanahoshi-mona-ui-color-picker.mjs +31 -3
- package/fesm2022/nanahoshi-mona-ui-combo-box.mjs +15 -4
- package/fesm2022/nanahoshi-mona-ui-date-picker.mjs +15 -4
- package/fesm2022/nanahoshi-mona-ui-datetime-picker.mjs +15 -4
- package/fesm2022/nanahoshi-mona-ui-dropdown-list.mjs +19 -7
- package/fesm2022/nanahoshi-mona-ui-editor.mjs +24 -4
- package/fesm2022/nanahoshi-mona-ui-filter.mjs +1 -0
- package/fesm2022/nanahoshi-mona-ui-form-field.mjs +666 -0
- package/fesm2022/nanahoshi-mona-ui-i18n-icu.mjs +32 -0
- package/fesm2022/nanahoshi-mona-ui-internal.mjs +27 -2
- package/fesm2022/nanahoshi-mona-ui-locales-ar-sa.mjs +28 -0
- package/fesm2022/nanahoshi-mona-ui-locales-de-de.mjs +14 -0
- package/fesm2022/nanahoshi-mona-ui-locales-es-es.mjs +20 -0
- package/fesm2022/nanahoshi-mona-ui-locales-fr-fr.mjs +20 -0
- package/fesm2022/nanahoshi-mona-ui-locales-id-id.mjs +14 -0
- package/fesm2022/nanahoshi-mona-ui-locales-it-it.mjs +20 -0
- package/fesm2022/nanahoshi-mona-ui-locales-ja-jp.mjs +14 -0
- package/fesm2022/nanahoshi-mona-ui-locales-ko-kr.mjs +14 -0
- package/fesm2022/nanahoshi-mona-ui-locales-pl-pl.mjs +24 -0
- package/fesm2022/nanahoshi-mona-ui-locales-pt-br.mjs +20 -0
- package/fesm2022/nanahoshi-mona-ui-locales-ru-ru.mjs +24 -0
- package/fesm2022/nanahoshi-mona-ui-locales-tr-tr.mjs +14 -0
- package/fesm2022/nanahoshi-mona-ui-locales-zh-cn.mjs +14 -0
- package/fesm2022/nanahoshi-mona-ui-locales-zh-tw.mjs +14 -0
- package/fesm2022/nanahoshi-mona-ui-multi-select.mjs +19 -7
- package/fesm2022/nanahoshi-mona-ui-numeric-text-box.mjs +14 -5
- package/fesm2022/nanahoshi-mona-ui-otp-input.mjs +6 -3
- package/fesm2022/nanahoshi-mona-ui-qr-code.mjs +1671 -0
- package/fesm2022/nanahoshi-mona-ui-radio-button.mjs +15 -9
- package/fesm2022/nanahoshi-mona-ui-rating.mjs +6 -3
- package/fesm2022/nanahoshi-mona-ui-segmented.mjs +25 -1
- package/fesm2022/nanahoshi-mona-ui-slider.mjs +8 -5
- package/fesm2022/nanahoshi-mona-ui-switch.mjs +25 -1
- package/fesm2022/nanahoshi-mona-ui-text-box.mjs +6 -3
- package/fesm2022/nanahoshi-mona-ui-time-picker.mjs +15 -4
- package/form-field/package.json +4 -0
- package/i18n/catalogs/en-US.messages.json +16 -0
- package/llms.txt +5 -1
- package/locales/ar-sa/ar-sa.messages.json +16 -0
- package/locales/de-de/de-de.messages.json +16 -0
- package/locales/es-es/es-es.messages.json +16 -0
- package/locales/fr-fr/fr-fr.messages.json +16 -0
- package/locales/id-id/id-id.messages.json +16 -0
- package/locales/it-it/it-it.messages.json +16 -0
- package/locales/ja-jp/ja-jp.messages.json +16 -0
- package/locales/ko-kr/ko-kr.messages.json +16 -0
- package/locales/pl-pl/pl-pl.messages.json +16 -0
- package/locales/pt-br/pt-br.messages.json +16 -0
- package/locales/ru-ru/ru-ru.messages.json +16 -0
- package/locales/tr-tr/tr-tr.messages.json +16 -0
- package/locales/zh-cn/zh-cn.messages.json +16 -0
- package/locales/zh-tw/zh-tw.messages.json +16 -0
- package/package.json +9 -1
- package/qr-code/package.json +4 -0
- package/types/nanahoshi-mona-ui-auto-complete.d.ts +2 -0
- package/types/nanahoshi-mona-ui-button-group.d.ts +11 -0
- package/types/nanahoshi-mona-ui-check-box.d.ts +1 -0
- package/types/nanahoshi-mona-ui-color-gradient.d.ts +11 -0
- package/types/nanahoshi-mona-ui-color-palette.d.ts +11 -0
- package/types/nanahoshi-mona-ui-color-picker.d.ts +11 -0
- package/types/nanahoshi-mona-ui-combo-box.d.ts +2 -0
- package/types/nanahoshi-mona-ui-date-picker.d.ts +2 -0
- package/types/nanahoshi-mona-ui-datetime-picker.d.ts +2 -0
- package/types/nanahoshi-mona-ui-dropdown-list.d.ts +2 -0
- package/types/nanahoshi-mona-ui-form-field.d.ts +173 -0
- package/types/nanahoshi-mona-ui-i18n.d.ts +33 -0
- package/types/nanahoshi-mona-ui-internal.d.ts +39 -4
- package/types/nanahoshi-mona-ui-locales-ar-sa.d.ts +16 -0
- package/types/nanahoshi-mona-ui-locales-de-de.d.ts +16 -0
- package/types/nanahoshi-mona-ui-locales-es-es.d.ts +16 -0
- package/types/nanahoshi-mona-ui-locales-fr-fr.d.ts +16 -0
- package/types/nanahoshi-mona-ui-locales-id-id.d.ts +16 -0
- package/types/nanahoshi-mona-ui-locales-it-it.d.ts +16 -0
- package/types/nanahoshi-mona-ui-locales-ja-jp.d.ts +16 -0
- package/types/nanahoshi-mona-ui-locales-ko-kr.d.ts +16 -0
- package/types/nanahoshi-mona-ui-locales-pl-pl.d.ts +16 -0
- package/types/nanahoshi-mona-ui-locales-pt-br.d.ts +16 -0
- package/types/nanahoshi-mona-ui-locales-ru-ru.d.ts +16 -0
- package/types/nanahoshi-mona-ui-locales-tr-tr.d.ts +16 -0
- package/types/nanahoshi-mona-ui-locales-zh-cn.d.ts +16 -0
- package/types/nanahoshi-mona-ui-locales-zh-tw.d.ts +16 -0
- package/types/nanahoshi-mona-ui-multi-select.d.ts +2 -0
- package/types/nanahoshi-mona-ui-numeric-text-box.d.ts +2 -0
- package/types/nanahoshi-mona-ui-otp-input.d.ts +1 -0
- package/types/nanahoshi-mona-ui-qr-code.d.ts +322 -0
- package/types/nanahoshi-mona-ui-radio-button.d.ts +2 -0
- package/types/nanahoshi-mona-ui-rating.d.ts +1 -0
- package/types/nanahoshi-mona-ui-segmented.d.ts +11 -0
- package/types/nanahoshi-mona-ui-slider.d.ts +1 -0
- package/types/nanahoshi-mona-ui-switch.d.ts +11 -0
- package/types/nanahoshi-mona-ui-text-box.d.ts +1 -0
- package/types/nanahoshi-mona-ui-time-picker.d.ts +2 -0
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
# Form Field
|
|
2
|
+
|
|
3
|
+
> Labels one form control, describes it with a hint and shows its validation errors.
|
|
4
|
+
|
|
5
|
+
Entry point: `@nanahoshi/mona-ui/form-field`
|
|
6
|
+
|
|
7
|
+
**Selector:** `mona-form-field`
|
|
8
|
+
|
|
9
|
+
`FormFieldComponent` puts a label, a hint and validation errors around one form control. It names the control with the label, describes it with the hint and the errors, and shows the errors of a bound signal form field once the control is touched.
|
|
10
|
+
|
|
11
|
+
## Overview
|
|
12
|
+
|
|
13
|
+
Use a form field around each control of a form. It draws the label above or beside the control, a hint below it, and the control's errors under the hint, and it connects all three to the control for assistive technology. Bound to a signal form, it reads the errors and the `required` rule itself, so a field needs no `@if` blocks of its own.
|
|
14
|
+
|
|
15
|
+
It works with every Mona UI form control, and with a native `<input>`, `<select>` or `<textarea>`.
|
|
16
|
+
|
|
17
|
+
Use `mona-label` instead when a control only needs a caption, and `mona-fieldset` to group several fields under one legend. A form field holds one control: for a group of check boxes, use a fieldset.
|
|
18
|
+
|
|
19
|
+
## Import & Quick Start
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { FormFieldComponent } from "@nanahoshi/mona-ui/form-field";
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```html
|
|
26
|
+
<mona-form-field label="Email" hint="We send the confirmation here.">
|
|
27
|
+
<mona-text-box [formField]="account.email"></mona-text-box>
|
|
28
|
+
</mona-form-field>
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Anatomy
|
|
32
|
+
|
|
33
|
+
```html
|
|
34
|
+
<mona-form-field label="…" hint="…" [error]="…">
|
|
35
|
+
<ng-template monaFormFieldLabelTemplate>…</ng-template>
|
|
36
|
+
<ng-template monaFormFieldHintTemplate>…</ng-template>
|
|
37
|
+
<ng-template monaFormFieldErrorTemplate let-message let-error="error">…</ng-template>
|
|
38
|
+
</mona-form-field>
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
| Part | What it is |
|
|
42
|
+
| --- | --- |
|
|
43
|
+
| Label | A `<label>` with the `label` text or the label template, then the optional text or the required marker. It names the control. |
|
|
44
|
+
| Control | The projected control. |
|
|
45
|
+
| Hint | The `hint` text or the hint template. It describes the control. |
|
|
46
|
+
| Errors | The `error` text, then each error of the bound field, one per line. They describe the control while shown. |
|
|
47
|
+
|
|
48
|
+
The error template is rendered once for each error. Its `$implicit` is the message the field would show, and `error` is the validation error, or null for the `error` input.
|
|
49
|
+
|
|
50
|
+
## Examples
|
|
51
|
+
|
|
52
|
+
### Validation from a signal form
|
|
53
|
+
|
|
54
|
+
The field shows the errors of the control's `[formField]` once the control is touched, which happens when focus leaves it or when `submit()` marks the whole form. Each validator has a message in every built-in locale.
|
|
55
|
+
|
|
56
|
+
`signal-forms-example.component.ts`
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import { Component, signal } from "@angular/core";
|
|
60
|
+
import { email, form, FormField, minLength, required, submit } from "@angular/forms/signals";
|
|
61
|
+
import { ButtonDirective } from "@nanahoshi/mona-ui/button";
|
|
62
|
+
import { FormFieldComponent } from "@nanahoshi/mona-ui/form-field";
|
|
63
|
+
import { TextBoxComponent } from "@nanahoshi/mona-ui/text-box";
|
|
64
|
+
|
|
65
|
+
interface AccountModel {
|
|
66
|
+
email: string;
|
|
67
|
+
name: string;
|
|
68
|
+
password: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
@Component({
|
|
72
|
+
selector: "app-form-field-signal-forms-example",
|
|
73
|
+
imports: [ButtonDirective, FormField, FormFieldComponent, TextBoxComponent],
|
|
74
|
+
templateUrl: "./signal-forms-example.component.html"
|
|
75
|
+
})
|
|
76
|
+
export class FormFieldSignalFormsExampleComponent {
|
|
77
|
+
readonly #model = signal<AccountModel>({ email: "", name: "", password: "" });
|
|
78
|
+
protected readonly account = form(this.#model, path => {
|
|
79
|
+
required(path.name);
|
|
80
|
+
required(path.email);
|
|
81
|
+
email(path.email);
|
|
82
|
+
required(path.password);
|
|
83
|
+
minLength(path.password, 12);
|
|
84
|
+
});
|
|
85
|
+
protected readonly created = signal(false);
|
|
86
|
+
|
|
87
|
+
protected onSubmit(event: Event): void {
|
|
88
|
+
event.preventDefault();
|
|
89
|
+
void submit(this.account, async () => {
|
|
90
|
+
this.created.set(true);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
`signal-forms-example.component.html`
|
|
97
|
+
|
|
98
|
+
```html
|
|
99
|
+
<form class="flex max-w-sm flex-col gap-4" novalidate (submit)="onSubmit($event)">
|
|
100
|
+
<mona-form-field label="Name">
|
|
101
|
+
<mona-text-box [formField]="account.name" />
|
|
102
|
+
</mona-form-field>
|
|
103
|
+
<mona-form-field label="Email" hint="We send the confirmation here.">
|
|
104
|
+
<mona-text-box type="email" [formField]="account.email" />
|
|
105
|
+
</mona-form-field>
|
|
106
|
+
<mona-form-field label="Password" hint="At least 12 characters.">
|
|
107
|
+
<mona-text-box type="password" [formField]="account.password" />
|
|
108
|
+
</mona-form-field>
|
|
109
|
+
<div class="flex items-center gap-3">
|
|
110
|
+
<button monaButton look="primary" type="submit">Create account</button>
|
|
111
|
+
@if (created()) {
|
|
112
|
+
<span class="text-sm" role="status">Account created.</span>
|
|
113
|
+
}
|
|
114
|
+
</div>
|
|
115
|
+
</form>
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Hints and optional fields
|
|
119
|
+
|
|
120
|
+
`hint` describes the control. Use the hint template for content such as a link. `optional` adds text after the label, which reads "Optional" unless `optionalText` replaces it. A field shows a required marker when its form rule or its `required` input says so.
|
|
121
|
+
|
|
122
|
+
`hint-and-optional-example.component.ts`
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
import { Component } from "@angular/core";
|
|
126
|
+
import { FormFieldComponent, FormFieldHintTemplateDirective } from "@nanahoshi/mona-ui/form-field";
|
|
127
|
+
import { TextBoxComponent } from "@nanahoshi/mona-ui/text-box";
|
|
128
|
+
|
|
129
|
+
@Component({
|
|
130
|
+
selector: "app-form-field-hint-and-optional-example",
|
|
131
|
+
imports: [FormFieldComponent, FormFieldHintTemplateDirective, TextBoxComponent],
|
|
132
|
+
templateUrl: "./hint-and-optional-example.component.html"
|
|
133
|
+
})
|
|
134
|
+
export class FormFieldHintAndOptionalExampleComponent {}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
`hint-and-optional-example.component.html`
|
|
138
|
+
|
|
139
|
+
```html
|
|
140
|
+
<div class="flex max-w-sm flex-col gap-4">
|
|
141
|
+
<mona-form-field label="Full name" required>
|
|
142
|
+
<mona-text-box required />
|
|
143
|
+
</mona-form-field>
|
|
144
|
+
<mona-form-field label="Company" optional>
|
|
145
|
+
<mona-text-box />
|
|
146
|
+
</mona-form-field>
|
|
147
|
+
<mona-form-field label="VAT number" optional optionalText="If you have one">
|
|
148
|
+
<ng-template monaFormFieldHintTemplate>
|
|
149
|
+
The format depends on the country. <a class="underline" href="#vat">Where to find it</a>
|
|
150
|
+
</ng-template>
|
|
151
|
+
<mona-text-box />
|
|
152
|
+
</mona-form-field>
|
|
153
|
+
</div>
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Label beside the control
|
|
157
|
+
|
|
158
|
+
With `orientation` set to `horizontal`, the label takes a column of its own and the hint and errors line up under the control. The `--mona-form-field-label-width` custom property sets that column, on the field or on any element around a set of fields.
|
|
159
|
+
|
|
160
|
+
`horizontal-example.component.ts`
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
import { Component } from "@angular/core";
|
|
164
|
+
import { DropdownListComponent } from "@nanahoshi/mona-ui/dropdown-list";
|
|
165
|
+
import { FormFieldComponent } from "@nanahoshi/mona-ui/form-field";
|
|
166
|
+
import { NumericTextBoxComponent } from "@nanahoshi/mona-ui/numeric-text-box";
|
|
167
|
+
import { TextBoxComponent } from "@nanahoshi/mona-ui/text-box";
|
|
168
|
+
|
|
169
|
+
@Component({
|
|
170
|
+
selector: "app-form-field-horizontal-example",
|
|
171
|
+
imports: [DropdownListComponent, FormFieldComponent, NumericTextBoxComponent, TextBoxComponent],
|
|
172
|
+
templateUrl: "./horizontal-example.component.html"
|
|
173
|
+
})
|
|
174
|
+
export class FormFieldHorizontalExampleComponent {
|
|
175
|
+
protected readonly units = ["Pieces", "Boxes", "Pallets"];
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
`horizontal-example.component.html`
|
|
180
|
+
|
|
181
|
+
```html
|
|
182
|
+
<div class="flex max-w-lg flex-col gap-4 [--mona-form-field-label-width:8rem]">
|
|
183
|
+
<mona-form-field label="Product" orientation="horizontal">
|
|
184
|
+
<mona-text-box />
|
|
185
|
+
</mona-form-field>
|
|
186
|
+
<mona-form-field label="Quantity" orientation="horizontal" hint="Up to 500 per order.">
|
|
187
|
+
<mona-numeric-text-box [minValue]="1" [maxValue]="500" />
|
|
188
|
+
</mona-form-field>
|
|
189
|
+
<mona-form-field label="Unit" orientation="horizontal">
|
|
190
|
+
<mona-dropdown-list class="w-full" [data]="units" />
|
|
191
|
+
</mona-form-field>
|
|
192
|
+
</div>
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### Your own error messages
|
|
196
|
+
|
|
197
|
+
A validator's `message` option wins over the built-in one. The error template changes how each error is drawn, here with an icon.
|
|
198
|
+
|
|
199
|
+
`custom-errors-example.component.ts`
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
import { Component, signal } from "@angular/core";
|
|
203
|
+
import { form, FormField, pattern, required } from "@angular/forms/signals";
|
|
204
|
+
import { LucideCircleAlert } from "@lucide/angular";
|
|
205
|
+
import { FormFieldComponent, FormFieldErrorTemplateDirective } from "@nanahoshi/mona-ui/form-field";
|
|
206
|
+
import { TextBoxComponent } from "@nanahoshi/mona-ui/text-box";
|
|
207
|
+
|
|
208
|
+
@Component({
|
|
209
|
+
selector: "app-form-field-custom-errors-example",
|
|
210
|
+
imports: [FormField, FormFieldComponent, FormFieldErrorTemplateDirective, LucideCircleAlert, TextBoxComponent],
|
|
211
|
+
templateUrl: "./custom-errors-example.component.html"
|
|
212
|
+
})
|
|
213
|
+
export class FormFieldCustomErrorsExampleComponent {
|
|
214
|
+
readonly #model = signal({ postcode: "" });
|
|
215
|
+
protected readonly address = form(this.#model, path => {
|
|
216
|
+
required(path.postcode, { message: "Enter your postcode." });
|
|
217
|
+
pattern(path.postcode, /^\d{2}-\d{3}$/, { message: "Enter a postcode like 00-950." });
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
protected touch(): void {
|
|
221
|
+
this.address.postcode().markAsTouched();
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
`custom-errors-example.component.html`
|
|
227
|
+
|
|
228
|
+
```html
|
|
229
|
+
<div class="flex max-w-sm flex-col items-start gap-3">
|
|
230
|
+
<mona-form-field class="w-full" label="Postcode" hint="Two digits, a hyphen and three digits.">
|
|
231
|
+
<ng-template monaFormFieldErrorTemplate let-message>
|
|
232
|
+
<span class="inline-flex items-center gap-1">
|
|
233
|
+
<svg lucideCircleAlert class="size-3.5 shrink-0" aria-hidden="true"></svg>
|
|
234
|
+
{{ message }}
|
|
235
|
+
</span>
|
|
236
|
+
</ng-template>
|
|
237
|
+
<mona-text-box [formField]="address.postcode" />
|
|
238
|
+
</mona-form-field>
|
|
239
|
+
<button class="text-sm underline" type="button" (click)="touch()">Show the error</button>
|
|
240
|
+
</div>
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### An error from a server
|
|
244
|
+
|
|
245
|
+
`error` shows text whether or not the control is touched, such as an answer from a server. Set the control's `invalid` and `touched` too, so it draws its error state.
|
|
246
|
+
|
|
247
|
+
`server-error-example.component.ts`
|
|
248
|
+
|
|
249
|
+
```typescript
|
|
250
|
+
import { Component, computed, signal } from "@angular/core";
|
|
251
|
+
import { ButtonDirective } from "@nanahoshi/mona-ui/button";
|
|
252
|
+
import { FormFieldComponent } from "@nanahoshi/mona-ui/form-field";
|
|
253
|
+
import { TextBoxComponent } from "@nanahoshi/mona-ui/text-box";
|
|
254
|
+
|
|
255
|
+
const TAKEN = ["admin", "mona", "root"];
|
|
256
|
+
|
|
257
|
+
@Component({
|
|
258
|
+
selector: "app-form-field-server-error-example",
|
|
259
|
+
imports: [ButtonDirective, FormFieldComponent, TextBoxComponent],
|
|
260
|
+
templateUrl: "./server-error-example.component.html"
|
|
261
|
+
})
|
|
262
|
+
export class FormFieldServerErrorExampleComponent {
|
|
263
|
+
protected readonly checked = signal<string | null>(null);
|
|
264
|
+
protected readonly error = computed(() => {
|
|
265
|
+
const checked = this.checked();
|
|
266
|
+
return checked !== null && TAKEN.includes(checked.toLowerCase()) ? `"${checked}" is already taken.` : "";
|
|
267
|
+
});
|
|
268
|
+
protected readonly username = signal("mona");
|
|
269
|
+
|
|
270
|
+
protected check(): void {
|
|
271
|
+
this.checked.set(this.username());
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
`server-error-example.component.html`
|
|
277
|
+
|
|
278
|
+
```html
|
|
279
|
+
<div class="flex max-w-sm flex-col items-start gap-3">
|
|
280
|
+
<mona-form-field class="w-full" label="Username" [error]="error()">
|
|
281
|
+
<mona-text-box [invalid]="!!error()" [touched]="!!error()" [(value)]="username" />
|
|
282
|
+
</mona-form-field>
|
|
283
|
+
<button monaButton type="button" (click)="check()">Check availability</button>
|
|
284
|
+
</div>
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
### Other controls
|
|
288
|
+
|
|
289
|
+
The field names and describes any Mona UI control, and a native `<input>`, `<select>` or `<textarea>`. A click on the label focuses the control, or toggles a switch, as a native label does.
|
|
290
|
+
|
|
291
|
+
`other-controls-example.component.ts`
|
|
292
|
+
|
|
293
|
+
```typescript
|
|
294
|
+
import { Component, signal } from "@angular/core";
|
|
295
|
+
import { form, FormField, required } from "@angular/forms/signals";
|
|
296
|
+
import { DatePickerComponent } from "@nanahoshi/mona-ui/date-picker";
|
|
297
|
+
import { DropdownListComponent } from "@nanahoshi/mona-ui/dropdown-list";
|
|
298
|
+
import { FormFieldComponent } from "@nanahoshi/mona-ui/form-field";
|
|
299
|
+
import { RadioButtonComponent, RadioGroupComponent } from "@nanahoshi/mona-ui/radio-button";
|
|
300
|
+
import { SwitchComponent } from "@nanahoshi/mona-ui/switch";
|
|
301
|
+
import { TextAreaDirective } from "@nanahoshi/mona-ui/text-area";
|
|
302
|
+
|
|
303
|
+
interface BookingModel {
|
|
304
|
+
date: Date | null;
|
|
305
|
+
newsletter: boolean;
|
|
306
|
+
notes: string;
|
|
307
|
+
room: string | null;
|
|
308
|
+
seating: string;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
@Component({
|
|
312
|
+
selector: "app-form-field-other-controls-example",
|
|
313
|
+
imports: [
|
|
314
|
+
DatePickerComponent,
|
|
315
|
+
DropdownListComponent,
|
|
316
|
+
FormField,
|
|
317
|
+
FormFieldComponent,
|
|
318
|
+
RadioButtonComponent,
|
|
319
|
+
RadioGroupComponent,
|
|
320
|
+
SwitchComponent,
|
|
321
|
+
TextAreaDirective
|
|
322
|
+
],
|
|
323
|
+
templateUrl: "./other-controls-example.component.html"
|
|
324
|
+
})
|
|
325
|
+
export class FormFieldOtherControlsExampleComponent {
|
|
326
|
+
readonly #model = signal<BookingModel>({ date: null, newsletter: false, notes: "", room: null, seating: "table" });
|
|
327
|
+
protected readonly booking = form(this.#model, path => {
|
|
328
|
+
required(path.date);
|
|
329
|
+
required(path.room);
|
|
330
|
+
});
|
|
331
|
+
protected readonly rooms = ["Garden", "Library", "Terrace"];
|
|
332
|
+
}
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
`other-controls-example.component.html`
|
|
336
|
+
|
|
337
|
+
```html
|
|
338
|
+
<div class="flex max-w-sm flex-col gap-4">
|
|
339
|
+
<mona-form-field label="Date">
|
|
340
|
+
<mona-date-picker [formField]="booking.date" />
|
|
341
|
+
</mona-form-field>
|
|
342
|
+
<mona-form-field label="Room" hint="Each room seats up to 40 people.">
|
|
343
|
+
<mona-dropdown-list class="w-full" [data]="rooms" [formField]="booking.room" />
|
|
344
|
+
</mona-form-field>
|
|
345
|
+
<mona-form-field label="Seating">
|
|
346
|
+
<mona-radio-group [formField]="booking.seating">
|
|
347
|
+
<mona-radio-button label="Tables" radioValue="table" />
|
|
348
|
+
<mona-radio-button label="Rows" radioValue="rows" />
|
|
349
|
+
</mona-radio-group>
|
|
350
|
+
</mona-form-field>
|
|
351
|
+
<mona-form-field label="Notes" optional>
|
|
352
|
+
<textarea monaTextArea class="w-full" rows="3" [formField]="booking.notes"></textarea>
|
|
353
|
+
</mona-form-field>
|
|
354
|
+
<mona-form-field label="Send me the newsletter" orientation="horizontal">
|
|
355
|
+
<mona-switch [formField]="booking.newsletter" />
|
|
356
|
+
</mona-form-field>
|
|
357
|
+
</div>
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
## Appearance & Styling
|
|
361
|
+
|
|
362
|
+
- **Spacing:** the label, hint and errors are spaced with margins, so the error row takes no room while it's empty.
|
|
363
|
+
- **Width:** the field is a grid that fills its container, and the control keeps its own width inside it. Text boxes and pickers fill the field; a dropdown list or a native `<textarea>` is as wide as its content, so give it `class="w-full"` to fill the field. Set the field's own width with `class`.
|
|
364
|
+
- **Label column:** in horizontal fields, the label column is `10rem` unless `--mona-form-field-label-width` says otherwise. The label is centered on the control's first row.
|
|
365
|
+
- **Colors:** the hint uses `muted-foreground` and the errors and the required marker use `error`. Both read at 4.5:1 or more on the background in every built-in theme.
|
|
366
|
+
- **`class`** is merged onto the host with `tailwind-merge`.
|
|
367
|
+
|
|
368
|
+
## Behavior
|
|
369
|
+
|
|
370
|
+
### Where the errors come from
|
|
371
|
+
|
|
372
|
+
- **A bound signal form field:** the field finds the first `[formField]` inside it. Its errors show once it's touched, and its `required` rule shows the required marker.
|
|
373
|
+
- **The `error` input:** its text shows at once, before the field's own errors. It doesn't change the control, so set the control's `invalid` and `touched` as well.
|
|
374
|
+
- With neither, the field shows no errors. Reactive forms and `ngModel` take the `error` input.
|
|
375
|
+
|
|
376
|
+
### Messages
|
|
377
|
+
|
|
378
|
+
A validator's `message` option always wins. Otherwise the message comes from the `formField` messages of the locale: `required`, `email`, `pattern`, `min`, `max`, `minLength`, `maxLength`, `minDate` and `maxDate` have their own, and any other kind, such as a custom validator without a message or a parse error, gets `invalid`. Dates are written in the locale's long format. Change any of them with `provideMonaI18n`, like the other messages.
|
|
379
|
+
|
|
380
|
+
### One control per field
|
|
381
|
+
|
|
382
|
+
The first control inside the field takes the label and the description. Composite controls, such as a date picker, a color picker or a dropdown with a filter, keep them for themselves: the inputs inside their popups have names of their own. A radio group takes them, not its radios, and a check box keeps its own label as its name while the field describes it.
|
|
383
|
+
|
|
384
|
+
### Native controls
|
|
385
|
+
|
|
386
|
+
A native `<input>`, `<select>` or `<textarea>` has no Mona UI code to read the field, so the field sets its `aria-labelledby`, `aria-describedby` and, while an error shows, `aria-invalid`. It adds its ids after the element's own and removes only what it added. An element with its own `aria-label` or `aria-labelledby` keeps its name.
|
|
387
|
+
|
|
388
|
+
## Forms Integration
|
|
389
|
+
|
|
390
|
+
Bind the control, not the field:
|
|
391
|
+
|
|
392
|
+
```html
|
|
393
|
+
<mona-form-field label="Quantity">
|
|
394
|
+
<mona-numeric-text-box [formField]="order.quantity"></mona-numeric-text-box>
|
|
395
|
+
</mona-form-field>
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
The field reads the binding's `touched()`, `required()` and errors, and the control reads the rest of its state from the same binding, as described in [Forms](forms.md). `field().focusBoundControl()` still moves focus to the control, so focusing the first invalid field works as before.
|
|
399
|
+
|
|
400
|
+
## Accessibility
|
|
401
|
+
|
|
402
|
+
- The label is a native `<label>`. The control is named by it through `aria-labelledby`, so the name is the label text and the optional text, without the required marker, which is `aria-hidden`. The control's own `aria-required` says it's required.
|
|
403
|
+
- The hint and the shown errors describe the control through `aria-describedby`, after any ids the control already has. The errors sit in a polite live region that stays in the page, so a new error is announced once.
|
|
404
|
+
- A control's own `aria-label` or `aria-labelledby` wins over the label, as everywhere in Mona UI.
|
|
405
|
+
- A click on the label focuses the control, or activates it the way a native label would: it toggles a switch.
|
|
406
|
+
- Keep interactive elements out of the label template, since the label is the control's name. A link belongs in the hint.
|
|
407
|
+
- The field adds no keyboard handling of its own and no tab stop.
|
|
408
|
+
|
|
409
|
+
## API
|
|
410
|
+
|
|
411
|
+
Everything below is imported from `@nanahoshi/mona-ui/form-field`. Each name links to its reference page, with every input, output, property and method.
|
|
412
|
+
|
|
413
|
+
| Name | Kind | Description |
|
|
414
|
+
| --- | --- | --- |
|
|
415
|
+
| [`FormFieldComponent`](api/form-field.md#formfieldcomponent) | Component | Puts a label, a hint and validation errors around one form control, and connects them to it. |
|
|
416
|
+
| [`FormFieldErrorTemplateDirective`](api/form-field.md#formfielderrortemplatedirective) | Directive | Renders each error the form field shows, in place of its message. |
|
|
417
|
+
| [`FormFieldHintTemplateDirective`](api/form-field.md#formfieldhinttemplatedirective) | Directive | Replaces the `hint` text with projected content, such as text with a link. |
|
|
418
|
+
| [`FormFieldLabelTemplateDirective`](api/form-field.md#formfieldlabeltemplatedirective) | Directive | Replaces the `label` text with projected content. |
|
|
419
|
+
|
|
420
|
+
### Types
|
|
421
|
+
|
|
422
|
+
| Type | Kind | Description |
|
|
423
|
+
| --- | --- | --- |
|
|
424
|
+
| [`FormFieldErrorTemplateContext`](api/form-field.md#formfielderrortemplatecontext) | Interface | The context of a `monaFormFieldErrorTemplate`, rendered once for each error the form field shows. |
|
|
425
|
+
| [`FormFieldOrientation`](api/form-field.md#formfieldorientation) | Type alias | Where a form field puts its label: above the control (`vertical`) or beside it (`horizontal`). |
|
package/docs/forms.md
CHANGED
|
@@ -47,7 +47,15 @@ Without a form, use the control's two-way binding: `[(value)]`, or `[(checked)]`
|
|
|
47
47
|
|
|
48
48
|
## 2. Naming a Control
|
|
49
49
|
|
|
50
|
-
Every control needs a name that assistive technology can read. There are
|
|
50
|
+
Every control needs a name that assistive technology can read. There are five ways to give one.
|
|
51
|
+
|
|
52
|
+
**Put it in a form field.** `mona-form-field` names the control with its label, describes it with its hint, and shows the errors of the bound field once the control is touched. It works for every control in the table below, and for a native input. See [Form Field](form-field.md).
|
|
53
|
+
|
|
54
|
+
```html
|
|
55
|
+
<mona-form-field label="Email" hint="We send the confirmation here.">
|
|
56
|
+
<mona-text-box [formField]="account.email"></mona-text-box>
|
|
57
|
+
</mona-form-field>
|
|
58
|
+
```
|
|
51
59
|
|
|
52
60
|
**Wrap it in a label.** The label's `text` names the control, and a click on it reaches the control. Only `text` can do this, because text projected next to the control can't be referenced without pulling in the control's own content. A wrapped control with no `text` warns in development.
|
|
53
61
|
|
|
@@ -91,15 +99,15 @@ If more than one applies, an explicit `aria-labelledby` wins, and then `aria-lab
|
|
|
91
99
|
| `mona-color-palette` | Yes | Yes | Yes | No | No |
|
|
92
100
|
| `mona-color-picker` | Yes | Yes | Yes | No | No |
|
|
93
101
|
| `mona-color-gradient` | Yes | Yes | Yes | No | No |
|
|
94
|
-
| `mona-numeric-text-box`, `mona-decimal-text-box` | Yes
|
|
102
|
+
| `mona-numeric-text-box`, `mona-decimal-text-box` | Yes | Focus only | Yes | Yes | No |
|
|
95
103
|
| `mona-date-picker` | Yes | Focus only | Yes | Yes | No |
|
|
96
|
-
| `mona-dropdown-list` |
|
|
104
|
+
| `mona-dropdown-list` | Yes | Focus only | Yes | No | No |
|
|
97
105
|
|
|
98
106
|
A check box and a radio button render their own `<label>`, and its text has to stay their name, so a wrapping `mona-label` doesn't name them. Use their `label` input, or `[for]` from outside.
|
|
99
107
|
|
|
100
108
|
A text area is a directive on a native `<textarea>`, so a label's `for` and an `id` work as they do for any native element.
|
|
101
109
|
|
|
102
|
-
"Focus only" means `[for]` makes a click on the label focus the control, but doesn't name it. Name those controls with `inputId` and a string `for`, by wrapping them where the table says yes, or with `aria-labelledby` pointing at the label's `id`. A dropdown list has no native input, so `aria-labelledby` or `aria-label
|
|
110
|
+
"Focus only" means `[for]` makes a click on the label focus the control, but doesn't name it. Name those controls with `inputId` and a string `for`, by wrapping them where the table says yes, or with `aria-labelledby` pointing at the label's `id`. A dropdown list has no native input, so wrap it, or name it with `aria-labelledby` or `aria-label`. The other dropdowns, the pickers and the editor take a wrapping label or form field the same way.
|
|
103
111
|
|
|
104
112
|
---
|
|
105
113
|
|
package/docs/label.md
CHANGED
|
@@ -15,8 +15,7 @@ Entry point: `@nanahoshi/mona-ui/label`
|
|
|
15
15
|
### Out of scope
|
|
16
16
|
|
|
17
17
|
- Floating labels (`FloatingLabel`).
|
|
18
|
-
-
|
|
19
|
-
- Automatically reading a control's `required` state or rendering a required asterisk.
|
|
18
|
+
- Hints, validation errors and a required marker. [`mona-form-field`](form-field.md) adds them around a control, with a label of its own.
|
|
20
19
|
- Automatically generating IDs for controls, or naming custom components that don't support it.
|
|
21
20
|
|
|
22
21
|
A label that animates into a placeholder position is a separate feature that isn't implemented yet. `LabelComponent` and `LabelDirective` only render static, always-visible captions.
|
|
@@ -260,8 +259,8 @@ A string `for` can't work on a component's host element, because a host such as
|
|
|
260
259
|
| `mona-check-box`, `mona-radio-button` | Don't wrap: they render their own `<label>` | Named. A click activates. | Yes, on `inputId` |
|
|
261
260
|
| `mona-switch` | Named. A click toggles. | Named. A click toggles. | No, warns in development |
|
|
262
261
|
| `mona-slider`, `mona-range-slider`, `mona-rating`, `mona-segmented`, `mona-radio-group`, `mona-button-group`, `mona-color-palette`, `mona-color-picker`, `mona-color-gradient` | Named. A click focuses. | Named. A click focuses. | No, warns in development |
|
|
263
|
-
| `mona-numeric-text-box`, `mona-decimal-text-box`, `mona-date-picker`
|
|
264
|
-
| `mona-dropdown-list`
|
|
262
|
+
| `mona-numeric-text-box`, `mona-decimal-text-box`, `mona-date-picker`, `mona-time-picker`, `mona-datetime-picker`, `mona-combo-box`, `mona-auto-complete` | Named. A click focuses. | Focus only, not named | Yes, on `inputId` |
|
|
263
|
+
| `mona-dropdown-list`, `mona-multi-select`, `mona-editor` | Named. A click focuses. | Focus only, not named | No: they have no native input |
|
|
265
264
|
| Other controls | Not connected, unless they hold a native input | Focus only, not named | Only on a native input's `id` |
|
|
266
265
|
|
|
267
266
|
A control that is focus only with `[for]` still needs a name. Give it `aria-labelledby` pointing at the label's `id`, as the dropdown list examples do, or use `inputId` where the control has one:
|
|
@@ -289,7 +288,7 @@ Do not wrap `CheckBoxComponent` or `RadioButtonComponent` in `<mona-label>`, and
|
|
|
289
288
|
- Both APIs render a real native `<label>` — no ARIA `role` is added, and none is needed.
|
|
290
289
|
- A wrapped control is named by the `text` input or its own `aria-label` or `aria-labelledby`. Text projected next to it can't name it, and `mona-label` warns in development if nothing else does.
|
|
291
290
|
- A reference in `for` names the control only if the control supports it. Other targets, such as a native element reference, `mona-dropdown-list`, `mona-numeric-text-box` or `mona-date-picker`, only receive focus on click, so give them their own `aria-label` or `aria-labelledby`, or point a string `for` at their `inputId` where they have one.
|
|
292
|
-
- `LabelComponent` never reads a projected control's `required` state and never renders a required asterisk
|
|
291
|
+
- `LabelComponent` never reads a projected control's `required` state and never renders a required asterisk. Use `mona-form-field` for that, and for hints and errors.
|
|
293
292
|
- Do not wrap `CheckBoxComponent` or `RadioButtonComponent` in `<mona-label>`; both already render their own native `<label>`, and nesting `<label>` elements is invalid HTML. Use their built-in `label` input, or `[for]="ref"` from outside.
|
|
294
293
|
|
|
295
294
|
## API
|
package/docs/numeric-textbox.md
CHANGED
|
@@ -212,7 +212,7 @@ The `invalid` and `touched` inputs are written by `FormField` when the control i
|
|
|
212
212
|
|
|
213
213
|
**Consumer responsibilities**
|
|
214
214
|
|
|
215
|
-
- Name the control. Point a `<label for>` or `<mona-label for>` at `inputId`, wrap it in `<mona-label text="...">`
|
|
215
|
+
- Name the control. Point a `<label for>` or `<mona-label for>` at `inputId`, wrap it in `<mona-label text="...">` or `<mona-form-field label="...">`, or set `aria-labelledby` or `aria-label`. The placeholder is not a label. `[for]` bound to a template reference only moves focus to the input on click, and doesn't name it.
|
|
216
216
|
|
|
217
217
|
## API
|
|
218
218
|
|