Skip to content

Commit 566a20f

Browse files
committed
feat(select): add MtxSelectIntl (#356)
1 parent d23036e commit 566a20f

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed
+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './select-module';
22
export * from './select';
3+
export * from './select-intl';
34
export * from './option';
45
export * from './templates';
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Inject, Injectable, Optional } from '@angular/core';
2+
import { Subject } from 'rxjs';
3+
import { MTX_SELECT_DEFAULT_OPTIONS, MtxSelectDefaultOptions } from './select';
4+
5+
@Injectable({ providedIn: 'root' })
6+
export class MtxSelectIntl {
7+
constructor(
8+
@Optional()
9+
@Inject(MTX_SELECT_DEFAULT_OPTIONS)
10+
private _defaultOptions?: MtxSelectDefaultOptions
11+
) {}
12+
13+
/**
14+
* Stream to emit from when labels are changed. Use this to notify components when the labels have
15+
* changed after initialization.
16+
*/
17+
readonly changes = new Subject<void>();
18+
19+
placeholder = this._defaultOptions?.placeholder;
20+
notFoundText = this._defaultOptions?.notFoundText ?? 'No items found';
21+
typeToSearchText = this._defaultOptions?.typeToSearchText ?? 'Type to search';
22+
addTagText = this._defaultOptions?.addTagText ?? 'Add item';
23+
loadingText = this._defaultOptions?.loadingText ?? 'Loading...';
24+
clearAllText = this._defaultOptions?.clearAllText ?? 'Clear all';
25+
}

projects/extensions/select/select.html

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
[class.ng-select-invalid]="errorState"
33
[(ngModel)]="value"
44
[ngModelOptions]="{standalone: true}"
5-
[placeholder]="placeholder"
5+
[placeholder]="placeholder || _intl.placeholder!"
66
[items]="items"
77
[addTag]="addTag"
8-
[addTagText]="addTagText"
8+
[addTagText]="addTagText || _intl.addTagText"
99
[appendTo]="appendTo"
1010
[appearance]="appearance"
1111
[bindLabel]="bindLabel!"
1212
[bindValue]="bindValue!"
1313
[closeOnSelect]="closeOnSelect"
14-
[clearAllText]="clearAllText"
14+
[clearAllText]="clearAllText || _intl.clearAllText"
1515
[clearable]="clearable"
1616
[clearOnBackspace]="clearOnBackspace"
1717
[dropdownPosition]="dropdownPosition"
@@ -22,15 +22,15 @@
2222
[isOpen]="isOpen"
2323
[inputAttrs]="inputAttrs"
2424
[loading]="loading"
25-
[loadingText]="loadingText"
25+
[loadingText]="loadingText || _intl.loadingText"
2626
[labelForId]="labelForId"
2727
[markFirst]="markFirst"
2828
[maxSelectedItems]="maxSelectedItems"
2929
[multiple]="multiple"
30-
[notFoundText]="notFoundText"
30+
[notFoundText]="notFoundText || _intl.notFoundText"
3131
[readonly]="readonly || disabled"
3232
[typeahead]="typeahead"
33-
[typeToSearchText]="typeToSearchText"
33+
[typeToSearchText]="typeToSearchText || _intl.typeToSearchText"
3434
[trackByFn]="trackByFn"
3535
[searchable]="searchable"
3636
[searchFn]="searchFn"

projects/extensions/select/select.ts

+16-7
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ import {
3636
import { ErrorStateMatcher, _ErrorStateTracker } from '@angular/material/core';
3737
import { MAT_FORM_FIELD, MatFormField, MatFormFieldControl } from '@angular/material/form-field';
3838
import { NgSelectComponent, NgSelectModule } from '@ng-select/ng-select';
39-
import { Subject, merge } from 'rxjs';
39+
import { Subject, Subscription, merge } from 'rxjs';
4040
import { startWith, takeUntil } from 'rxjs/operators';
4141
import { MtxOption } from './option';
42+
import { MtxSelectIntl } from './select-intl';
4243
import {
4344
MtxSelectFooterTemplate,
4445
MtxSelectHeaderTemplate,
@@ -159,13 +160,13 @@ export class MtxSelect
159160
mtxOptions!: QueryList<MtxOption>;
160161

161162
@Input() addTag: boolean | AddTagFn = false;
162-
@Input() addTagText = this._defaultOptions?.addTagText ?? 'Add item';
163+
@Input() addTagText?: string;
163164
@Input() appearance = 'underline';
164165
@Input() appendTo = this._defaultOptions?.appendTo ?? 'body';
165166
@Input() bindLabel = this._defaultOptions?.bindLabel;
166167
@Input() bindValue = this._defaultOptions?.bindValue;
167168
@Input({ transform: booleanAttribute }) closeOnSelect = true;
168-
@Input() clearAllText = this._defaultOptions?.clearAllText ?? 'Clear all';
169+
@Input() clearAllText?: string;
169170
@Input({ transform: booleanAttribute }) clearable = true;
170171
@Input({ transform: booleanAttribute }) clearOnBackspace = true;
171172
@Input() compareWith!: CompareWithFn;
@@ -177,12 +178,12 @@ export class MtxSelect
177178
@Input({ transform: booleanAttribute }) selectableGroupAsModel = true;
178179
@Input({ transform: booleanAttribute }) hideSelected = false;
179180
@Input({ transform: booleanAttribute }) loading = false;
180-
@Input() loadingText = this._defaultOptions?.loadingText ?? 'Loading...';
181+
@Input() loadingText?: string;
181182
@Input() labelForId: string | null = null;
182183
@Input({ transform: booleanAttribute }) markFirst = true;
183184
@Input() maxSelectedItems!: number;
184185
@Input({ transform: booleanAttribute }) multiple = false;
185-
@Input() notFoundText = this._defaultOptions?.notFoundText ?? 'No items found';
186+
@Input() notFoundText?: string;
186187
@Input({ transform: booleanAttribute }) searchable = true;
187188
@Input({ transform: booleanAttribute }) readonly = false;
188189
@Input() searchFn: SearchFn | null = null;
@@ -196,7 +197,7 @@ export class MtxSelect
196197
@Input({ transform: booleanAttribute }) editableSearchTerm = false;
197198
@Input() keyDownFn = (_: KeyboardEvent) => true;
198199
@Input({ transform: booleanAttribute }) virtualScroll = false;
199-
@Input() typeToSearchText = this._defaultOptions?.typeToSearchText ?? 'Type to search';
200+
@Input() typeToSearchText?: string;
200201
@Input() typeahead!: Subject<string>;
201202
@Input() isOpen?: boolean;
202203

@@ -275,7 +276,7 @@ export class MtxSelect
275276
this._placeholder = value;
276277
this.stateChanges.next();
277278
}
278-
private _placeholder = this._defaultOptions?.placeholder;
279+
private _placeholder!: string;
279280

280281
/** Whether the select is focused. */
281282
get focused(): boolean {
@@ -363,7 +364,10 @@ export class MtxSelect
363364
this._errorStateTracker.errorState = value;
364365
}
365366

367+
private _intlChangesSubscription = Subscription.EMPTY;
368+
366369
constructor(
370+
protected _intl: MtxSelectIntl,
367371
protected _changeDetectorRef: ChangeDetectorRef,
368372
protected _elementRef: ElementRef,
369373
protected _focusMonitor: FocusMonitor,
@@ -376,6 +380,10 @@ export class MtxSelect
376380
@Inject(MTX_SELECT_DEFAULT_OPTIONS)
377381
protected _defaultOptions?: MtxSelectDefaultOptions
378382
) {
383+
this._intlChangesSubscription = this._intl.changes.subscribe(() => {
384+
this._changeDetectorRef.detectChanges();
385+
});
386+
379387
_focusMonitor.monitor(this._elementRef, true).subscribe(origin => {
380388
if (this._focused && !origin) {
381389
this._onTouched();
@@ -443,6 +451,7 @@ export class MtxSelect
443451
this._destroy$.complete();
444452
this.stateChanges.complete();
445453
this._focusMonitor.stopMonitoring(this._elementRef);
454+
this._intlChangesSubscription.unsubscribe();
446455
}
447456

448457
/** Gets the value for the `aria-labelledby` attribute of the inputs. */

0 commit comments

Comments
 (0)