Skip to content

Commit

Permalink
fix(core): get proper closest parent element
Browse files Browse the repository at this point in the history
  • Loading branch information
artolshansky committed Jun 4, 2021
1 parent b66a43f commit 7f1a8ac
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AfterViewInit, ChangeDetectionStrategy, Component, ElementRef } from '@angular/core';

import { QuickViewGroupItemContentElementDirective } from './quick-view-group-item-content-element.directive';
import { getClosest } from '@fundamental-ngx/core/utils';

@Component({
selector: 'fd-quick-view-group-item-content',
Expand All @@ -19,11 +20,11 @@ export class QuickViewGroupItemContentComponent implements AfterViewInit {
/** @hidden
* Needed for binding the id of the element and id of the proper label to aria-labelledby. */
private _bindElementAttributes(): void {
const parentId = this._elRef.nativeElement?.parentElement?.id;
const parentId = getClosest('.fd-form-item', this._elRef.nativeElement)?.id;
const id = `${parentId}-content`;

const element = this._elRef.nativeElement.querySelector(`.${QuickViewGroupItemContentElementDirective.class}`);
if (element) {
if (element && parentId) {
element.setAttribute('id', id);
element.setAttribute('aria-labelledby', `${parentId}-label ${id}`);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AfterViewInit, ChangeDetectionStrategy, Component, ElementRef } from '@angular/core';
import { getClosest } from '@fundamental-ngx/core/utils';

@Component({
selector: 'fd-quick-view-group-item-label',
Expand All @@ -17,9 +18,9 @@ export class QuickViewGroupItemLabelComponent implements AfterViewInit {
/** @hidden
* Needed for binding the id to the label element (and this id needed for aria-labelledby of proper element). */
private _bindElementAttributes(): void {
const parentId = this._elRef.nativeElement?.parentElement?.id;
const parentId = getClosest('.fd-form-item', this._elRef.nativeElement)?.id;

if (this._elRef.nativeElement.firstChild) {
if (this._elRef.nativeElement.firstChild && parentId) {
this._elRef.nativeElement.firstChild.setAttribute('id', `${parentId}-label`);
}
}
Expand Down
17 changes: 17 additions & 0 deletions libs/core/src/lib/utils/functions/closest-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,20 @@ export function closestElement(selector, element): Element | null {

return element;
}

/** Alternative way to get closes element. */
export function getClosest(selector, elem): Element | null {
// .matches polyfill
if (!Element.prototype.matches) {
/** @ts-ignore */
Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;
}

// get the closest matching element
for ( ; elem && elem !== document; elem = elem.parentNode) {
if (elem.matches(selector)) {
return elem;
}
}
return null;
}

0 comments on commit 7f1a8ac

Please sign in to comment.