Skip to content

Commit

Permalink
Merge branch 'release/2.0' into ui-improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
e11sy committed Oct 30, 2024
2 parents 522673c + 1dbf3d4 commit 0efccba
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
30 changes: 10 additions & 20 deletions src/ListTabulator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,15 +613,10 @@ export default class ListTabulator<Renderer extends ListRenderer> {
focusItem(item, false);

/**
* If previous parent's children list is now empty, remove it.
* If parent item has empty child wrapper after unshifting of the current item, then we need to remove child wrapper
* This case could be reached if the only child item of the parent was unshifted
*/
const parentItemChildWrapper = getItemChildWrapper(parentItem);

if (!parentItemChildWrapper) {
return;
}

removeChildWrapperIfEmpty(parentItemChildWrapper);
removeChildWrapperIfEmpty(parentItem);
}

/**
Expand Down Expand Up @@ -858,14 +853,11 @@ export default class ListTabulator<Renderer extends ListRenderer> {
*/
item.remove();

const targetItemChildWrapper = getItemChildWrapper(targetItem);

/**
* Remove target item child wrapper if it is empty
* If target item has empty child wrapper after merge, we need to remove child wrapper
* This case could be reached if the only child item of the target was merged with target
*/
if (targetItemChildWrapper !== null) {
removeChildWrapperIfEmpty(targetItemChildWrapper);
}
removeChildWrapperIfEmpty(targetItem);

return;
}
Expand Down Expand Up @@ -1002,14 +994,12 @@ export default class ListTabulator<Renderer extends ListRenderer> {
prevItem.appendChild(prevItemChildrenListWrapper);
}

const currentItemChildWrapper = getItemChildWrapper(currentItem);

/**
* Remove child wrapper after moving all children
* Remove child wrapper of the current item if it is empty after adding the tab
* This case would be reached, because after adding tab current item will have same nesting level with children
* So its child wrapper would be empty
*/
if (currentItemChildWrapper !== null) {
removeChildWrapperIfEmpty(currentItemChildWrapper);
}
removeChildWrapperIfEmpty(currentItem);

focusItem(currentItem, false);
}
Expand Down
26 changes: 21 additions & 5 deletions src/utils/removeChildWrapperIfEmpty.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
import type { ItemChildWrapperElement } from '../types/Elements';
import { DefaultListCssClasses } from '../ListRenderer';
import type { ItemChildWrapperElement, ItemElement } from '../types/Elements';
import { getChildItems } from './getChildItems';
import { getItemChildWrapper } from './getItemChildWrapper';

/**
* Method that will remove passed child wrapper if it has no child items
* @param childWrapper - childWrapper to be removed if it is empty
* @param element - child wrapper or actual item, from where we get child wrapper
*/
export function removeChildWrapperIfEmpty(childWrapper: ItemChildWrapperElement): void {
// eslint-disable-next-line @typescript-eslint/no-duplicate-type-constituents
export function removeChildWrapperIfEmpty(element: ItemChildWrapperElement | ItemElement): void {
let itemChildWrapper: HTMLElement | null = element;

/**
* If passed element is list item than get item's child wrapper
*/
if (element.classList.contains(DefaultListCssClasses.item)) {
itemChildWrapper = getItemChildWrapper(element);
}

if (itemChildWrapper === null) {
return;
}

/**
* Check that there is at least one item
*/
if (getChildItems(childWrapper).length === 0) {
childWrapper.remove();
if (getChildItems(itemChildWrapper).length === 0) {
itemChildWrapper.remove();
}
}

0 comments on commit 0efccba

Please sign in to comment.