Skip to content
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

Typeahead: Added emptyOptionLabel Typeahead input #1928

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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
6 changes: 6 additions & 0 deletions libs/angular/src/v-angular/dropdown/dropdown.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@
&:hover {
background: #e7e7e7;
}

&:active {
background: inherit;
color: inherit;
border-color: inherit;
}
}

.gds-form-item__footer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export class NggvTypeaheadInputComponent
/** Used to determine the height of the inputin html */
buttonHeight?: number

/** @internal Used to determine wether the input has been moved or not */
inputMoved = false

constructor(
private element: ElementRef,
private renderer2: Renderer2,
Expand Down Expand Up @@ -76,7 +79,7 @@ export class NggvTypeaheadInputComponent
*/
@HostListener('document:keyup', ['$event'])
onKeyUp(event: KeyboardEvent) {
if (event.code === 'Space') {
if (event.code === 'Space' && this.expanded) {
event.preventDefault()
}
}
Expand All @@ -98,6 +101,7 @@ export class NggvTypeaheadInputComponent
// Get the height of the parent button so the input can be explicitly set to the same height since it's absolutely positioned
this.buttonHeight =
this.dropdownButton.getBoundingClientRect().height || 32 // Default to 2em;
this.inputMoved = true
}
}, 0)
}
Expand All @@ -113,6 +117,10 @@ export class NggvTypeaheadInputComponent
.subscribe((state) => {
this.expanded = state

// Calling this function from onInit caused issues when DOM has not fully been initialized because of
// different CSS used to hide (but not remove) from DOM
if (!this.inputMoved) this.moveInput()

if (this.expanded) {
// Weird workaround for setting focus. Didn't set focus, but wrapping in setTimeout solved it.
// See suggestion here: https://github.com/ionic-team/stencil/issues/3772#issuecomment-1292599609
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ export class NggvTypeaheadDirective<
/** Custom label for the unselect option */
@Input() unselectLabel?: string

/** Custom label for the empty option */
@Input() emptyOptionLabel?: string

/** Emits the entered string the user has written in the input */
@Output() filterPhraseChange = new EventEmitter<string>()

Expand All @@ -83,7 +86,11 @@ export class NggvTypeaheadDirective<
}

get emptyOption(): OptionBase<any> {
return { key: null, label: 'label.nomatchingoptions', disabled: true }
return {
key: null,
label: this.emptyOptionLabel || 'label.nomatchingoptions',
disabled: true,
}
}

/** Name of the component. nggv-dropdown if NggvDropdownComponent or nggv-input if NggvInputComponent */
Expand All @@ -108,7 +115,6 @@ export class NggvTypeaheadDirective<

ngOnInit() {
this.handleInputChanges()
this.inputValue$.next('')

if (this.hostIsDropdown) this.createInput()
else this.createDropdownList()
Expand Down Expand Up @@ -137,6 +143,9 @@ export class NggvTypeaheadDirective<
.subscribe(([filteredValues, input]) =>
this.setOptions(filteredValues, input),
)

// Trigger the pipe when this function have been called
this.inputValue$.next('')
}

/**
Expand Down Expand Up @@ -182,8 +191,8 @@ export class NggvTypeaheadDirective<
this.allowUnselect &&
!input &&
!(
Object.keys(filteredValues[0]).includes('key') &&
filteredValues[0].key == null
Object.keys(filteredValues[0] ?? {}).includes('key') &&
filteredValues[0]?.key == null
)
if (filteredValues.length === 0) {
filteredValues = [this.emptyOption]
Expand Down
Loading