Skip to content

native-ly/react-native-checkbox

 
 

Repository files navigation

@react-native-community/checkbox

CircleCI Status Supports Android and iOS MIT License npm version Lean Core Extracted

React Native component for Checkbox

Android Example IOS Example

Support

RN version Checkbox version
> 0.60 >= 0.3 (Support IOS from 0.4)
< 0.60 0.2 (only Android)

Getting started

yarn add @react-native-community/checkbox

or

npm install @react-native-community/checkbox --save

and install cocoapods

npx pod-install

Mostly automatic installation

From react-native >= 0.60 autolinking will take care of the link

for react-native =< 0.59.X

react-native link @react-native-community/checkbox

Manual installation

Manually link the library on Android

android/settings.gradle

include ':react-native-community-checkbox'
project(':react-native-community-checkbox').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-community/checkbox/android')

android/app/build.gradle

dependencies {
   ...
   implementation project(':react-native-community-checkbox')
}

android/app/src/main/.../MainApplication.java

On top, where imports are:

import com.reactnativecommunity.checkbox.ReactCheckBoxPackage;

Add the checkbox class to your list of exported packages.

@Override
protected List<ReactPackage> getPackages() {
    return Arrays.asList(
            new MainReactPackage(),
            new ReactCheckBoxPackage()
    );
}

Migrating from the core react-native module

This module was created when the CheckBox was split out from the core of React Native. To migrate to this module you need to follow the installation instructions above and then change you imports from:

import { CheckBox } from 'react-native';

to:

import CheckBox from '@react-native-community/checkbox';

Usage

Example

import CheckBox from '@react-native-community/checkbox';
  const [toggleCheckBox, setToggleCheckBox] = useState(false)
  
  <CheckBox
    disabled={false}
    value={toggleCheckBox}
    onValueChange={() => toggleCheckBox ? setToggleCheckBox(false) : setToggleCheckBox(true)}
  />

Check out the example project for more examples.

Props

Common Props

View props...

Prop name Type Description
onChange function Invoked on change with the native event.
onValueChange function Invoked with the new boolean value when it changes.
value boolean The value of the checkbox. If true the checkbox will be turned on. Default value is false.
testID string Used to locate this view in end-to-end tests.

Android Only Props

Prop name Type Description
disabled boolean If true the user won't be able to toggle the checkbox. Default value is false.
tintColors string An object with the following shape: { true?: ?ColorValue, false?: ?ColorValue }. The color value for true will be used when the checkbox is checked, and the color value for false will be used when it is off.

IOS Only Props

Prop name Type Description
lineWidth boolean The width of the lines of the check mark and box. Defaults to 2.0.
hideBox boolean ontrol if the box should be hidden or not. Defaults to false
boxType 'circle' or 'square' The type of box to use. Defaults to 'circle'
tintColor string The color of the box when the checkbox is Off. Defaults to '#aaaaaa'
onCheckColor string The color of the check mark when it is On. Defaults to '#007aff'
onFillColor string The color of the inside of the box when it is On. Defaults to transparent
onTintColor string The color of the line around the box when it is On. Defaults to '#007aff'
animationDuration number The duration in seconds of the animations. Defaults to 0.5
onAnimationType 'stroke' or 'fill' or 'bounce' or 'flat' or 'one-stroke' or 'fade' The type of animation to use when the checkbox gets checked. Default to 'stroke'
offAnimationType 'stroke' or 'fill' or 'bounce' or 'flat' or 'one-stroke' or 'fade' The type of animation to use when the checkbox gets unchecked. 'stroke'

Contributors

This module was extracted from react-native core.

The implementaion of IOS version refered to BEMCheckBox

License

The library is released under the MIT licence. For more information see LICENSE.

About

Checkbox component for React Native

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 30.7%
  • Java 26.3%
  • Objective-C 15.3%
  • JavaScript 13.4%
  • Ruby 9.1%
  • Starlark 5.2%