Options
All
  • Public
  • Public/Protected
  • All
Menu

Module @react-md/tree

Index

Type aliases

ConfigurableTreeItemProps: Omit<TreeItemProps, "id" | "depth" | "itemIndex" | "listSize" | "selected" | "expanded" | "focused" | "renderChildItems"> & { children?: ReactNode }
ExpandedIds: readonly TreeItemId[]
GetItemProps<T>: (item: T & TreeItemStates) => ConfigurableTreeItemProps | undefined

Type parameters

Type declaration

    • A function to get additional props to pass to each tree item. It will be provided the current item and the "states" of the item merged together.

      Note: It is generally recommended to use the itemRenderer instead for additional functionality as you will have more control. This prop is more for applying custom styles or display data on the item.

      Parameters

      Returns ConfigurableTreeItemProps | undefined

ProvidedTreeProps: Pick<TreeProps, "linkComponent" | "id"> & Required<Pick<TreeProps, "rootId" | "multiSelect" | "labelKey" | "valueKey" | "getItemLabel" | "getItemValue" | "getItemProps" | "expanderLeft" | "expanderIcon">>
SelectedIds: readonly TreeItemId[]
TreeData<T>: Record<TreeItemId, T>

This type represents the data that can be handled by the Tree component. This is really just a Map of itemId -> item for quick lookups for the logic within the tree.

Type parameters

TreeItemId: string
TreeItemRenderer<T>: (providedProps: ProvidedTreeItemProps, item: T & { visibleIndex: number }, treeProps: ProvidedTreeProps) => ReactElement | null

Type parameters

Type declaration

TreeItemSorter<T>: (items: readonly T[]) => readonly T[]

Type parameters

Type declaration

    • (items: readonly T[]): readonly T[]
    • A function to call that will sort the items within the tree for each unique parentId. If you have a tree like:

      a
      ├── a1
      b
      ├── b1
      ├── b2
      │ └── b2.1
      c
      ├── c1
      ├── c2
      └── c3

      This function will be called with:

      • [a1]
      • [b2.1]
      • [b1, b2]
      • [c1, c2, c3]
      • [a, b, c]

      Parameters

      • items: readonly T[]

      Returns readonly T[]

Variables

Tree: ForwardRefExoticComponent<TreeProps<any> & RefAttributes<ListElement>> = ...

Creates an accessible tree widget that allows you to show hierarchical data in a list presentation view. This component requires the selection and expansion state to be provided/controlled but you can use the useTreeItemSelection and useTreeItemExpansion hooks for a great starting point for this functionality.

TreeGroup: ForwardRefExoticComponent<TreeGroupProps & RefAttributes<ListElement>> = ...

The TreeGroup component is used to render a tree item's nested items whenever the expanded prop is true. It uses the Collapse component behind the scenes to animate in-and-out of view and will fully unrender when the expanded prop is false.

TreeItem: ForwardRefExoticComponent<(TreeItemProps & RefAttributes<HTMLLIElement>) & (Pick<TreeItemWithContentComponentProps, keyof TreeItemWithContentComponentProps> & RefAttributes<HTMLLIElement>)> = ...

This component renders an item within a tree with optional child items. This should almost always be used from the itemRenderer prop from the Tree component as it provides a lot of the required a11y props for you.

Functions

  • A "reasonable" default implementation for rendering a tree item that extracts the most used ListItem props and passes them down to the TreeItem.

    This is actually exported from this package so it can be used along with a custom renderer for all items that have isCustom enabled.

    const itemRenderer: TreeItemRenderer<MyTreeItem> = (
    itemProps,
    item,
    treeProps
    ) => {
    const { key } = itemProps;
    const { isCustom } = item;
    if (isCustom) {
    return <MyFancyNonTreeItem item={item} key={key} />
    }

    return defaultTreeItemRenderer(itemProps, item, treeProps);
    }

    Parameters

    • itemProps: ProvidedTreeItemProps

      The provided tree item props that should be passed down for keyboard functionality, accessibility, and a key for the item.

    • item: BaseTreeItem & { visibleIndex: number }

      The item itself. This is used to extract any of the common ListItemChildren props.

    • treeProps: ProvidedTreeProps

      The props for the Tree this item is being rendered in. This is really used so the expanderLeft, expanderIcon, labelKey, getItemLabel, and getItemProps can be used to render the TreeItem itself.

    Returns ReactElement

    a TreeItem or a custom ReactElement

  • getChildItems<T>(data: TreeData<T> | readonly T[], parentId: null | string, recursive?: boolean): readonly T[]
  • Gets all the child items for a specific parent item id. If the recursive argument is enabled, all children of the items will also be returned instead of only the top level items.

    Type parameters

    Parameters

    • data: TreeData<T> | readonly T[]

      Either the flattened tree data or a list of all the tree data to iterate over

    • parentId: null | string

      The parent id to get children of

    • recursive: boolean = false

      Boolean if the children's children should also be returned

    Returns readonly T[]

    a list of all the items for a specific parent item id. Note: if the recursive param is enabled, the list will be ordered so that the children of a item will appear before the next item at the same level. So you either need to sort by parentId or something else if you want a specific order.

  • getItemsFrom<T>(data: TreeData<T>, itemId: null | string): readonly T[]
  • This will get all the items from the provided itemId up to the root of the tree that can be used for drag and drop behavior or building a breadcrumb list.

    Type parameters

    Parameters

    • data: TreeData<T>

      The flattened tree data to navigate.

    • itemId: null | string

      The item id to start the search at.

    Returns readonly T[]

    an ordered list of the current item followed by all the direct parents of that item.

  • A hook that implements the base functionality for expanding different tree items.

    Parameters

    • defaultExpandedIds: ExpandedIds | (() => ExpandedIds)

      Either a list of tree item ids to be expanded by default or a function that will return the list of tree item ids to be expanded by default

    Returns TreeItemExpansion

    An object containing props that can be passed to the Tree component to handle the expansion state within the tree.

  • A hook that implements the base functionality for selecting different tree items.

    Parameters

    • defaultSelectedIds: SelectedIds | (() => SelectedIds)

      The default list of tree item ids that should be expanded by default

    • multiSelect: boolean = false

      Boolean if the tree can have multiple items selected or not.

    Returns Required<TreeItemSelection>

    an object containing props that can be passed to the Tree component to handle the selection state within the tree

Generated using TypeDoc