用于 React Native 的复选框组件
@react-native-community/checkboxReact Native component for Checkbox
| Android Example | IOS Example | Windows Example |
|---|---|---|
| RN version | Checkbox version |
|---|---|
| >= 0.76 (New Architecture) | >= 0.5.20 |
| >= 0.62 to run on Windows | 0.5 |
| > 0.60 & = 0.3 (Support iOS from 0.4) | |
| = 0.60 (and react-native-windows >= 0.63), autolinking takes | |
| care of linking the module on iOS, Android, and Windows — no extra steps are | |
needed beyond installing the package (and running pod install on iOS). |
Only needed for react-native < 0.60 or react-native-windows < 0.63.
Manually link the library on Android
android/settings.gradleinclude ':react-native-community-checkbox'
project(':react-native-community-checkbox').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-community/checkbox/android')android/app/build.gradledependencies {
...
implementation project(':react-native-community-checkbox')
}android/app/src/main/.../MainApplication.javaOn top, where imports are:
import com.reactnativecommunity.checkbox.ReactCheckBoxPackage;Add the checkbox class to your list of exported packages.
@Override
protected List getPackages() {
return Arrays.asList(
new MainReactPackage(),
new ReactCheckBoxPackage()
);
}Manually link the library on Windows
\node_modules\@react-native-community\checkbox\windows\Checkbox\Checkbox.vcxproj.Add a reference to Checkbox to your main application project. From Visual Studio:
Right-click main application project > Add > Reference... Check 'Checkbox' from the 'Project > Solution' tab on the left.
Add #include "winrt/Checkbox.h".
Add PackageProviders().Append(winrt::Checkbox::ReactPackageProvider()); before InitializeComponent();.
react-native moduleThis 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 your imports from:
import { CheckBox } from 'react-native';to:
import CheckBox from '@react-native-community/checkbox';import CheckBox from '@react-native-community/checkbox'; const [toggleCheckBox, setToggleCheckBox] = useState(false)
setToggleCheckBox(newValue)}
/>CheckBox is a controlled component: it renders the value prop and does
not keep its own internal state. You must update value (usually from
onValueChange) for the checkbox to reflect a tap. If value is not updated,
the checkbox stays in the state you passed — this is now consistent across
Android, iOS, and Windows.
Check out the example project for more examples.
| 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. |
| disabled | boolean | If true the user won't be able to toggle the checkbox. Default value is false. |
| Prop name | Type | Description |
|---|---|---|
| tintColors | object | 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. |
| Prop name | Type | Description |
|---|---|---|
| lineWidth | number | The width of the lines of the check mark and box. Defaults to 2.0. |
| hideBox | boolean | Control 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' |
Implemented most of iOS and Android props. Defaults for color styling can be referenced here: https://docs.microsoft.com/en-us/dotnet/framework/wpf/controls/checkbox-styles-and-templates
| Prop name | Type | Description |
|---|---|---|
| disabled | boolean | If true the user won't be able to toggle the checkbox. Default value is false. |
| tintColor | string | The color of the box when the checkbox is Off. |
| onCheckColor | string | The color of the check mark when it is On. |
| onFillColor | string | The color of the inside of the box when it is On. |
| onTintColor | string | The color of the line around the box when it is On. |
This module was extracted from react-native core.
The implementaion of IOS version refered to BEMCheckBox
The library is released under the MIT licence. For more information see LICENSE.
暂无开放 Issues,或尚未同步最近议题。