2.9.0 • Published 11 days ago

react-accessible-treeview v2.9.0

Weekly downloads
53
License
MIT
Repository
github
Last release
11 days ago

react-accessible-treeview Build Status Greenkeeper badge npm version License: MIT

A react component that implements the treeview pattern as described by the WAI-ARIA Authoring Practices.

Features

  • Single and multiple selection.
  • Disabled nodes.
  • Extensive key bindings.
  • Highly customizable through the use of the render prop and prop getter patterns.
  • WAI-ARIA compliant.

Documentation and Demo

Prop Types

Prop nameTypeDefault valueDescription
dataarray[node]requiredTree data
nodeRendererfuncrequiredRender prop for the node (see below for more details)
onSelectfuncnoopFunction called when a node changes its selected state
onNodeSelectfuncnoopFunction called when a node was manually selected/deselected
onExpandfuncnoopFunction called when a node changes its expanded state
classNamestring""className to add to the outermost dom element, al ul with role = "tree"
multiSelectboolfalseAllows multiple nodes to be selected
propagateSelectboolfalseIf set to true, selecting a node will also select its descendants
propagateSelectUpwardsboolfalseIf set to true, selecting a node will update the state of its parent (e.g. a parent node in a checkbox will be automatically selected if all of its children are selected
propagateCollapseboolfalseIf set to true, collapsing a node will also collapse its descendants
expandOnKeyboardSelectboolfalseSelecting a node with a keyboard (using Space or Enter) will also toggle its expanded state
togglableSelectboolfalseWhether the selected state is togglable
defaultSelectedIdsarray[]Array with the ids of the default selected nodes
defaultExpandedIdsarray[]Array with the ids of the default expanded nodes
defaultDisabledIdsarray[]Array with the ids of the default disabled nodes
selectedIdsarray[](Controlled) Array with the ids that should be selected
expandedIdsarray[](Controlled) Array with the ids of branch node that should be expanded
clickActionenumSELECTAction to perform on click. One of: EXCLUSIVE_SELECT, FOCUS, SELECT
onBlurfuncnoopCustom onBlur event that is triggered when focusing out of the component as a whole (moving focus between the nodes won't trigger it).

data

An array of nodes. Nodes are objects with the following structure:

PropertyTypeDefaultDescription
idnumber or stringrequiredA nonnegative integer or string that uniquely identifies the node
namestringrequiredUsed to match on key press
childrenarray[id]requiredAn array with the ids of the children nodes.
parentidrequiredThe parent of the node. null for the root node
isBranchbooleanoptionalUsed to indicated whether a node is branch to be able load async data onExpand, default is false
metadataobjectoptionalUsed to add metadata into node object. We do not currently support metadata that is a nested object

The item with parent:null of the array represents the root node and won't be displayed.

Example:

const data = [
  { name: "", children: [1, 4, 9, 10, 11], id: 0, parent: null },
  { name: "src", children: [2, 3], id: 1, parent: 0 },
  { name: "index.js", id: 2, parent: 1 },
  { name: "styles.css", id: 3, parent: 1 },
  { name: "node_modules", children: [5, 7], id: 4, parent: 0 },
  { name: "react-accessible-treeview", children: [6], id: 5, parent: 4 },
  { name: "bundle.js", id: 6, parent: 5 },
  { name: "react", children: [8], id: 7, parent: 4 },
  { name: "bundle.js", id: 8, parent: 7 },
  { name: ".npmignore", id: 9, parent: 0 },
  { name: "package.json", id: 10, parent: 0 },
  { name: "webpack.config.js", id: 11, parent: 0 },
];

The array can also be generated from a nested object using the flattenTree helper (see the examples below). flattenTree preserves metadata.

Data supports non-sequential ids provided by user.

nodeRenderer

  • Arguments: An object containing the following properties:
PropertyTypeDescription
elementobjectThe object that represents the rendered node
getNodePropsfunctionA function which gives back the props to pass to the node
isBranchboolWhether the rendered node is a branch node
isSelectedboolWhether the rendered node is selected
isHalfSelectedbool or undefinedIf the node is a branch node, whether it is half-selected, else undefined
isExpandedbool or undefinedIf the node is a branch node, whether it is expanded, else undefined
isDisabledboolWhether the rendered node is disabled
levelnumberA positive integer that corresponds to the aria-level attribute
setsizenumberA positive integer that corresponds to the aria-setsize attribute
posinsetnumberA positive integer that corresponds to the aria-posinset attribute
handleSelectfunctionFunction to assign to the onClick event handler of the element(s) that will toggle the selected state
handleExpandfunctionFunction to assign to the onClick event handler of the element(s) that will toggle the expanded state
dispatchfunctionFunction to dispatch actions
treeStateobjectstate of the treeview

onSelect

  • Arguments: onSelect({element, isBranch, isExpanded, isSelected, isHalfSelected, isDisabled, treeState }) Note: the function uses the state after the selection.

onNodeSelect

  • Arguments: onNodeSelect({element, isBranch, isSelected, treeState }) Note: the function uses the state right after the selection before propagation.

onExpand

  • Arguments: onExpand({element, isExpanded, isSelected, isHalfSelected, isDisabled, treeState}) Note: the function uses the state after the expansion.

onLoadData

  • Arguments: onLoadData({element, isExpanded, isSelected, isHalfSelected, isDisabled, treeState}) Note: the function uses the state after inital data is loaded and on expansion.

Keyboard Navigation

Follows the same conventions described in https://www.w3.org/TR/wai-aria-practices/examples/treeview/treeview-1/treeview-1b.html and https://www.w3.org/TR/wai-aria-practices/#keyboard-interaction-22.

KeyFunction
Enter or SpaceUpdates the selected node to the current node and triggers onSelect
Down ArrowMoves focus to the next node that is tabbable without opening or closing a node. If focus is on the last node, does nothing.
Up arrow Moves focus to the previous node that is tabbable without opening or closing a node. If focus is on the first node, does nothing.
Right ArrowWhen focus is on a closed node, opens the node; focus does not move. When focus is on an end node, does nothing. When focus is on a open node, moves focus to the first child node.
Left Arrow When focus is on an open node, closes the node. When focus is on a child node that is also either an end node or a closed node, moves focus to its parent node. When focus is on a root node that is also either an end node or a closed node, does nothing.
HomeMoves focus to first node without opening or closing a node.
EndMoves focus to the last node that can be focused without expanding any nodes that are closed.
a-z, A-Z Focus moves to the next node with a name that starts with the typed character. Search wraps to first node if a matching name is not found among the nodes that follow the focused node. Search ignores nodes that are descendants of closed nodes.
* (asterisk) Expands all closed sibling nodes that are at the same level as the focused node. Focus does not move.
Shift + Down ArrowMoves focus to and toggles the selection state of the next node.
Shift + Up ArrowMoves focus to and toggles the selection state of the previous node.
Ctrl + ASelects all nodes in the tree. If all nodes are selected, unselects all nodes.

Mouse Navigation

KeyFunction
ClickToggles parent nodes and also performs one of clickActions = SELECT, EXCLUSIVE_SELECT, FOCUS
Ctrl+ClickIf multiselect is set to true, selects the node without dropping the current selected ones. If false, it selects the clicked node. Doesn't toggle parents.
Shift+ClickIf multiselect is set to true, selects from the node without dropping the current selected ones. If false, it focus the clicked node. Doesn't toggle parents.

Click actions

VariantFunction
SELECTSelects the clicked node (default).
EXCLUSIVE_SELECTSelects the clicked node and deselects the rest.
FOCUSFocuses the clicked node

treeState

The internal state of the component.

PropertyTypeDefaultDescription
selectedIdsSetnew Set(defaultSelectedIds)Set of the ids of the selected nodes
controlledIdsSetnew Set(controlledSelectedIds)Set of the ids of the nodes selected programmatically
tabbableIdnumberdata[0].children[0]Id of the node with tabindex = 0
isFocusedboolfalseWhether the tree has focus
expandedIdsSetnew Set(defaultExpandedIds)Set of the ids of the expanded nodes
halfSelectedIdsSetnew Set()Set of the ids of the selected nodes
lastUserSelectnumberdata[0].children[0]Last selection made directly by the user
lastInteractedWithnumber or nullnullLast node interacted with
lastManuallyTogglednumber or nullnullLast node that was manually selected/deselected
disabledIdsSetnew Set(defaultDisabledIds)Set of the ids of the selected nodes
2.9.0

11 days ago

2.8.3

4 months ago

2.8.2

4 months ago

2.8.1

5 months ago

2.7.0

9 months ago

2.6.1

11 months ago

2.6.0

11 months ago

2.6.3

10 months ago

2.5.4

12 months ago

2.8.0

9 months ago

2.6.2

10 months ago

2.6.1-beta.0

11 months ago

2.5.6

12 months ago

2.5.5

12 months ago

2.5.0

12 months ago

2.4.0

1 year ago

2.5.2

12 months ago

2.5.1

12 months ago

2.5.3

12 months ago

2.3.5

1 year ago

2.3.2

1 year ago

2.3.4

1 year ago

2.3.3

1 year ago

2.3.0

2 years ago

2.3.1

2 years ago

2.2.0

2 years ago

2.1.4

2 years ago

2.1.3

5 years ago

2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago