Documentation
Class: FileTree
Description:
The FileTree
component renders a tree-like representation of files and folders, allowing users to navigate and select files from a repository. It uses the primereact/tree
component and provides custom functionality for expanding, collapsing, and selecting nodes.
Parameters:
fetchFn
(Function
): A function that fetches the file tree data from a repository.- Parameters:
repo
(String
): The repository name.branch
(String
): The branch name.
- Returns:
Object
: An object containing the fetched file tree data, including thetree
property.
- Parameters:
repo
(String
): The name of the repository.branch
(String
): The branch of the repository.filesRef
(Object
): A reference to theFileTree
instance, used for external access to its methods.onSelect
(Function
): A function that is called when a file is selected.- Parameters:
e
(Object
): The event object containing the selected node data.files
(Array
): An array of selected file data.
- Parameters:
multiple
(Boolean
): Whether multiple file selections are allowed.- Default:
false
- Default:
props
(Object
): Additional props passed to the component.setBusy
(Function
): A function to set the busy state of the component.
Returns:
JSX.Element
: The rendered file tree component.
Methods
Method: sync
Description:
The sync
method synchronizes the selected files with the currentSelection
prop, refreshing the file tree if necessary. It returns an array of selected file data.
Parameters:
newSelection
(null
): An optional new selection object.- Default:
null
- Default:
Returns:
Array
: An array of selected file data.
Throws:
None
Example:
const files = FileTreeInstance.sync();
Method: expand
Description:
The expand
method expands the specified path in the file tree.
Parameters:
path
(String
): The path to expand.
Returns:
Boolean
:true
if the path was successfully expanded,false
otherwise.
Throws:
None
Example:
FileTreeInstance.expand("cs/Helpers.cs");
Method: bulkToggle
Description:
The bulkToggle
method toggles the selection of all files in the file tree. If checked
is true
, it selects all files; otherwise, it deselects all files and collapses the tree.
Parameters:
checked
(Boolean
): Whether to select or deselect all files.
Returns:
void
Throws:
None
Example:
FileTreeInstance.bulkToggle(true);
Method: collapse
Description:
The collapse
method collapses the specified path in the file tree. If no path is provided, it collapses the entire tree.
Parameters:
key
(String
): The path to collapse.
Returns:
void
Throws:
None
Example:
FileTreeInstance.collapse("cs/Helpers.cs");
Method: count
Description:
The count
method returns the number of selectable files in the file tree.
Parameters:
None
Returns:
Number
: The number of selectable files.
Throws:
None
Example:
const fileCount = FileTreeInstance.count();
==========
Function: _transform
Description:
The _transform
function takes an array of file data and transforms it into a tree structure suitable for the primereact/tree
component. It also handles selecting nodes based on the selectedKeys
parameter.
Parameters:
files
(Array
): An array of file data.multiple
(Boolean
): Whether multiple selections are allowed.- Default:
false
- Default:
selectedKeys
(Object
): An object containing keys representing selected files.- Default:
{}
- Default:
Returns:
Array
: An array of nodes representing the file tree.
Throws:
None
Example:
const treeData = _transform(fileData, true, { "cs/Helpers.cs": true });
Function: _merge
Description:
The _merge
function recursively merges child nodes in the file tree, combining labels for parent-child relationships.
Parameters:
node
(Object
): A node object from the file tree.
Returns:
Object
: The merged node object.
Throws:
None
Example:
const mergedNode = _merge(treeData[0]);
Function: _find
Description:
The _find
function recursively searches for a node in the file tree based on its key.
Parameters:
nodes
(Array
): An array of nodes to search within.target
(String
): The key of the node to find.
Returns:
Object
: The found node object, ornull
if not found.
Throws:
None
Example:
const foundNode = _find(treeData, "cs/Helpers.cs");
Function: _filter
Description:
The _filter
function filters a selection object, keeping only entries that represent files that are supported and have a valid file extension.
Parameters:
selection
(Object
): The selection object to filter.
Returns:
Object
: The filtered selection object.
Throws:
None
Example:
const filteredSelection = _filter(selection);
Function: _collect
Description:
The _collect
function collects an array of selected file data from the currentSelection
prop and the provided selection
object.
Parameters:
selection
(Object
): An object containing keys representing selected files.
Returns:
Array
: An array of selected file data.
Throws:
None
Example:
const selectedFiles = _collect(selection);
Function: _refresh
Description:
The _refresh
function updates the file tree based on the provided selection
object.
Parameters:
selection
(Object
): An object containing keys representing selected files.
Returns:
void
Throws:
None
Example:
_refresh(selection);
Function: _onSelect
Description:
The _onSelect
function is called when a node is selected in the file tree. It handles multiple selections and updates the selected file data.
Parameters:
e
(Object
): The event object containing the selected node data.
Returns:
void
Throws:
None
Example:
const _onSelect = async (e) => {
// ...
};
Function: _onClick
Description:
The _onClick
function is called when a node is clicked in the file tree. It handles single selections and toggling expanded states.
Parameters:
node
(Object
): The clicked node object.
Returns:
void
Throws:
None
Example:
const _onClick = async ({ node }) => {
// ...
};
Function: _expand
Description:
The _expand
function recursively expands all nodes in the file tree, including their children.
Parameters:
nodes
(Array
): An array of nodes to expand.
Returns:
void
Throws:
None
Example:
_expand(treeData);
Function: _select
Description:
The _select
function recursively selects all selectable files in the file tree, including their children.
Parameters:
nodes
(Array
): An array of nodes to select from.
Returns:
Object
: An object containing keys representing selected files.
Throws:
None
Example:
const selection = _select(treeData);