Sha256: fe22ec3e0bf1cf4b83d395f26b3f9069b2b2fa84e556b32c915235d8f3347faf

Contents?: true

Size: 1.97 KB

Versions: 35

Compression:

Stored size: 1.97 KB

Contents

export const findItemById = (
  items: { [key: string]: any }[],
  id: string
): any => {
  for (const item of items) {
    if (item.id === id) {
      return item;
    }
    if (item.children) {
      const found = findItemById(item.children, id);
      if (found) {
        return found;
      }
    }
  }
  return null;
};

export const checkIt = (
  foundItem: { [key: string]: any },
  selectedItems: any[],
  setSelectedItems: Function,
  expand: boolean
) => {
  if (!foundItem) {
    return;
  }

  foundItem.checked = true;
  foundItem.expanded = expand;
  selectedItems.push(foundItem);

  if (foundItem.children) {
    foundItem.children.map((x: any) => {
      checkIt(x, selectedItems, setSelectedItems, expand);
    });
  }

  setSelectedItems([...selectedItems]);
};

export const unCheckIt = (
  foundItem: { [key: string]: any },
  selectedItems: any,
  setSelectedItems: any,
  expand: boolean
) => {
  if (!foundItem) {
    return;
  }

  foundItem.checked = false;
  foundItem.expanded = false;
  const newSelectedItems = selectedItems.filter(
    (item: any) => item.id !== foundItem.id
  );
  if (foundItem.children) {
    foundItem.children.map((x: any) => {
      unCheckIt(x, selectedItems, setSelectedItems, expand);
    });
  }
  setSelectedItems([...newSelectedItems]);
};


export const getParentAndAncestorsIds = (itemId:string, items:{ [key: string]: string; }[], ancestors:string[] = []):any => {
  for (let i = 0; i < items.length; i++) {
    const item:any = items[i];
    if (item.id === itemId) {
      // item found in current level of items array
      return [...ancestors, item.id];
    }
    if (item.children && item.children.length > 0) {
      // recursively search through children
      const foundAncestors = getParentAndAncestorsIds(
        itemId,
        item.children,
        [...ancestors, item.id]
      );
      if (foundAncestors) {
        return foundAncestors;
      }
    }
  }
  // item not found in this level of items array or its children
  return null;
}

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
playbook_ui-12.25.0.pre.alpha.play822bolddefaultfortitle3764 app/pb_kits/playbook/pb_multi_level_select/helper_functions.ts
playbook_ui-12.25.0.pre.alpha.PLAY733DetailKit761 app/pb_kits/playbook/pb_multi_level_select/helper_functions.ts
playbook_ui-12.25.0.pre.alpha.railsmultilevelimprovements758 app/pb_kits/playbook/pb_multi_level_select/helper_functions.ts
playbook_ui-12.25.0 app/pb_kits/playbook/pb_multi_level_select/helper_functions.ts
playbook_ui-12.24.0.pre.alpha.PLAY603datepickerquickpickinputpresetdropdown756 app/pb_kits/playbook/pb_multi_level_select/helper_functions.ts
playbook_ui-12.24.0.pre.alpha.play824753 app/pb_kits/playbook/pb_multi_level_select/helper_functions.ts
playbook_ui-12.24.0.pre.alpha.PLAY603datepickerquickpickinputpresetdropdown752 app/pb_kits/playbook/pb_multi_level_select/helper_functions.ts
playbook_ui-12.24.0.pre.alpha.play824751 app/pb_kits/playbook/pb_multi_level_select/helper_functions.ts
playbook_ui-12.24.0.pre.alpha.railsmultilevelimprovements749 app/pb_kits/playbook/pb_multi_level_select/helper_functions.ts
playbook_ui-12.24.0.pre.alpha.play824745 app/pb_kits/playbook/pb_multi_level_select/helper_functions.ts
playbook_ui-12.24.0.pre.alpha.railsmultilevelimprovements742 app/pb_kits/playbook/pb_multi_level_select/helper_functions.ts
playbook_ui-12.24.0.pre.alpha.railsmultilevelimprovements741 app/pb_kits/playbook/pb_multi_level_select/helper_functions.ts
playbook_ui-12.24.0.pre.alpha.railsmultilevelimprovements739 app/pb_kits/playbook/pb_multi_level_select/helper_functions.ts
playbook_ui-12.24.0.pre.alpha.play824733 app/pb_kits/playbook/pb_multi_level_select/helper_functions.ts
playbook_ui-12.24.0.pre.alpha.play833boldoptionfortitle2and3721 app/pb_kits/playbook/pb_multi_level_select/helper_functions.ts
playbook_ui-12.24.0.pre.alpha.alphatestforselfhostfak720 app/pb_kits/playbook/pb_multi_level_select/helper_functions.ts
playbook_ui-12.24.0.pre.alpha.play833boldoptionfortitle2and3719 app/pb_kits/playbook/pb_multi_level_select/helper_functions.ts
playbook_ui-12.24.0.pre.alpha.play833boldoptionfortitle2and3717 app/pb_kits/playbook/pb_multi_level_select/helper_functions.ts
playbook_ui-12.24.0 app/pb_kits/playbook/pb_multi_level_select/helper_functions.ts
playbook_ui-12.23.0.pre.alpha.movemarkdown712 app/pb_kits/playbook/pb_multi_level_select/helper_functions.ts