@spartan-ng/brain 0.0.1-alpha.613 → 0.0.1-alpha.628

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.
@@ -1,8 +1,8 @@
1
1
  import { ActiveDescendantKeyManager } from '@angular/cdk/a11y';
2
2
  import * as i0 from '@angular/core';
3
- import { InjectionToken, inject, DestroyRef, ElementRef, Injector, input, linkedSignal, computed, signal, effect, forwardRef, Directive, output, contentChild, contentChildren, untracked, afterNextRender, TemplateRef, ViewContainerRef, PLATFORM_ID, booleanAttribute } from '@angular/core';
3
+ import { InjectionToken, inject, forwardRef, Injector, input, booleanAttribute, linkedSignal, output, model, contentChildren, effect, untracked, afterNextRender, Directive, TemplateRef, ViewContainerRef, computed, ElementRef, signal, PLATFORM_ID } from '@angular/core';
4
4
  import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
5
- import { DefaultValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
5
+ import { NG_VALUE_ACCESSOR } from '@angular/forms';
6
6
  import { startWith } from 'rxjs/operators';
7
7
  import { isPlatformBrowser } from '@angular/common';
8
8
 
@@ -11,11 +11,6 @@ function provideBrnCommandItem(command) {
11
11
  return { provide: BrnCommandItemToken, useExisting: command };
12
12
  }
13
13
 
14
- const BrnCommandSearchInputToken = new InjectionToken('BrnCommandSearchInputToken');
15
- function provideBrnCommandSearchInput(command) {
16
- return { provide: BrnCommandSearchInputToken, useExisting: command };
17
- }
18
-
19
14
  const BrnCommandToken = new InjectionToken('BrnCommandToken');
20
15
  function provideBrnCommand(command) {
21
16
  return { provide: BrnCommandToken, useExisting: command };
@@ -34,74 +29,11 @@ function injectBrnCommandConfig() {
34
29
  return inject(BrnCommandConfigToken, { optional: true }) ?? defaultConfig;
35
30
  }
36
31
 
37
- class BrnCommandSearchInput extends DefaultValueAccessor {
38
- _destroyRef = inject(DestroyRef);
39
- _el = inject(ElementRef);
40
- _injector = inject(Injector);
41
- _command = injectBrnCommand();
42
- /** The initial value of the search input */
43
- value = input('');
44
- /** @internal The mutable value of the search input */
45
- mutableValue = linkedSignal(this.value);
46
- /** @internal The "real" value of the search input */
47
- valueState = computed(() => this.mutableValue());
48
- /** The id of the active option */
49
- _activeDescendant = signal(undefined);
50
- ngOnInit() {
51
- this._command.keyManager.change
52
- .pipe(startWith(this._command.keyManager.activeItemIndex), takeUntilDestroyed(this._destroyRef))
53
- .subscribe(() => this._activeDescendant.set(this._command.keyManager.activeItem?.id()));
54
- effect(() => {
55
- this._el.nativeElement.value = this.valueState();
56
- }, { injector: this._injector });
57
- }
58
- /** Listen for changes to the input value */
59
- onInput() {
60
- this.mutableValue.set(this._el.nativeElement.value);
61
- }
62
- /** Listen for keydown events */
63
- onKeyDown(event) {
64
- this._command.keyManager.onKeydown(event);
65
- }
66
- /** CONROL VALUE ACCESSOR */
67
- writeValue(value) {
68
- super.writeValue(value);
69
- if (value) {
70
- this.mutableValue.set(value);
71
- }
72
- }
73
- /** @nocollapse */ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.7", ngImport: i0, type: BrnCommandSearchInput, deps: null, target: i0.ɵɵFactoryTarget.Directive });
74
- /** @nocollapse */ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "20.0.7", type: BrnCommandSearchInput, isStandalone: true, selector: "input[brnCommandSearchInput]", inputs: { value: { classPropertyName: "value", publicName: "value", isSignal: true, isRequired: false, transformFunction: null } }, host: { attributes: { "role": "combobox", "aria-autocomplete": "list" }, listeners: { "keydown": "onKeyDown($event)", "input": "onInput()" }, properties: { "attr.aria-activedescendant": "_activeDescendant()" } }, providers: [
75
- provideBrnCommandSearchInput(BrnCommandSearchInput),
76
- {
77
- provide: NG_VALUE_ACCESSOR,
78
- useExisting: forwardRef((() => BrnCommandSearchInput)),
79
- multi: true,
80
- },
81
- ], usesInheritance: true, ngImport: i0 });
82
- }
83
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.7", ngImport: i0, type: BrnCommandSearchInput, decorators: [{
84
- type: Directive,
85
- args: [{
86
- selector: 'input[brnCommandSearchInput]',
87
- providers: [
88
- provideBrnCommandSearchInput(BrnCommandSearchInput),
89
- {
90
- provide: NG_VALUE_ACCESSOR,
91
- useExisting: forwardRef((() => BrnCommandSearchInput)),
92
- multi: true,
93
- },
94
- ],
95
- host: {
96
- role: 'combobox',
97
- 'aria-autocomplete': 'list',
98
- '[attr.aria-activedescendant]': '_activeDescendant()',
99
- '(keydown)': 'onKeyDown($event)',
100
- '(input)': 'onInput()',
101
- },
102
- }]
103
- }] });
104
-
32
+ const BRN_COMMAND_VALUE_ACCESSOR = {
33
+ provide: NG_VALUE_ACCESSOR,
34
+ useExisting: forwardRef(() => BrnCommand),
35
+ multi: true,
36
+ };
105
37
  class BrnCommand {
106
38
  static _id = 0;
107
39
  _injector = inject(Injector);
@@ -110,20 +42,23 @@ class BrnCommand {
110
42
  id = input(`brn-command-${++BrnCommand._id}`);
111
43
  /** A custom filter function to use when searching. */
112
44
  filter = input(this._config.filter);
45
+ /** Whether the command is disabled */
46
+ disabled = input(false, { transform: booleanAttribute });
47
+ _disabled = linkedSignal(this.disabled);
48
+ /** @internal The disabled state as a readonly signal */
49
+ disabledState = this._disabled.asReadonly();
113
50
  /** when the selection has changed */
114
51
  valueChange = output();
115
- /** @internal The search query */
116
- search = computed(() => this._searchInput()?.valueState() ?? '');
117
- /** Access the search input if present */
118
- _searchInput = contentChild(BrnCommandSearchInput, {
119
- descendants: true,
120
- });
52
+ /** The current search query. */
53
+ search = model('');
121
54
  /** @internal Access all the items within the command */
122
55
  items = contentChildren(BrnCommandItemToken, {
123
56
  descendants: true,
124
57
  });
125
58
  /** @internal The key manager for managing active descendant */
126
59
  keyManager = new ActiveDescendantKeyManager(this.items, this._injector);
60
+ _onChange;
61
+ _onTouched;
127
62
  constructor() {
128
63
  this.keyManager
129
64
  .withVerticalOrientation()
@@ -155,14 +90,29 @@ class BrnCommand {
155
90
  selectActiveItem() {
156
91
  this.keyManager.activeItem?.selected.emit();
157
92
  }
93
+ /** CONTROL VALUE ACCESSOR */
94
+ writeValue(value) {
95
+ if (value) {
96
+ this.search.set(value);
97
+ }
98
+ }
99
+ registerOnChange(fn) {
100
+ this._onChange = fn;
101
+ }
102
+ registerOnTouched(fn) {
103
+ this._onTouched = fn;
104
+ }
105
+ setDisabledState(isDisabled) {
106
+ this._disabled.set(isDisabled);
107
+ }
158
108
  /** @nocollapse */ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.7", ngImport: i0, type: BrnCommand, deps: [], target: i0.ɵɵFactoryTarget.Directive });
159
- /** @nocollapse */ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.2.0", version: "20.0.7", type: BrnCommand, isStandalone: true, selector: "[brnCommand]", inputs: { id: { classPropertyName: "id", publicName: "id", isSignal: true, isRequired: false, transformFunction: null }, filter: { classPropertyName: "filter", publicName: "filter", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { valueChange: "valueChange" }, host: { listeners: { "keydown.enter": "selectActiveItem()" }, properties: { "id": "id()" } }, providers: [provideBrnCommand(BrnCommand)], queries: [{ propertyName: "_searchInput", first: true, predicate: BrnCommandSearchInput, descendants: true, isSignal: true }, { propertyName: "items", predicate: BrnCommandItemToken, descendants: true, isSignal: true }], ngImport: i0 });
109
+ /** @nocollapse */ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.2.0", version: "20.0.7", type: BrnCommand, isStandalone: true, selector: "[brnCommand]", inputs: { id: { classPropertyName: "id", publicName: "id", isSignal: true, isRequired: false, transformFunction: null }, filter: { classPropertyName: "filter", publicName: "filter", isSignal: true, isRequired: false, transformFunction: null }, disabled: { classPropertyName: "disabled", publicName: "disabled", isSignal: true, isRequired: false, transformFunction: null }, search: { classPropertyName: "search", publicName: "search", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { valueChange: "valueChange", search: "searchChange" }, host: { listeners: { "keydown.enter": "selectActiveItem()" }, properties: { "id": "id()" } }, providers: [provideBrnCommand(BrnCommand), BRN_COMMAND_VALUE_ACCESSOR], queries: [{ propertyName: "items", predicate: BrnCommandItemToken, descendants: true, isSignal: true }], ngImport: i0 });
160
110
  }
161
111
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.7", ngImport: i0, type: BrnCommand, decorators: [{
162
112
  type: Directive,
163
113
  args: [{
164
114
  selector: '[brnCommand]',
165
- providers: [provideBrnCommand(BrnCommand)],
115
+ providers: [provideBrnCommand(BrnCommand), BRN_COMMAND_VALUE_ACCESSOR],
166
116
  host: {
167
117
  '[id]': 'id()',
168
118
  '(keydown.enter)': 'selectActiveItem()',
@@ -221,6 +171,66 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.7", ngImpor
221
171
  }]
222
172
  }] });
223
173
 
174
+ const BrnCommandInputToken = new InjectionToken('BrnCommandInputToken');
175
+ function provideBrnCommandInput(command) {
176
+ return { provide: BrnCommandInputToken, useExisting: command };
177
+ }
178
+
179
+ class BrnCommandInput {
180
+ static _id = 0;
181
+ _el = inject(ElementRef);
182
+ _command = injectBrnCommand();
183
+ _initialId = `brn-command-input-${++BrnCommandInput._id}`;
184
+ /** The id of the command input */
185
+ id = input(this._initialId, {
186
+ transform: (value) => value || this._initialId,
187
+ });
188
+ _disabled = this._command.disabledState;
189
+ /** The id of the active option */
190
+ _activeDescendant = signal(undefined);
191
+ constructor() {
192
+ this._command.keyManager.change
193
+ .pipe(startWith(this._command.keyManager.activeItemIndex), takeUntilDestroyed())
194
+ .subscribe(() => this._activeDescendant.set(this._command.keyManager.activeItem?.id()));
195
+ effect(() => {
196
+ const search = this._command.search();
197
+ if (this._el.nativeElement.value !== search) {
198
+ this._el.nativeElement.value = search;
199
+ }
200
+ });
201
+ }
202
+ /** Listen for changes to the input value */
203
+ onInput() {
204
+ this._command.search.set(this._el.nativeElement.value);
205
+ }
206
+ /** Listen for keydown events */
207
+ onKeyDown(event) {
208
+ this._command.keyManager.onKeydown(event);
209
+ }
210
+ /** @nocollapse */ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.7", ngImport: i0, type: BrnCommandInput, deps: [], target: i0.ɵɵFactoryTarget.Directive });
211
+ /** @nocollapse */ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "20.0.7", type: BrnCommandInput, isStandalone: true, selector: "input[brnCommandInput]", inputs: { id: { classPropertyName: "id", publicName: "id", isSignal: true, isRequired: false, transformFunction: null } }, host: { attributes: { "role": "combobox", "aria-autocomplete": "list", "type": "text", "autocomplete": "off", "autocorrect": "off", "spellcheck": "false" }, listeners: { "keydown": "onKeyDown($event)", "input": "onInput()" }, properties: { "id": "id()", "attr.aria-activedescendant": "_activeDescendant()", "attr.disabled": "_disabled() ? \"\" : null" } }, providers: [provideBrnCommandInput(BrnCommandInput)], ngImport: i0 });
212
+ }
213
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.7", ngImport: i0, type: BrnCommandInput, decorators: [{
214
+ type: Directive,
215
+ args: [{
216
+ selector: 'input[brnCommandInput]',
217
+ providers: [provideBrnCommandInput(BrnCommandInput)],
218
+ host: {
219
+ '[id]': 'id()',
220
+ role: 'combobox',
221
+ 'aria-autocomplete': 'list',
222
+ '[attr.aria-activedescendant]': '_activeDescendant()',
223
+ '[attr.disabled]': '_disabled() ? "" : null',
224
+ '(keydown)': 'onKeyDown($event)',
225
+ '(input)': 'onInput()',
226
+ type: 'text',
227
+ autocomplete: 'off',
228
+ autocorrect: 'off',
229
+ spellcheck: 'false',
230
+ },
231
+ }]
232
+ }], ctorParameters: () => [] });
233
+
224
234
  class BrnCommandItem {
225
235
  static _id = 0;
226
236
  _platform = inject(PLATFORM_ID);
@@ -354,9 +364,9 @@ const BrnCommandImports = [
354
364
  BrnCommand,
355
365
  BrnCommandEmpty,
356
366
  BrnCommandGroup,
367
+ BrnCommandInput,
357
368
  BrnCommandItem,
358
369
  BrnCommandList,
359
- BrnCommandSearchInput,
360
370
  BrnCommandSeparator,
361
371
  ];
362
372
 
@@ -364,5 +374,5 @@ const BrnCommandImports = [
364
374
  * Generated bundle index. Do not edit.
365
375
  */
366
376
 
367
- export { BrnCommand, BrnCommandEmpty, BrnCommandGroup, BrnCommandImports, BrnCommandItem, BrnCommandItemToken, BrnCommandList, BrnCommandSearchInput, BrnCommandSearchInputToken, BrnCommandSeparator, BrnCommandToken, injectBrnCommand, injectBrnCommandConfig, provideBrnCommand, provideBrnCommandConfig, provideBrnCommandItem, provideBrnCommandSearchInput };
377
+ export { BRN_COMMAND_VALUE_ACCESSOR, BrnCommand, BrnCommandEmpty, BrnCommandGroup, BrnCommandImports, BrnCommandInput, BrnCommandInputToken, BrnCommandItem, BrnCommandItemToken, BrnCommandList, BrnCommandSeparator, BrnCommandToken, injectBrnCommand, injectBrnCommandConfig, provideBrnCommand, provideBrnCommandConfig, provideBrnCommandInput, provideBrnCommandItem };
368
378
  //# sourceMappingURL=spartan-ng-brain-command.mjs.map