# react-native-tree-checkbox

> A simple and elegant checkbox tree for React Native.

Latest version **1.1.1** (published 2024-05-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-native-tree-checkbox
pnpm add react-native-tree-checkbox
yarn add react-native-tree-checkbox
bun add react-native-tree-checkbox
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2024-05-03 |
| First published | 2022-11-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 25.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | CTT Worker |
| Maintainers | upmanager |
| Keywords | react-native, elements, components, checkbox-tree, tree-view, hierarchy |

## Links

- npm: https://www.npmjs.com/package/react-native-tree-checkbox
- Repository: https://github.com/worker722/react-native-tree-checkbox
- Homepage: https://github.com/worker722/react-native-tree-checkbox#readme
- Issues: https://github.com/worker722/react-native-tree-checkbox/issues
- npm.io page: https://npm.io/package/react-native-tree-checkbox

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 1.1.1 (latest) — 2024-05-03
- 1.1.0 — 2022-12-02
- 1.0.9 — 2022-11-30
- 1.0.8 — 2022-11-30
- 1.0.7 — 2022-11-30
- 1.0.6 — 2022-11-30
- 1.0.5 — 2022-11-30
- 1.0.4 — 2022-11-25
- 1.0.3 — 2022-11-25
- 1.0.2 — 2022-11-25
- 1.0.1 — 2022-11-25
- 1.0.0 — 2022-11-25

## README

## react-native-tree-checkbox
A simple and elegant checkbox tree for React Native. Implemented using [react-native-vector-icons](https://github.com/oblador/react-native-vector-icons)

## Getting started
```js
    npm install react-native-tree-checkbox --save
```
or
```js
    yarn add react-native-tree-checkbox
```

Now we need to install [react-native-vector-icons](https://github.com/oblador/react-native-vector-icons).
```js
    npm install react-native-vector-icons --save
```
or
```js
    yarn add react-native-vector-icons
```

#### Source code demo
[react-native-template-components](https://github.com/worker722/react-native-template-components) A beautiful template for React Native.

### Demo
![](https://github.com/devsolution0112/react-native-tree-checkbox/assets/89163655/419111a6-8222-4311-9eb6-ccb32336ff28)
![](https://github.com/devsolution0112/react-native-tree-checkbox/assets/89163655/1fac4c97-28c9-4b64-97d6-ac0e1b52d151)

#### Props

| Props              | Params               | isRequire | Description                                                               |
| ------------------ | -------------------- | --------- | ------------------------------------------------------------------------- |
| data               | Array                | Yes       | Data is a plain array                                                     |
| keyField           | String               | Yes       | Extract the key from the data item                                        |
| textField          | String               | Yes       | Extract the lable from the data item                                      |
| childField         | String               | Yes       | Extract the field children from the data item                             |
| childCheckField    | String               | No        | Extract the child exists from the data item(default check child.length)   |
| onSelect           | (item[])=> void      | Yes       | Selection callback                                                        |
| style              | ViewStyle            | No        | Styling for container view                                                |
| textStyle          | TextStyle            | No        | Styling for text                                                          |
| iconSize           | Number               | No        | Customize icon size                                                       |
| iconColor          | String               | No        | Customize icon color                                                      |
| autoSelectChilds   | Boolean              | No        | Automatically select childs when selecting an item, default is true       |
| autoSelectParents  | Boolean              | No        | Automatically select parent when all childs are selected, default is true |
| openIcon           | Element              | No        | Customize open icon. Only using react-native-vector-icons                 |
| closeIcon          | Element              | No        | Customize close icon. Only using react-native-vector-icons                |
| checkIcon          | Element              | No        | Customize check icon. Only using react-native-vector-icons                |
| unCheckIcon        | Element              | No        | Customize uncheck icon. Only using react-native-vector-icons              |
| renderItem         | (item, isSelect, isOpen, onOpen, onClose, onSelect)=> Element | No | Takes an item from data and renders it into the list |

#### Method
| API                | Description                                           |
| ------------------ | ----------------------------------------------------- |
| clear              | Refresh data                                          |
| setSelectedItem    | The input value is the array of items id              |
| updateListData     | The input values are list data and selected ids array |

#### Example 1
![](https://github.com/worker722/file-upload/blob/master/document/checkboxtree/example1.png)
```javascript
import React, { useEffect, useRef, useState } from 'react';
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import CheckboxTree from 'react-native-tree-checkbox';
import AntDesign from 'react-native-vector-icons/AntDesign';
import Ionicons from 'react-native-vector-icons/Ionicons';

const recursiveData = [
  {
    shopReportName: 'Name 1',
    shopCode: '00001',
    shopType: '2',
    shopId: 1,
    shopName: 'Name 1',
    childs: [
      {
        shopReportName: 'Name 2',
        shopCode: '00002',
        shopType: '3',
        shopId: 2,
        shopName: 'Name 2',
        childs: [
          {
            shopReportName: 'Name 3',
            shopCode: '00003',
            shopType: '4',
            shopId: 3,
            shopName: 'Name 3',
            childs: [
              {
                shopReportName: 'Name 4',
                shopCode: '00004',
                shopType: '4',
                shopId: 4,
                shopName: 'Name 4',
              },
              {
                shopReportName: 'Name 5',
                shopCode: '00005',
                shopType: '4',
                shopId: 5,
                shopName: 'Name 5',
                childs: [
                  {
                    shopReportName: 'Name 6',
                    shopCode: '00006',
                    shopType: '4',
                    shopId: 7,
                    shopName: 'Name 6',
                    childs: [
                      {
                        shopReportName: 'Name 7',
                        shopCode: '00007',
                        shopType: '4',
                        shopId: 7,
                        shopName: 'Name 7',
                      },
                    ],
                  },
                ],
              },
              {
                shopReportName: 'Name 8',
                shopCode: '00008',
                shopType: '4',
                shopId: 8,
                shopName: 'Name 8',
              },
            ],
          },
        ],
      },
    ],
  },
];

export interface Props {}

const CheckboxTreeScreen: React.FC<Props> = _props => {
  const ref: any = useRef();

  useEffect(() => {
    if (ref && ref.current) {
      ref.current.setSelectedItem([1, 2]);
    }
  }, [ref]);

  useEffect(() => {
    if (ref && ref.current) {
      ref.current.updateListData(recursiveData, [1, 2]);
    }
  }, [recursiveData]);

  return (
    <View style={styles.container}>
      <CheckboxTree
        ref={ref}
        data={recursiveData}
        keyField="shopId"
        textField="shopName"
        childField="childs"
        textStyle={{ color: 'black' }}
        iconColor="black"
        iconSize={26}
        openIcon={<AntDesign name="arrowdown" size={26} />}
        closeIcon={<AntDesign name="arrowright" size={26} />}
        renderItem={({ item, isSelect, isOpen, onOpen, onClose, onSelect }) => (
          <View style={styles.wrapItem}>
            {isOpen ? (
              <TouchableOpacity onPress={onClose}>
                <AntDesign size={30} name="arrowright" />
              </TouchableOpacity>
            ) : (
              <TouchableOpacity onPress={onOpen}>
                <AntDesign size={30} name="arrowdown" />
              </TouchableOpacity>
            )}
            <TouchableOpacity onPress={onSelect}>
              <Ionicons
                size={26}
                name={isSelect ? 'checkbox-outline' : 'square-outline'}
              />
            </TouchableOpacity>
            <Text style={styles.name}>{item.shopName}</Text>
          </View>
        )}
        onSelect={item => {
          console.log(`Selected ${item.length} item`);
        }}
      />
    </View>
  );
};

export default CheckboxTreeScreen;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 20,
  },
  wrapItem: {
    flexDirection: 'row',
    alignItems: 'center',
    marginVertical: 8,
  },
  icon: {
    marginHorizontal: 8,
  },
  name: {
    fontSize: 20,
    marginLeft: 8,
  },
});
```

#### Example 2
![](https://github.com/worker722/file-upload/blob/master/document/checkboxtree/example2.png)
```javascript
  import React from 'react';
  import { StyleSheet, View } from 'react-native';
  import CheckboxTree from 'react-native-tree-checkbox';
  import AntDesign from 'react-native-vector-icons/AntDesign';

  const recursiveData = [
    {
      shopReportName: 'Name 1',
      shopCode: '00001',
      shopType: '2',
      shopId: 1,
      shopName: 'Name 1',
      childs: [
        {
          shopReportName: 'Name 2',
          shopCode: '00002',
          shopType: '3',
          shopId: 2,
          shopName: 'Name 2',
          childs: [
            {
              shopReportName: 'Name 3',
              shopCode: '00003',
              shopType: '4',
              shopId: 3,
              shopName: 'Name 3',
              childs: [
                {
                  shopReportName: 'Name 4',
                  shopCode: '00004',
                  shopType: '4',
                  shopId: 4,
                  shopName: 'Name 4',
                },
                {
                  shopReportName: 'Name 5',
                  shopCode: '00005',
                  shopType: '4',
                  shopId: 5,
                  shopName: 'Name 5',
                  childs: [
                    {
                      shopReportName: 'Name 6',
                      shopCode: '00006',
                      shopType: '4',
                      shopId: 7,
                      shopName: 'Name 6',
                      childs: [
                        {
                          shopReportName: 'Name 7',
                          shopCode: '00007',
                          shopType: '4',
                          shopId: 7,
                          shopName: 'Name 7',
                        },
                      ],
                    },
                  ],
                },
                {
                  shopReportName: 'Name 8',
                  shopCode: '00008',
                  shopType: '4',
                  shopId: 8,
                  shopName: 'Name 8',
                },
              ],
            },
          ],
        },
      ],
    },
  ];

  const CheckboxTreenScreen = _props => {
    return (
      <View style={styles.container}>
        <CheckboxTree
          data={recursiveData}
          keyField="shopId"
          textField="shopName"
          childField="childs"
          textStyle={{ color: 'black' }}
          iconColor="black"
          iconSize={26}
          openIcon={<AntDesign name="arrowdown" size={26} />}
          closeIcon={<AntDesign name="arrowright" size={26} />}
          checkIcon={<View />}
          unCheckIcon={<View />}
          renderItem={item => (
            <View style={styles.wrapItem}>
              <AntDesign
                style={styles.iconItem}
                name="folderopen"
                size={20}
              />
              <Text style={styles.text}>{item.shopName}</Text>
            </View>
          )}
          onSelect={item => {
            console.log(`Selected ${item.length} item`);
          }}
        />
      </View>
    );
  };

  export default CheckboxTreenScreen;

  const styles = StyleSheet.create({
    container: {
      flex: 1,
      paddingVertical: 40,
    },
    wrapItem: {
      flexDirection: 'row',
      marginVertical: 8
    },
    text: {
      fontSize: 18
    },
    iconItem:{
      marginHorizontal: 8
    }
  });
```

---
_Source: https://npm.io/package/react-native-tree-checkbox · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
