Skip to content

Commit e87e853

Browse files
Add support for onFocus and onBlur component binding callbacks (#54)
1 parent 5bf9960 commit e87e853

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,14 @@ behavior of the omnibox:
218218
The following &-callback functions are available for binding on the `ngc-omnibox` component in
219219
response to events:
220220

221+
- `onFocus({event})`: An expression that's called when the component gets focus. This includes not
222+
just the input field, but the component in general. This is necessary since selecting the choices
223+
might blur the input field, but not the component itself. If you need to know when just the input
224+
field receives focus, you can use the `target` property of the event object to figure it out.
225+
- `onBlur({event})`: An expression that's called when the component loses focus. This also includes
226+
the entire component, not just the field. This blur event also has logic to reduce the noise that
227+
sometimes happens where it'll lose focus then immediately regain it, so the blur is called only
228+
after a timeout to make sure it doesn't re-receive focus first.
221229
- `onChosen({choice})`: An expression that's called when a suggestion is chosen. In its locals it
222230
has access to `choice`, which is the item that was chosen.
223231
- `onUnchosen({choice})`: An expression that's called when a suggestion is unchosen (removed as a

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.1",
3+
"version": "0.2.2",
44
"description": "A modern, flexible, Angular 1.x autocomplete library with limited assumptions.",
55
"main": "dist/ngc-omnibox.js",
66
"scripts": {

src/angularComponent/ngcOmniboxComponent.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ export default {
6161
isSelectable: '&',
6262
canShowSuggestions: '&',
6363
requireMatch: '<?',
64+
onFocus: '&',
65+
onBlur: '&',
6466
onChosen: '&',
6567
onUnchosen: '&',
6668
onShowSuggestions: '&',

src/angularComponent/ngcOmniboxController.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,21 @@ export default class NgcOmniboxController {
4343
$scope.$apply();
4444
});
4545

46-
if (this.hideOnBlur !== 'false') {
47-
let blurTimeout;
48-
this.element.addEventListener('focus', () => {
49-
clearTimeout(blurTimeout);
50-
}, true);
51-
this.element.addEventListener('blur', () => {
52-
blurTimeout = setTimeout(() => {
46+
let blurTimeout;
47+
this.element.addEventListener('focus', (event) => {
48+
clearTimeout(blurTimeout);
49+
this.onFocus({event});
50+
}, true);
51+
this.element.addEventListener('blur', (event) => {
52+
blurTimeout = setTimeout(() => {
53+
if (this.hideOnBlur !== 'false') {
5354
this.hideSuggestions = true;
5455
$scope.$apply();
55-
}, SUGGESTIONS_BLUR_THRESHOLD);
56-
}, true);
57-
}
56+
}
57+
58+
this.onBlur({event});
59+
}, SUGGESTIONS_BLUR_THRESHOLD);
60+
}, true);
5861

5962
// Remove the focus ring when the overall component is focused
6063
const styleSheets = this.doc.styleSheets;

0 commit comments

Comments
 (0)