Skip to content

feat: Combo box forms #1327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
border: 2px solid $blue-4;
}
}

.add-icon {
cursor: pointer;
}
}

.container {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { IconType } from '@hypertrace/assets-library';
import { Color } from '@hypertrace/common';
import { IconSize } from '../icon/icon-size';
Expand Down Expand Up @@ -37,7 +37,7 @@ import { IconSize } from '../icon/icon-size';
</div>
`
})
export class ColorPickerComponent {
export class ColorPickerComponent implements ControlValueAccessor {
@Input()
public selected?: string;

Expand Down
40 changes: 34 additions & 6 deletions projects/components/src/combo-box/combo-box.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import {
ViewChild,
ViewChildren
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { IconType } from '@hypertrace/assets-library';
import { DomElementScrollIntoViewService, isNonEmptyString, TypedSimpleChanges } from '@hypertrace/common';
import { isNil } from 'lodash-es';
import { isEmpty, isNil } from 'lodash-es';
import { IconSize } from '../icon/icon-size';
import { PopoverRef } from '../popover/popover-ref';
import { PopoverComponent } from '../popover/popover.component';
Expand All @@ -25,6 +26,13 @@ import { ComboBoxMode, ComboBoxOption, ComboBoxResult } from './combo-box-api';
selector: 'ht-combo-box',
styleUrls: ['./combo-box.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [
{
provide: NG_VALUE_ACCESSOR,
multi: true,
useExisting: ComboBoxComponent
}
],
template: `
<ht-popover (popoverOpen)="this.onPopoverOpen($event)" (popoverClose)="this.onPopoverClose()" class="combo-box">
<ht-popover-trigger>
Expand Down Expand Up @@ -113,7 +121,7 @@ import { ComboBoxMode, ComboBoxOption, ComboBoxResult } from './combo-box-api';
</ht-popover>
`
})
export class ComboBoxComponent<TValue = string> implements AfterViewInit, OnChanges, OnDestroy {
export class ComboBoxComponent<TValue = string> implements AfterViewInit, OnChanges, OnDestroy, ControlValueAccessor {
@Input()
public mode?: ComboBoxMode = ComboBoxMode.Input;

Expand Down Expand Up @@ -176,6 +184,9 @@ export class ComboBoxComponent<TValue = string> implements AfterViewInit, OnChan

public createOption?: ComboBoxOption<TValue>;

private propagateControlValueChange?: (value: string | undefined) => void;
private propagateControlValueChangeOnTouch?: (value: string | undefined) => void;

public constructor(
private readonly changeDetectorRef: ChangeDetectorRef,
private readonly scrollIntoViewService: DomElementScrollIntoViewService
Expand Down Expand Up @@ -204,10 +215,9 @@ export class ComboBoxComponent<TValue = string> implements AfterViewInit, OnChan
}

private setFilteredOptions(searchText?: string): void {
this.filteredOptions =
searchText === undefined
? this.options ?? []
: (this.options ?? []).filter(option => option.text.toLowerCase().includes(searchText.toLowerCase()));
this.filteredOptions = isEmpty(searchText)
? this.options ?? []
: (this.options ?? []).filter(option => option.text.toLowerCase().includes(searchText!.toLowerCase()));
this.createOption = this.isShowCreateOption(searchText) ? this.buildCreateOption(searchText) : undefined;
this.setHighlightedOptionIndex();
}
Expand All @@ -227,6 +237,7 @@ export class ComboBoxComponent<TValue = string> implements AfterViewInit, OnChan
this.measureText();
this.setHighlightedOptionIndex();
this.textChange.emit(this.text);
this.propagateValueChangeToFormControl(this.text);
}
}

Expand Down Expand Up @@ -405,4 +416,21 @@ export class ComboBoxComponent<TValue = string> implements AfterViewInit, OnChan
this.changeDetectorRef.markForCheck(); // Yes, required
});
}

private propagateValueChangeToFormControl(value?: string): void {
this.propagateControlValueChange?.(value);
this.propagateControlValueChangeOnTouch?.(value);
}

public writeValue(text?: string): void {
this.text = text;
}

public registerOnChange(onChange: (value?: string) => void): void {
this.propagateControlValueChange = onChange;
}

public registerOnTouched(onTouch: (value?: string) => void): void {
this.propagateControlValueChangeOnTouch = onTouch;
}
}