Skip to content

Commit

Permalink
fix(datepicker): focus on multiple instances (#3494)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpolychronis-amadeus committed Dec 4, 2019
1 parent 4780dc1 commit 437a526
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/datepicker/datepicker.ts
Expand Up @@ -31,7 +31,7 @@ import {NgbDateAdapter} from './adapters/ngb-date-adapter';
import {NgbDateStruct} from './ngb-date-struct';
import {NgbDatepickerI18n} from './datepicker-i18n';
import {isChangedDate, isChangedMonth} from './datepicker-tools';
import {hasClassName} from '../util/util';
import {grandparent, hasClassName} from '../util/util';

const NGB_DATEPICKER_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
Expand Down Expand Up @@ -387,7 +387,7 @@ export class NgbDatepicker implements OnDestroy,
merge(focusIns$, focusOuts$)
.pipe(
filter(
({target, relatedTarget}) =>
({target, relatedTarget}) => (grandparent(target) !== grandparent(relatedTarget)) ||
!(hasClassName(target, 'ngb-dp-day') && hasClassName(relatedTarget, 'ngb-dp-day'))),
takeUntil(this._destroyed$))
.subscribe(({type}) => this._ngZone.run(() => this._service.focusVisible = type === 'focusin'));
Expand Down
19 changes: 18 additions & 1 deletion src/util/util.spec.ts
@@ -1,4 +1,4 @@
import {toInteger, toString, getValueInRange, isInteger, isString, hasClassName} from './util';
import {toInteger, toString, getValueInRange, isInteger, isString, hasClassName, grandparent} from './util';

describe('util', () => {

Expand Down Expand Up @@ -109,4 +109,21 @@ describe('util', () => {
});
});

describe('grandparent', () => {

it('should return undefined on undefined', () => {
const element = undefined;
expect(grandparent(element)).toBeFalsy();
});

it('should return undefined on undefined parent', () => {
const element = {parentNode: undefined};
expect(grandparent(element)).toBeFalsy();
});

it('should return parent of parent', () => {
const element = {parentNode: {parentNode: {}}};
expect(grandparent(element)).toBe(element.parentNode.parentNode);
});
});
});
4 changes: 4 additions & 0 deletions src/util/util.ts
Expand Up @@ -73,3 +73,7 @@ export function closest(element: HTMLElement, selector): HTMLElement {

return element.closest(selector);
}

export function grandparent(element: any) {
return element && element.parentNode && element.parentNode.parentNode;
}

0 comments on commit 437a526

Please sign in to comment.