Skip to content

Commit

Permalink
110088: new implementation for keyboard support in dropdowns
Browse files Browse the repository at this point in the history
  • Loading branch information
Jens Vannerum committed Jan 4, 2024
1 parent 92a10ce commit 4bbfb4c
Showing 1 changed file with 57 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export class DsDynamicScrollableDropdownComponent extends DsDynamicVocabularyCom
public loading = false;
public pageInfo: PageInfo;
public optionsList: any;
public inputText: string = null;
public acceptableKeys = ['Space', 'NumpadMultiply', 'NumpadAdd', 'NumpadSubtract', 'NumpadDecimal', 'Semicolon', 'Equal', 'Comma', 'Minus', 'Period', 'Quote', 'Backquote'];

constructor(protected vocabularyService: VocabularyService,
protected cdr: ChangeDetectorRef,
Expand All @@ -54,32 +56,25 @@ export class DsDynamicScrollableDropdownComponent extends DsDynamicVocabularyCom
*/
ngOnInit() {
this.updatePageInfo(this.model.maxOptions, 1);
this.vocabularyService.getVocabularyEntries(this.model.vocabularyOptions, this.pageInfo).pipe(
this.loadOptions();
}

loadOptions() {
this.loading = true;
this.vocabularyService.getVocabularyEntriesByValue(this.inputText, false, this.model.vocabularyOptions, this.pageInfo).pipe(
getFirstSucceededRemoteDataPayload(),
catchError(() => observableOf(buildPaginatedList(
new PageInfo(),
[]
))
))
.subscribe((list: PaginatedList<VocabularyEntry>) => {
this.optionsList = list.page;
if (this.model.value) {
this.setCurrentValue(this.model.value, true);
}

this.updatePageInfo(
list.pageInfo.elementsPerPage,
list.pageInfo.currentPage,
list.pageInfo.totalElements,
list.pageInfo.totalPages
);
this.cdr.detectChanges();
});

this.group.get(this.model.id).valueChanges.pipe(distinctUntilChanged())
.subscribe((value) => {
this.setCurrentValue(value);
});
catchError(() => observableOf(buildPaginatedList(new PageInfo(), []))),
tap(() => this.loading = false)
).subscribe((list: PaginatedList<VocabularyEntry>) => {
this.optionsList = list.page;
this.updatePageInfo(
list.pageInfo.elementsPerPage,
list.pageInfo.currentPage,
list.pageInfo.totalElements,
list.pageInfo.totalPages
);
this.cdr.detectChanges();
});
}

/**
Expand All @@ -94,6 +89,8 @@ export class DsDynamicScrollableDropdownComponent extends DsDynamicVocabularyCom
openDropdown(sdRef: NgbDropdown) {
if (!this.model.readOnly) {
this.group.markAsUntouched();
this.inputText = null;
this.loadOptions();
sdRef.open();
}
}
Expand All @@ -112,7 +109,41 @@ export class DsDynamicScrollableDropdownComponent extends DsDynamicVocabularyCom
sdRef.toggle();
} else if (keyName === 'ArrowDown' || keyName === 'ArrowUp') {
this.openDropdown(sdRef);
} else if (keyName === 'Backspace') {
this.removeKeyFromInput();
} else if (this.isAcceptableKey(keyName)) {
this.addKeyToInput(keyName);
}
}

addKeyToInput(keyName: string) {
if (this.inputText === null) {
this.inputText = '';
}
this.inputText += keyName;
// When a new key is added, we need to reset the page info
this.updatePageInfo(this.model.maxOptions, 1);
this.loadOptions();
}

removeKeyFromInput() {
if (this.inputText !== null) {
this.inputText = this.inputText.slice(0, -1);
if (this.inputText === '') {
this.inputText = null;
}
this.loadOptions();
}
}


isAcceptableKey(keyPress: string): boolean {
// allow all letters and numbers
if (keyPress.length == 1 && keyPress.match(/^[a-zA-Z0-9]*$/)) {

Check failure on line 142 in src/app/shared/form/builder/ds-dynamic-form-ui/models/scrollable-dropdown/dynamic-scrollable-dropdown.component.ts

View workflow job for this annotation

GitHub Actions / tests (16.x)

Expected '===' and instead saw '=='

Check failure on line 142 in src/app/shared/form/builder/ds-dynamic-form-ui/models/scrollable-dropdown/dynamic-scrollable-dropdown.component.ts

View workflow job for this annotation

GitHub Actions / tests (18.x)

Expected '===' and instead saw '=='
return true;
}
// Some other characters like space, dash, etc should be allowed as well
return this.acceptableKeys.includes(keyPress);
}

/**
Expand All @@ -127,7 +158,7 @@ export class DsDynamicScrollableDropdownComponent extends DsDynamicVocabularyCom
this.pageInfo.totalElements,
this.pageInfo.totalPages
);
this.vocabularyService.getVocabularyEntries(this.model.vocabularyOptions, this.pageInfo).pipe(
this.vocabularyService.getVocabularyEntriesByValue(this.inputText, false, this.model.vocabularyOptions, this.pageInfo).pipe(
getFirstSucceededRemoteDataPayload(),
catchError(() => observableOf(buildPaginatedList(
new PageInfo(),
Expand Down

0 comments on commit 4bbfb4c

Please sign in to comment.