Skip to content

Commit 4a01146

Browse files
Add ability to highlight a specific choice from the list of choices (#56)
1 parent 37a032c commit 4a01146

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ startIndex override what the current suggestion should be.
291291

292292
#### Choices (only work when `multiple` is set to `true`)
293293

294+
- `omnibox.highlightChoice(choice)`: Highlights a choice from the list of choices.
294295
- `omnibox.highlightPreviousChoice()`: Highlights the previous suggestion before the currently
295296
higlighted one. If the first one is highlighted then the field is focused.
296297
- `omnibox.highlightNextChoice()`: Highlights the next suggestion after the currently higlighted

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngc-omnibox",
3-
"version": "0.2.2",
3+
"version": "0.3.0",
44
"description": "A modern, flexible, Angular 1.x autocomplete library with limited assumptions.",
55
"main": "dist/ngc-omnibox.js",
66
"scripts": {

spec/tests/angularComponent/ngcOmniboxControllerSpec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,24 @@ describe('ngcOmnibox.angularComponent.ngcOmniboxController', () => {
446446
expect(omniboxController.highlightedChoice).toBe(null);
447447
});
448448

449+
it('should highlight a specific choice', () => {
450+
expect(omniboxController.highlightedChoice).toBe(null);
451+
452+
omniboxController.highlightChoice('two');
453+
expect(omniboxController.highlightedChoice).toBe('two');
454+
});
455+
456+
it('should only highlight a choice that exists in the list of choices', () => {
457+
omniboxController.highlightChoice('two');
458+
expect(omniboxController.highlightedChoice).toBe('two');
459+
460+
omniboxController.highlightChoice('bogus');
461+
expect(omniboxController.highlightedChoice).toBe('two');
462+
463+
omniboxController.highlightChoice('three');
464+
expect(omniboxController.highlightedChoice).toBe('three');
465+
});
466+
449467
it('should highlight the next choice', () => {
450468
omniboxController.highlightNextChoice();
451469
expect(omniboxController.highlightedChoice).toBe('one');

src/angularComponent/ngcOmniboxController.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,20 @@ export default class NgcOmniboxController {
299299
}
300300

301301
/**
302-
* Highlights the next suggestion after the currently higlighted one. If the last one is
302+
* Highlights a choice from the list of choices.
303+
*
304+
* @param {Any} choice
305+
*/
306+
highlightChoice(choice) {
307+
if (!this.multiple || !Array.isArray(this.ngModel) || this.ngModel.indexOf(choice) === -1) {
308+
return;
309+
}
310+
311+
this.highlightedChoice = choice;
312+
}
313+
314+
/**
315+
* Highlights the next choice after the currently higlighted one. If the last one is
303316
* highlighted then the field is focused.
304317
*/
305318
highlightNextChoice() {
@@ -575,10 +588,18 @@ export default class NgcOmniboxController {
575588

576589
if ((keyCode === KEY.LEFT || (keyCode === KEY.BACKSPACE && !this.query)) &&
577590
this.selectionStartKeyUp === 0) {
578-
this.highlightLastChoice();
591+
if (this.highlightedChoice) {
592+
this.highlightPreviousChoice();
593+
} else {
594+
this.highlightLastChoice();
595+
}
579596
} else if ((keyCode === KEY.RIGHT || keyCode === KEY.DELETE && !this.query) &&
580597
this.selectionStartKeyUp === inputLength) {
581-
this.highlightFirstChoice();
598+
if (this.highlightedChoice) {
599+
this.highlightNextChoice();
600+
} else {
601+
this.highlightFirstChoice();
602+
}
582603
}
583604
}
584605
} else if (this.highlightedChoice) {

0 commit comments

Comments
 (0)