Note: Make sure you have completed the React Native - Environment Setup instructions till "Creating a new application" step, before proceeding.
prview.mp4
type GenericTreeNode = {
id: number;
objectId: string;
title: string;
data?: GenericTreeNode[];
count?: number;
}
type TreeSpecificNode = GenericTreeNode & {
isExpanded?: boolean;
isSelected?: boolean;
parentNode?: TreeSpecificNode;
};
type TreeData = GenericTreeNode[];
type SelectionChip = {
title: string;
isAll: boolean;
selectedChildren: string;
isLeaf: boolean | undefined;
parentTitle: string | undefined;
};
const PRODUCT_LIST: TreeData = [
{
id: 1,
objectId: "4399e8b7-921e-4c0a-895a-7de42684fd68",
title: "Phones",
data: [
{
id: 1,
objectId: "c0b4d745-2f0a-4bcd-bdc2-eb6a8ebfc46b",
title: "Apple",
count: 180,
data: [
{
id: 1,
objectId: "81428cd4-ba9a-49b3-be8d-a05122e01fb3",
title: "iPhone 15",
count: 60,
data: [
{
title: "64GB",
count: 10,
id: 1,
objectId: "81428cd4-ba9a-49b3-be8d-a05122e01fb3",
},
],
},
],
},
],
},
];
Prop | Default | Type | Required | Description |
---|---|---|---|---|
data | - | TreeData | true | Array of data objects Must be of TreeData Type. |
autoSelectChildren | false | boolean | false | When parent node is selected it will recursively select all of its children. |
autoExpandChildren | false | bool | false | When parent expand is pressed it will expand its children. |
onSelect | - | (items: TreeSpecificNode[]) => void | true | Event handler which is called when any item is selected returning all selected items array (can be used to sync tree state with any local component state) |
onItemExpand | - | (item: TreeSpecificNode) => void | false | Event handler which is called when the node container or expand button pressed (can be used to catch pressed item) |
onItemSelect | - | (item: TreeSpecificNode) => void | false | Similar to onSelect but returning the selected node only instead of all selected nodes array (can be used to build your own state instead of tree state) |
renderSelectionChip | - | (chip: SelectionChip) => JSX.Element | false | Render selection chip instead of default component. |
selectionChipContainerStyle | - | ViewStyle | false | Customize the chip container view. |
nodeContainerStyle | - | ViewStyle | false | Control style of each tree node. |
renderExpandCollapseIcon | - | (isExpanded: boolean) => React.ReactNode | false | Change the expand and collapse icon. |
renderCheckMark | - | (isSelected: boolean) => React.ReactNode) | false | Change check mark icon. |
renderParentItemContent | - | (itemTitle: string, subNodeTitleCount: string, isExpanded: boolean) => React.ReactNode | false | Change parent nodes content container this only includes title and sub nodes title and count. |
renderLeafNodeContent | - | (itemTitle: string, itemCount: string) => React.ReactNode | false | similar to renderParentItemContent |
First, you will need to start Metro, the JavaScript bundler that ships with React Native.
To start Metro, run the following command from the root of your React Native project:
# using npm
npm start
# OR using Yarn
yarn start
Let Metro Bundler run in its own terminal. Open a new terminal from the root of your React Native project. Run the following command to start your Android or iOS app:
# using npm
npm run android
# OR using Yarn
yarn android
# using npm
npm run ios
# OR using Yarn
yarn ios
If everything is set up correctly, you should see your new app running in your Android Emulator or iOS Simulator shortly provided you have set up your emulator/simulator correctly.
This is one way to run your app — you can also run it directly from within Android Studio and Xcode respectively.
yarn test