@d4k-ui/nbx-inputs 21.0.11 → 21.1.1

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/CHANGELOG.md ADDED
@@ -0,0 +1,44 @@
1
+ # Changelog
2
+
3
+ ## 21.0.12
4
+
5
+ ### Breaking changes
6
+
7
+ - **Textarea** – the value is plain text now (it used to be the inner HTML of the field). The field uses
8
+ `contenteditable="plaintext-only"`, which needs Firefox 136 or newer. The public `onPaste()` method is removed.
9
+ - **Types** – `NbxNbxValidatorsErrorsMessageValue` is renamed to `NbxValidatorsErrorsMessageValue`.
10
+ - **Date pickers** – `NbxInputDatePickerComponent` and `NbxInputDateTimePickerComponent` are typed with `Date`
11
+ instead of `any`.
12
+ - **`nbxClipboard`** – the click on the host no longer propagates to parent elements.
13
+ - **Peer dependencies** – `@d4k-ui/theme` 21.1.2 or newer is required (was 21.0.12). With it neither the theme nor
14
+ this library needs `@angular/animations`.
15
+
16
+ ### Added
17
+
18
+ - **Tags** – `NbxInputTagsComponent` (`nbx-input-tags`) is exported and works as a form control.
19
+ - **Period select** – `NbxInputPeriodSelectComponent` (`nbx-input-period-select`) and its `RangeObject` /
20
+ `PeriodType` types are exported.
21
+ - **Number** – negative values can be typed; typed and pasted text is cleaned before it reaches the form control.
22
+ - **Text** – the `type` input (`text`, `email`, `search`, `tel`) is applied to the field.
23
+ - **Errors** – a message function also receives error values that are plain strings.
24
+ - The `touched` signal of the inputs follows `markAsTouched()` / `markAsUntouched()` of the control.
25
+
26
+ ### Changed
27
+
28
+ - **`nbxClipboard`** – the "Copied" tooltip is animated with `animate.enter` / `animate.leave`. The library no
29
+ longer uses `@angular/animations`, so the application does not need it or `provideAnimations()`. The fade-out
30
+ when the tooltip hides is now visible; it used to be cut off.
31
+ - **Peer dependencies** – `rxjs` (`^7.4.0`) is listed; the library imports it directly. Angular already requires
32
+ it, so nothing changes for an application.
33
+
34
+ ### Fixed
35
+
36
+ - **Date pickers** – with `format` set, the value the form had on start was shown empty or with day and month
37
+ swapped.
38
+ - **Number** – Enter in the field submits the form again; it used to be swallowed.
39
+ - **Period select** – the period given in `initValue` is written to the form control; it used to be only shown
40
+ and emitted.
41
+ - **Number** – switching to `onlyInt` drops the fraction in the form control too, not only in the field.
42
+ - **Date-range picker** – the change it makes to the shared `NbDateService` is undone when the last picker is
43
+ destroyed.
44
+ - **Messages** – wording of the `nbDatepickerMin` / `nbDatepickerMax` messages.
package/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Akveo.
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.
22
+
package/README.md CHANGED
@@ -1,14 +1,498 @@
1
- Nebular extended form with validation errors and some extensions
1
+ # @d4k-ui/nbx-inputs
2
2
 
3
- ## Works only on Angular 19 and above
4
- ## For correct operation you need to specify the root provider in your application:
5
- - NbThemeModule.forRoot(),
6
- - NbEvaIconsModule,
7
- - NbDatepickerModule.forRoot(),
8
- - NbToastrModule.forRoot(),
9
- - NbTimepickerModule.forRoot(),
10
- - NbDialogModule.forRoot(),
3
+ Form inputs for Angular built on top of [`@d4k-ui/theme`](https://www.npmjs.com/package/@d4k-ui/theme).
4
+ Every input is a standalone component that plugs into Angular reactive forms and brings its own label,
5
+ validation messages, required marker and an optional copy-to-clipboard button.
6
+
7
+ - [Compatibility](#compatibility)
8
+ - [Installation](#installation)
9
+ - [Setup](#setup)
10
+ - [Quick start](#quick-start)
11
+ - [Options shared by all inputs](#options-shared-by-all-inputs)
12
+ - [Inputs](#inputs)
13
+ - [Validation and error messages](#validation-and-error-messages)
14
+ - [Form helpers](#form-helpers)
15
+ - [Directives](#directives)
16
+ - [Building your own input](#building-your-own-input)
17
+ - [License](#license)
18
+
19
+ ## Compatibility
20
+
21
+ | Package | Version | Angular | UI kit |
22
+ | -------------------- | ------- | ------- | ----------------------- |
23
+ | `@d4k-ui/nbx-inputs` | 21.x | 21.x | `@d4k-ui/theme` ^21.1.2 |
24
+
25
+ Before the move to `@d4k-ui/theme` the library was published as `nbx-inputs` and depended on `@nebular/theme`.
26
+
27
+ The textarea relies on `contenteditable="plaintext-only"`, which needs Firefox 136 or newer
28
+ (Chrome, Edge and Safari have supported it for years).
29
+
30
+ ## Installation
31
+
32
+ ```bash
33
+ npm install @d4k-ui/nbx-inputs @d4k-ui/theme @angular/cdk
34
+ ```
35
+
36
+ Peer dependencies: `@angular/common`, `@angular/core`, `@angular/forms` ^21.2.8, `@angular/cdk` ^21.2.6,
37
+ `@d4k-ui/theme` ^21.1.2, `rxjs` ^7.4.0.
38
+
39
+ ## Setup
40
+
41
+ ### Providers
42
+
43
+ Register the theme once in the application config. The other providers are needed only by the inputs named in
44
+ the comments.
45
+
46
+ ```ts
47
+ import { ApplicationConfig } from '@angular/core';
48
+ import { provideNbDialog, provideNbNativeDateService, provideNbTheme, provideNbToastr } from '@d4k-ui/theme';
49
+
50
+ export const appConfig: ApplicationConfig = {
51
+ providers: [
52
+ provideNbTheme(), // theme, overlays, animations and the Eva icon pack
53
+ provideNbNativeDateService(), // only for the date, date-time and date-range pickers
54
+ provideNbDialog(), // only for the period select
55
+ provideNbToastr(), // only for the period select
56
+ ],
57
+ };
58
+ ```
59
+
60
+ To customise date parsing or the first day of the week, provide your own `NbDateService` instead of
61
+ `provideNbNativeDateService()`:
62
+
63
+ ```ts
64
+ { provide: NbDateService, useClass: MyDateService } // MyDateService extends NbNativeDateService
65
+ ```
66
+
67
+ ### Styles
68
+
69
+ The inputs are styled with the theme's CSS variables, so the theme's global styles have to be installed.
70
+
71
+ ```scss
72
+ // themes.scss
73
+ @forward '@d4k-ui/theme/styles/theming';
74
+ @use '@d4k-ui/theme/styles/theming' as *;
75
+ @use '@d4k-ui/theme/styles/themes/default';
76
+
77
+ $nb-themes: nb-register-theme((), default, default);
78
+ ```
79
+
80
+ ```scss
81
+ // styles.scss
82
+ @use 'themes' as *;
83
+ @use '@d4k-ui/theme/styles/globals' as *;
84
+
85
+ @include nb-install {
86
+ @include nb-theme-global;
87
+ }
88
+ ```
89
+
90
+ Follow the layout requirements of `@d4k-ui/theme` for overlays (selects, date pickers, tooltips). The demo
91
+ application of this repository renders everything inside `<nb-layout>`.
92
+
93
+ ## Quick start
94
+
95
+ ```ts
96
+ import { ChangeDetectionStrategy, Component } from '@angular/core';
97
+ import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
98
+ import {
99
+ markAllControlsAsDirty,
100
+ NbxInputNumberComponent,
101
+ NbxInputSelectComponent,
102
+ NbxInputTextComponent,
103
+ NbxSelectOption,
104
+ NbxValidators,
105
+ } from '@d4k-ui/nbx-inputs';
106
+
107
+ @Component({
108
+ selector: 'app-user-form',
109
+ imports: [ReactiveFormsModule, NbxInputTextComponent, NbxInputNumberComponent, NbxInputSelectComponent],
110
+ template: `
111
+ <form [formGroup]="form" (ngSubmit)="submit()">
112
+ <nbx-input-text formControlName="email" type="email" placeholder="name@example.com">Email</nbx-input-text>
113
+ <nbx-input-number formControlName="age" [onlyInt]="true">Age</nbx-input-number>
114
+ <nbx-input-select formControlName="role" [options]="roles">Role</nbx-input-select>
115
+ <button type="submit">Save</button>
116
+ </form>
117
+ `,
118
+ changeDetection: ChangeDetectionStrategy.OnPush,
119
+ })
120
+ export class UserFormComponent {
121
+ readonly roles: NbxSelectOption<string>[] = [
122
+ { title: 'Admin', value: 'admin' },
123
+ { title: 'User', value: 'user' },
124
+ ];
125
+
126
+ readonly form = new FormGroup({
127
+ email: new FormControl('', [Validators.required, NbxValidators.email]),
128
+ age: new FormControl<number | null>(null, [Validators.min(0)]),
129
+ role: new FormControl<string | null>(null, [Validators.required]),
130
+ });
131
+
132
+ submit(): void {
133
+ if (this.form.invalid) {
134
+ markAllControlsAsDirty(this.form); // reveals the error messages of untouched fields
135
+ return;
136
+ }
137
+ console.log(this.form.value);
138
+ }
139
+ }
140
+ ```
141
+
142
+ What you get without extra markup:
143
+
144
+ - **Label** – the content of the element (`Email`, `Age`, `Role` above).
145
+ - **Required marker** – an asterisk is added when the control has `Validators.required`.
146
+ - **Error messages** – shown under the field once the control is invalid **and dirty**.
147
+ - **Form binding** – use `formControlName` or `[formControl]`. An input also works without a control; in that
148
+ case read the value from its output.
149
+
150
+ ## Options shared by all inputs
151
+
152
+ | Input | Type | Default | Description |
153
+ | ---------------------- | ----------------------------------------------------------------------------------- | ---------- | ----------------------------------------------------------------------------------------------------------------- |
154
+ | `labelPosition` | `'block' \| 'block-center' \| 'inline' \| 'inline-reverse' \| 'float' \| 'none'` | – | Where the label is placed. Without a value the label sits above the field. `'none'` removes it. |
155
+ | `placeholder` | `string` | `''` | Placeholder text. |
156
+ | `status` | `'basic' \| 'primary' \| 'success' \| 'warning' \| 'danger' \| 'info' \| 'control'` | `'basic'` | Theme colour of the field. |
157
+ | `size` | `'tiny' \| 'small' \| 'medium' \| 'large' \| 'giant'` | `'medium'` | Theme size of the field. |
158
+ | `labelInfo` | `{ type: 'icon' \| 'text'; text: string; status?: NbComponentStatus }` | – | Hint next to the label: an info icon with a tooltip (`'icon'`) or plain text (`'text'`). |
159
+ | `withCopyButton` | `boolean \| 'string' \| 'json'` | `false` | Adds a button that copies the current value. The bare attribute copies as text, `'json'` copies `JSON.stringify`. |
160
+ | `customErrorsMessages` | `NbxInputValidationMessages` | `{}` | Overrides or adds error messages for this field, see [Validation](#validation-and-error-messages). |
161
+ | `id` | `string` | generated | `id` of the inner field. The label points to it. |
162
+
163
+ ```html
164
+ <nbx-input-text
165
+ formControlName="email"
166
+ labelPosition="float"
167
+ status="primary"
168
+ size="small"
169
+ withCopyButton
170
+ [labelInfo]="{ type: 'icon', text: 'Used for notifications' }">
171
+ Email
172
+ </nbx-input-text>
173
+ ```
174
+
175
+ ## Inputs
176
+
177
+ Options are described with `NbxSelectOption`:
178
+
179
+ ```ts
180
+ interface NbxSelectOption<T = any, C = any> {
181
+ title: string; // text shown to the user
182
+ value: T; // value written to the form control
183
+ disabled?: boolean;
184
+ customData?: C; // extra data, e.g. the CSS colour for the colour select
185
+ }
186
+ ```
187
+
188
+ ### Text – `nbx-input-text`
189
+
190
+ `NbxInputTextComponent`, value: `string`.
191
+
192
+ | Name | Kind | Type | Default | Description |
193
+ | -------------- | ------ | ---------------------------------------- | -------- | ---------------------------- |
194
+ | `type` | input | `'text' \| 'email' \| 'search' \| 'tel'` | `'text'` | Native input type. |
195
+ | `(inputKeyup)` | output | `string` | | Current text on every input. |
196
+
197
+ ### Number – `nbx-input-number`
198
+
199
+ `NbxInputNumberComponent`, value: `number | null`.
200
+
201
+ | Name | Kind | Type | Default | Description |
202
+ | -------------- | ------ | ---------------- | ------- | -------------------------------------- |
203
+ | `onlyInt` | input | `boolean` | `false` | Accept whole numbers only. |
204
+ | `min`, `max` | input | `number \| null` | `null` | Native `min` / `max` attributes. |
205
+ | `(inputKeyup)` | output | `string` | | Current (cleaned) text on every input. |
206
+
207
+ - Only digits, a leading minus and, unless `onlyInt` is set, one dot can be typed. Enter submits the form as usual.
208
+ - Pasted text is cleaned before it reaches the form: `1.5` becomes `1` with `onlyInt`, `1e5` becomes `100000`.
209
+ - A value set by the form is shown as it is, it is never rewritten.
210
+ - Arrow keys and the mouse wheel do not change the value.
211
+
212
+ ### Textarea – `nbx-input-textarea`
213
+
214
+ `NbxInputTextareaComponent`, value: `string` (plain text, line breaks as `\n`).
215
+
216
+ | Name | Kind | Type | Default | Description |
217
+ | -------------- | ------ | ---------------- | ------- | ---------------------------- |
218
+ | `rows` | input | `number \| null` | `null` | Minimum height in text rows. |
219
+ | `(inputKeyup)` | output | `string` | | Current text on every input. |
220
+
221
+ The field grows with its content. Pasted content is inserted as plain text.
222
+
223
+ ### Select – `nbx-input-select`
224
+
225
+ `NbxInputSelectComponent`, value: the option value, or an array of values with `multiple`.
226
+
227
+ | Name | Kind | Type | Default | Description |
228
+ | ------------------ | ------ | -------------------- | -------- | ------------------------------------------------------------ |
229
+ | `options` | input | `NbxSelectOption[]` | required | Options to choose from. |
230
+ | `multiple` | input | `boolean` | `false` | Multi-select. Adds a "Select all" option and a clear button. |
231
+ | `optionsListClass` | input | `string` | `''` | CSS class for the dropdown panel. |
232
+ | `(selectedChange)` | output | `string \| string[]` | | Value after the user changed the selection. |
233
+
234
+ ### Autocomplete – `nbx-input-autocomplete`
235
+
236
+ `NbxInputAutocompleteComponent`, value: the value of the chosen option. The field shows the option title and
237
+ filters the list while typing.
238
+
239
+ | Name | Kind | Type | Default | Description |
240
+ | ------------------------- | ------ | ------------------- | -------- | -------------------------------------------------------------------------- |
241
+ | `options` | input | `NbxSelectOption[]` | required | Options to search in. |
242
+ | `anyStringInput` | input | `boolean` | `false` | Also accept text that matches no option; the typed text becomes the value. |
243
+ | `enableVirtualScroll` | input | `boolean` | `false` | Render the list with virtual scroll, for long lists. |
244
+ | `virtualScrollItemSizePX` | input | `number` | `38` | Height of one option in px, change it if you restyle the options. |
245
+ | `optionsListClass` | input | `string` | `''` | CSS class for the dropdown panel. |
246
+ | `(selectChanged)` | output | `string` | | Value of the option the user picked. |
247
+
248
+ ### Multiselect autocomplete – `nbx-input-multiselect-autocomplete`
249
+
250
+ `NbxInputMultiselectAutocompleteComponent`, value: `(string | number)[]`. Selected titles are shown in the
251
+ field, typing filters the list. Has "Select all" and a clear button.
252
+
253
+ | Name | Kind | Type | Default | Description |
254
+ | ------------------------- | ------ | ---------------------- | -------- | ----------------------------------------------------------------- |
255
+ | `options` | input | `NbxSelectOption[]` | required | Options to choose from. |
256
+ | `selectedOnTop` | input | `boolean` | `false` | Move the selected options to the top of the list. |
257
+ | `enableVirtualScroll` | input | `boolean` | `false` | Render the list with virtual scroll, for long lists. |
258
+ | `virtualScrollItemSizePX` | input | `number` | `38` | Height of one option in px, change it if you restyle the options. |
259
+ | `optionsListClass` | input | `string` | `''` | CSS class for the dropdown panel. |
260
+ | `(selectedChange)` | output | `(string \| number)[]` | | Value after the user changed the selection. |
261
+
262
+ ### Colour select – `nbx-input-color-select`
263
+
264
+ `NbxInputColorSelectComponent`, value: `string | null`. Put the CSS colour into `customData` to show a colour
265
+ swatch; options without `customData` are shown as text. `labelPosition="float"` is not supported.
266
+
267
+ | Name | Kind | Type | Default | Description |
268
+ | ----------------- | ------ | ----------------------------------- | -------- | ----------------------------------------- |
269
+ | `options` | input | `NbxSelectOption<string, string>[]` | required | Colours to choose from. |
270
+ | `withTitle` | input | `boolean` | `true` | Show the option title next to the swatch. |
271
+ | `(colorSelected)` | output | `string \| null` | | Chosen value, `null` when it is cleared. |
272
+
273
+ ```ts
274
+ colors: NbxSelectOption<string, string>[] = [{ title: 'Red', value: 'red', customData: '#ff0606' }];
275
+ ```
276
+
277
+ ### Tags – `nbx-input-tags`
278
+
279
+ `NbxInputTagsComponent`, value: `(string | number)[]`. A tag is added by typing text and pressing Enter or by
280
+ picking one of the options; each tag has a remove button.
281
+
282
+ | Name | Kind | Type | Default | Description |
283
+ | ------------------ | ------ | ---------------------- | -------- | --------------------------------- |
284
+ | `options` | input | `NbxSelectOption[]` | required | Suggestions; pass `[]` for none. |
285
+ | `(selectedChange)` | output | `(string \| number)[]` | | Tags after the user changed them. |
286
+
287
+ ### Period select – `nbx-input-period-select`
288
+
289
+ `NbxInputPeriodSelectComponent`, value: `{ start: Date; end: Date }` (exported as `RangeObject`). A select with
290
+ ready-made periods – yesterday, this week, month, quarter, half year, year – and a "Custom range" option that
291
+ opens a calendar in a dialog.
292
+
293
+ | Name | Kind | Type | Default | Description |
294
+ | ----------------- | ------ | ------------- | ------- | ----------------------------------------------------------- |
295
+ | `initValue` | input | `PeriodType` | – | Period that is selected (and written to the form) on start. |
296
+ | `(rangeSelected)` | output | `RangeObject` | | Range of the period the user picked. |
297
+
298
+ `PeriodType` is `'yesterday' | 'this_week' | 'this_month' | 'this_quarter' | 'this_half_year' | 'this_year'`.
299
+
300
+ - Needs `provideNbDialog()` and `provideNbToastr()` from `@d4k-ui/theme` in the application config.
301
+ - Of the shared options only `labelPosition`, `placeholder`, `status` and `size` apply. The label is always
302
+ "Period".
303
+ - The field shows the chosen period, not the dates: a range written to the form control from code is kept as the
304
+ value but is not reflected in the field.
305
+
306
+ ### Date picker – `nbx-input-date-picker`
307
+
308
+ `NbxInputDatePickerComponent`, value: `Date | null`. Has a clear button.
309
+
310
+ | Name | Kind | Type | Default | Description |
311
+ | -------------------- | ------ | ------------------------ | ------- | ---------------------------------- |
312
+ | `minDate`, `maxDate` | input | `Date \| string \| null` | `null` | Selectable range. |
313
+ | `format` | input | `string` | – | Display format, e.g. `dd.MM.yyyy`. |
314
+ | `(dateChanged)` | output | `Date` | | Date the user picked. |
315
+
316
+ ### Date-time picker – `nbx-input-date-time-picker`
317
+
318
+ `NbxInputDateTimePickerComponent`, value: `Date | null`. Has a clear button.
319
+
320
+ | Name | Kind | Type | Default | Description |
321
+ | -------------------- | ------ | ------------------------ | ------- | ---------------------------------------- |
322
+ | `minDate`, `maxDate` | input | `Date \| string \| null` | `null` | Selectable range. |
323
+ | `format` | input | `string` | – | Display format, e.g. `dd.MM.yyyy HH:mm`. |
324
+ | `step` | input | `number` | `10` | Step of the time list in minutes. |
325
+ | `(dateChanged)` | output | `Date` | | Date and time the user picked. |
326
+
327
+ ### Date-range picker – `nbx-input-date-range-picker`
328
+
329
+ `NbxInputDateRangePickerComponent`, value: `{ start?: Date; end?: Date } | null`. Has a clear button.
330
+
331
+ | Name | Kind | Type | Default | Description |
332
+ | -------------------- | ------ | -------------------------------------- | ------- | ---------------------------------- |
333
+ | `minDate`, `maxDate` | input | `Date \| null` | `null` | Selectable range. |
334
+ | `format` | input | `string` | – | Display format, e.g. `dd.MM.yyyy`. |
335
+ | `(dateRangeChanged)` | output | `{ start?: Date; end?: Date } \| null` | | Range the user picked. |
336
+
337
+ Out-of-range dates produce the `nbDatepickerMin` / `nbDatepickerMax` errors, which have default messages.
338
+
339
+ ## Validation and error messages
340
+
341
+ Errors are shown under the field once the control is **invalid and dirty**. Fields the user has not edited stay
342
+ quiet, so mark the form dirty on submit to reveal them:
343
+
344
+ ```ts
345
+ import { markAllControlsAsDirty } from '@d4k-ui/nbx-inputs';
346
+
347
+ markAllControlsAsDirty(this.form);
348
+ ```
349
+
350
+ ### Built-in messages
351
+
352
+ | Error key | Message |
353
+ | ----------------- | ---------------------------------------------- |
354
+ | `required` | Required field |
355
+ | `email` | Invalid email |
356
+ | `url` | Invalid URL. Valid example: http://example.com |
357
+ | `min` | Min value {min} |
358
+ | `max` | Max value {max} |
359
+ | `minlength` | Minimum length must be {requiredLength} |
360
+ | `maxlength` | Maximum length must be {requiredLength} |
361
+ | `nbDatepickerMin` | Date is less than possible |
362
+ | `nbDatepickerMax` | Date is later than possible |
363
+
364
+ The defaults are exported as `NbxValidatorsErrorsMessage`.
365
+
366
+ ### Custom messages
367
+
368
+ Pass `customErrorsMessages` to a field. A message is either a string or a function that receives one property
369
+ of the error object (`dataKey` names that property).
370
+
371
+ ```ts
372
+ import { NbxInputValidationMessages } from '@d4k-ui/nbx-inputs';
373
+
374
+ messages: NbxInputValidationMessages = {
375
+ required: 'Please enter the site address',
376
+ minlength: {
377
+ dataKey: 'requiredLength', // Validators.minLength returns { requiredLength, actualLength }
378
+ messageFunction: (length) => `At least ${length} characters`,
379
+ },
380
+ };
381
+ ```
382
+
383
+ ```html
384
+ <nbx-input-text formControlName="site" [customErrorsMessages]="messages">Site</nbx-input-text>
385
+ ```
386
+
387
+ For your own validators the simplest way is to return the message as the error value – an error whose key has
388
+ no registered message is shown as is when its value is a string:
389
+
390
+ ```ts
391
+ const noSpaces: ValidatorFn = (control) =>
392
+ /\s/.test(control.value ?? '') ? { noSpaces: 'Spaces are not allowed' } : null;
393
+ ```
394
+
395
+ Any other unknown error is shown as "Not acceptable value".
396
+
397
+ ### Validators
398
+
399
+ `NbxValidators.email` and `NbxValidators.url` (http/https addresses) are ready-made validators. Both accept an
400
+ empty value, combine them with `Validators.required` when the field is mandatory.
401
+
402
+ ## Form helpers
403
+
404
+ | Function | Description |
405
+ | --------------------------------------------------------- | --------------------------------------------------------------------- |
406
+ | `markAllControlsAsDirty(control)` | Marks a control, group or array and everything inside it as dirty. |
407
+ | `getOnlyDirtyControlsValuesRecursive(form, excludeKeys?)` | Returns only the values the user changed – handy for PATCH requests. |
408
+ | `filterOptionsBySearch(options, query)` | Options whose title contains the query (case-insensitive). |
409
+ | `filterAndSortOptionsBySearch(options, query)` | Same, sorted so that earlier matches come first. |
410
+ | `sortOptionsBySearchIndex(list, query, key)` | Sorts any list by the position of the query in `item[key]`. |
411
+ | `getFilteredBySearchArray(items, query)` | Items in which any nested string or number contains the query. |
412
+ | `isQueryIncludesInObject(object, query)` | Whether any nested string or number of the object contains the query. |
413
+ | `getRandomId(prefix?, suffix?)` | Random id for DOM elements. |
414
+ | `resizeObservable(element, throttleMs?)` | Observable of `ResizeObserver` entries for an element. |
415
+
416
+ ## Directives
417
+
418
+ ### `nbxClipboard`
419
+
420
+ Copies a value on click and shows a short "Copied" tooltip. The host is disabled while the value is empty.
421
+
422
+ ```html
423
+ <button type="button" [nbxClipboard]="form.value" nbxClipboardCopyType="json">Copy</button>
424
+ ```
425
+
426
+ `nbxClipboardCopyType` is `'string'` (default; arrays are joined with `, `) or `'json'`.
427
+
428
+ ### `nbxIntOrFloat`
429
+
430
+ Restricts a text or number `<input>` to numbers. This is what `nbx-input-number` uses internally.
431
+
432
+ ```html
433
+ <input type="text" [formControl]="amount" nbxIntOrFloat="float" />
434
+ <input type="text" [formControl]="count" nbxIntOrFloat="int" />
435
+ ```
436
+
437
+ ## Building your own input
438
+
439
+ Extend `NbxControlValueAccessorBaseDirective` and wrap the field in `nbx-input-wrapper` to get the label,
440
+ required marker, error messages, copy button and all [shared options](#options-shared-by-all-inputs).
441
+
442
+ ```ts
443
+ import { ChangeDetectionStrategy, Component, forwardRef } from '@angular/core';
444
+ import { NG_VALUE_ACCESSOR, ReactiveFormsModule } from '@angular/forms';
445
+ import { NbInputDirective } from '@d4k-ui/theme';
446
+ import { NbxControlValueAccessorBaseDirective, NbxInputWrapperComponent } from '@d4k-ui/nbx-inputs';
447
+
448
+ @Component({
449
+ selector: 'app-input-password',
450
+ imports: [ReactiveFormsModule, NbInputDirective, NbxInputWrapperComponent],
451
+ providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => InputPasswordComponent), multi: true }],
452
+ template: `
453
+ <nbx-input-wrapper [context]="getContext()" [inputId]="getInputId('input-password')">
454
+ <ng-content label-content></ng-content>
455
+ <input
456
+ nbInput
457
+ fullWidth
458
+ type="password"
459
+ [formControl]="control"
460
+ [id]="getInputId('input-password')"
461
+ [status]="status()"
462
+ [size]="size()"
463
+ [placeholder]="placeholder()" />
464
+ </nbx-input-wrapper>
465
+ `,
466
+ changeDetection: ChangeDetectionStrategy.OnPush,
467
+ })
468
+ export class InputPasswordComponent extends NbxControlValueAccessorBaseDirective<string> {}
469
+ ```
470
+
471
+ ```html
472
+ <app-input-password formControlName="password" labelPosition="float">Password</app-input-password>
473
+ ```
474
+
475
+ What the base class gives you:
476
+
477
+ | Member | Description |
478
+ | ------------------------------------------------------------------------ | ----------------------------------------------------------------------------------- |
479
+ | `control` | The form control bound to the component. Bind an inner native field to it directly. |
480
+ | `value()`, `disabled()`, `dirty()`, `touched()`, `invalid()`, `errors()` | Signals that mirror the state of the control. |
481
+ | `valueChanged(value)` | Call it when a custom widget (not bound to `control`) produces a new value. |
482
+ | `getContext()` | State for `nbx-input-wrapper`. |
483
+ | `getInputId(prefix)` | The `id` input, or a generated id. |
484
+
485
+ `nbx-input-wrapper` inputs for special cases:
486
+
487
+ | Input | Description |
488
+ | ------------------------ | ----------------------------------------------------------------------------------------- |
489
+ | `isInputHasButton` | The field has its own button at the end; moves the copy button to the left of it. |
490
+ | `searchHasValue` | For a floating label when the visible text lives in a separate search control. |
491
+ | `floatLabelOnlyBySearch` | For a floating label when the value is not shown in the field itself (chips and similar). |
492
+
493
+ `NbxInputLabelComponent` (`nbx-input-label`) and `NbxInputErrorsComponent` (`nbx-input-errors`) are exported
494
+ too, for layouts that cannot use the wrapper.
11
495
 
12
496
  ## License
13
497
 
14
- [MIT](LICENSE.txt) license.
498
+ [MIT](LICENSE.txt)