Skip to content

Commit

Permalink
feat: add boolean and number transformers
Browse files Browse the repository at this point in the history
adding to ng-dropdown-panel, ng-option and ng-select
  • Loading branch information
Dafnik committed May 24, 2024
1 parent 185a364 commit 0b4ac58
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
3 changes: 2 additions & 1 deletion src/ng-select/lib/ng-dropdown-panel.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DOCUMENT } from '@angular/common';
import {
booleanAttribute,
ChangeDetectionStrategy,
Component,
ElementRef,
Expand Down Expand Up @@ -55,7 +56,7 @@ export class NgDropdownPanelComponent implements OnInit, OnChanges, OnDestroy {
@Input() position: DropdownPosition = 'auto';
@Input() appendTo: string;
@Input() bufferAmount;
@Input() virtualScroll = false;
@Input({transform: booleanAttribute}) virtualScroll = false;
@Input() headerTemplate: TemplateRef<any>;
@Input() footerTemplate: TemplateRef<any>;
@Input() filterValue: string = null;
Expand Down
14 changes: 4 additions & 10 deletions src/ng-select/lib/ng-option.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
AfterViewChecked,
booleanAttribute,
ChangeDetectionStrategy,
Component,
ElementRef,
Expand All @@ -18,13 +19,10 @@ import { Subject } from 'rxjs';
export class NgOptionComponent implements OnChanges, AfterViewChecked, OnDestroy {

@Input() value: any;
@Input()
get disabled() { return this._disabled; }
set disabled(value: any) { this._disabled = this._isDisabled(value) }
@Input({transform: booleanAttribute}) disabled: boolean = false;

readonly stateChange$ = new Subject<{ value: any, disabled: boolean, label?: string }>();

private _disabled = false;
private _previousLabel: string;

constructor(public elementRef: ElementRef<HTMLElement>) { }
Expand All @@ -37,7 +35,7 @@ export class NgOptionComponent implements OnChanges, AfterViewChecked, OnDestroy
if (changes.disabled) {
this.stateChange$.next({
value: this.value,
disabled: this._disabled
disabled: this.disabled
});
}
}
Expand All @@ -47,7 +45,7 @@ export class NgOptionComponent implements OnChanges, AfterViewChecked, OnDestroy
this._previousLabel = this.label;
this.stateChange$.next({
value: this.value,
disabled: this._disabled,
disabled: this.disabled,
label: this.elementRef.nativeElement.innerHTML
});
}
Expand All @@ -56,8 +54,4 @@ export class NgOptionComponent implements OnChanges, AfterViewChecked, OnDestroy
ngOnDestroy() {
this.stateChange$.complete();
}

private _isDisabled(value) {
return value != null && `${value}` !== 'false';
}
}
46 changes: 24 additions & 22 deletions src/ng-select/lib/ng-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import {
ContentChildren,
QueryList,
InjectionToken,
Attribute
Attribute,
booleanAttribute,
numberAttribute
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { takeUntil, startWith, tap, debounceTime, map, filter } from 'rxjs/operators';
Expand Down Expand Up @@ -74,7 +76,7 @@ export class NgSelectComponent implements OnDestroy, OnChanges, OnInit, AfterVie

@Input() bindLabel: string;
@Input() bindValue: string;
@Input() markFirst = true;
@Input({transform: booleanAttribute}) markFirst = true;
@Input() placeholder: string;
@Input() notFoundText: string;
@Input() typeToSearchText: string;
Expand All @@ -84,36 +86,36 @@ export class NgSelectComponent implements OnDestroy, OnChanges, OnInit, AfterVie
@Input() appearance: string;
@Input() dropdownPosition: DropdownPosition = 'auto';
@Input() appendTo: string;
@Input() loading = false;
@Input() closeOnSelect = true;
@Input() hideSelected = false;
@Input() selectOnTab = false;
@Input() openOnEnter: boolean;
@Input() maxSelectedItems: number;
@Input({transform: booleanAttribute}) loading = false;
@Input({transform: booleanAttribute}) closeOnSelect = true;
@Input({transform: booleanAttribute}) hideSelected = false;
@Input({transform: booleanAttribute}) selectOnTab = false;
@Input({transform: booleanAttribute}) openOnEnter: boolean;
@Input({transform: numberAttribute}) maxSelectedItems: number;
@Input() groupBy: string | ((value: any) => any);
@Input() groupValue: GroupValueFn;
@Input() bufferAmount = 4;
@Input() virtualScroll: boolean;
@Input() selectableGroup = false;
@Input() selectableGroupAsModel = true;
@Input({transform: numberAttribute}) bufferAmount = 4;
@Input({transform: booleanAttribute}) virtualScroll: boolean;
@Input({transform: booleanAttribute}) selectableGroup = false;
@Input({transform: booleanAttribute}) selectableGroupAsModel = true;
@Input() searchFn = null;
@Input() trackByFn = null;
@Input() clearOnBackspace = true;
@Input({transform: booleanAttribute}) clearOnBackspace = true;
@Input() labelForId = null;
@Input() inputAttrs: { [key: string]: string } = {};
@Input() tabIndex: number;
@Input() readonly = false;
@Input() searchWhileComposing = true;
@Input() minTermLength = 0;
@Input() editableSearchTerm = false;
@Input({transform: numberAttribute}) tabIndex: number;
@Input({transform: booleanAttribute}) readonly = false;
@Input({transform: booleanAttribute}) searchWhileComposing = true;
@Input({transform: numberAttribute}) minTermLength = 0;
@Input({transform: booleanAttribute}) editableSearchTerm = false;
@Input() keyDownFn = (_: KeyboardEvent) => true;

@Input() @HostBinding('class.ng-select-typeahead') typeahead: Subject<string>;
@Input() @HostBinding('class.ng-select-multiple') multiple = false;
@Input({transform: booleanAttribute}) @HostBinding('class.ng-select-multiple') multiple = false;
@Input() @HostBinding('class.ng-select-taggable') addTag: boolean | AddTagFn = false;
@Input() @HostBinding('class.ng-select-searchable') searchable = true;
@Input() @HostBinding('class.ng-select-clearable') clearable = true;
@Input() @HostBinding('class.ng-select-opened') isOpen?: boolean = false;
@Input({transform: booleanAttribute}) @HostBinding('class.ng-select-searchable') searchable = true;
@Input({transform: booleanAttribute}) @HostBinding('class.ng-select-clearable') clearable = true;
@Input({transform: booleanAttribute}) @HostBinding('class.ng-select-opened') isOpen? = false;

@Input()
get items() { return this._items };
Expand Down

0 comments on commit 0b4ac58

Please sign in to comment.