Skip to content

Commit

Permalink
feat: select can assign object as a value
Browse files Browse the repository at this point in the history
Closes #270
  • Loading branch information
AntoninoBonanno committed Sep 30, 2023
1 parent b8d43f7 commit bc892c7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export abstract class ItAbstractFormComponent<T = any> extends ItAbstractCompone
/**
* Internal form control
*/
protected control: FormControl;
protected control: FormControl<T>;

constructor(
protected readonly _translateService: TranslateService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class ItInputComponent extends ItAbstractFormComponent<string | number |
* If you need to retrieve items via API, can pass a function of Observable
* @default undefined
*/
@Input() autocompleteData?: Array<AutocompleteItem> | ((search?: string) => Observable<Array<AutocompleteItem>>);
@Input() autocompleteData?: Array<AutocompleteItem> | ((search?: string | number | null) => Observable<Array<AutocompleteItem>>);

/**
* Time span [ms] has passed without another source emission, to delay data filtering.
Expand Down Expand Up @@ -189,7 +189,7 @@ export class ItInputComponent extends ItAbstractFormComponent<string | number |

/** Observable da cui vengono emessi i risultati dell'auto completamento */
autocompleteResults$: Observable<{
searchedValue: string,
searchedValue: string | number | null | undefined,
relatedEntries: Array<AutocompleteItem>
}> = new Observable();

Expand Down Expand Up @@ -250,7 +250,7 @@ export class ItInputComponent extends ItAbstractFormComponent<string | number |
/**
* Create the autocomplete list
*/
private getAutocompleteResults$(): Observable<{ searchedValue: string, relatedEntries: Array<AutocompleteItem> }> {
private getAutocompleteResults$(): Observable<{ searchedValue: string | number | null | undefined, relatedEntries: Array<AutocompleteItem> }> {
if (this.type !== 'search') {
return of({ searchedValue: '', relatedEntries: [] });
}
Expand All @@ -259,13 +259,13 @@ export class ItInputComponent extends ItAbstractFormComponent<string | number |
distinctUntilChanged(), // Only if searchValue is distinct in comparison to the last value
switchMap(searchedValue => {
if (!this.autocompleteData) {
return of({ searchedValue, relatedEntries: [] });
return of({ searchedValue, relatedEntries: <Array<AutocompleteItem>>[] });
}

const autoCompleteData$ = Array.isArray(this.autocompleteData) ? of(this.autocompleteData) : this.autocompleteData(searchedValue);
return autoCompleteData$.pipe(
map(autocompleteData => {
if (!searchedValue) {
if (!searchedValue || typeof searchedValue === 'number') {
return { searchedValue, relatedEntries: [] };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
(blur)="markAsTouched()"
[attr.aria-describedby]="id + '-description'">

<option *ngIf="defaultOption" [value]="null" disabled selected>
<option *ngIf="defaultOption" [ngValue]="null" disabled selected>
{{defaultOption}}
</option>

<ng-content></ng-content>

<ng-container *ngIf="options">
<option *ngFor="let option of options" [disabled]="optionIsDisabled(option)" [value]="option.value">
<option *ngFor="let option of options" [disabled]="optionIsDisabled(option)" [ngValue]="option.value">
{{option.text ?? option.value}}
</option>
</ng-container>

<ng-container *ngIf="groups">
<optgroup *ngFor="let group of groups" [label]="group.label">
<option *ngFor="let option of group.options" [disabled]="optionIsDisabled(option)" [value]="option.value">
<option *ngFor="let option of group.options" [disabled]="optionIsDisabled(option)" [ngValue]="option.value">
{{option.text ?? option.value}}
</option>
</optgroup>
Expand Down
12 changes: 6 additions & 6 deletions projects/design-angular-kit/src/lib/interfaces/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import { IconName } from './icon';

export type InputControlType = 'text' | 'email' | 'number' | 'date' | 'time' | 'tel' | 'color' | 'url' | 'search';

export interface SelectControlOption {
value: any,
export interface SelectControlOption<T = any> {
value: T,
text?: string,
selected?: boolean | ((value: any) => boolean),
disabled?: boolean | ((value: any) => boolean)
selected?: boolean | ((value: T) => boolean),
disabled?: boolean | ((value: T) => boolean)
}

export interface SelectControlGroup {
export interface SelectControlGroup<T = any> {
label: string,
options: Array<SelectControlOption>,
options: Array<SelectControlOption<T>>,
dragdrop?: boolean
}

Expand Down

0 comments on commit bc892c7

Please sign in to comment.