@solidx/checkbox 0.7.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/NuGet.config +11 -0
- package/RNCCheckbox.podspec +3 -3
- package/android/build.gradle +19 -8
- package/android/src/main/AndroidManifest.xml +1 -2
- package/android/src/main/java/com/reactnativecommunity/checkbox/ReactCheckBoxEvent.java +3 -8
- package/android/src/main/java/com/reactnativecommunity/checkbox/ReactCheckBoxManager.java +7 -13
- package/dist/js/AndroidCheckBoxNativeComponent.d.ts +2 -0
- package/dist/{AndroidCheckBoxNativeComponent.js → js/AndroidCheckBoxNativeComponent.js} +1 -1
- package/dist/{CheckBox.android.d.ts → js/CheckBox.android.d.ts} +13 -13
- package/dist/{CheckBox.android.js → js/CheckBox.android.js} +7 -5
- package/dist/js/CheckBox.d.ts +5 -0
- package/dist/{CheckBox.ios.d.ts → js/CheckBox.ios.d.ts} +25 -25
- package/dist/{CheckBox.ios.js → js/CheckBox.ios.js} +11 -5
- package/dist/js/CheckBox.js +27 -0
- package/dist/js/CheckBox.windows.d.ts +95 -0
- package/dist/{CheckBox.windows.js → js/CheckBox.windows.js} +20 -21
- package/dist/js/CheckboxNativeComponent.d.ts +11 -0
- package/dist/js/CheckboxNativeComponent.js +22 -0
- package/dist/js/IOSCheckBoxNativeComponent.d.ts +2 -0
- package/dist/{IOSCheckBoxNativeComponent.js → js/IOSCheckBoxNativeComponent.js} +1 -1
- package/dist/js/NativeCheckboxModule.d.ts +11 -0
- package/dist/js/NativeCheckboxModule.js +10 -0
- package/dist/js/RNCCheckboxNativeComponent.d.ts +48 -0
- package/dist/js/RNCCheckboxNativeComponent.js +12 -0
- package/dist/js/index.d.ts +11 -0
- package/dist/js/index.js +34 -0
- package/dist/{setAndForwardRef.d.ts → js/setAndForwardRef.d.ts} +1 -1
- package/dist/{setAndForwardRef.js → js/setAndForwardRef.js} +2 -1
- package/jest.config.windows.js +3 -0
- package/js/AndroidCheckBoxNativeComponent.ts +12 -0
- package/js/CheckBox.android.tsx +217 -0
- package/js/CheckBox.ios.tsx +175 -0
- package/js/CheckBox.ts +9 -0
- package/js/CheckBox.windows.tsx +158 -0
- package/js/CheckboxNativeComponent.ts +25 -0
- package/js/IOSCheckBoxNativeComponent.ts +12 -0
- package/js/NativeCheckboxModule.ts +15 -0
- package/js/RNCCheckboxNativeComponent.ts +68 -0
- package/js/index.ts +17 -0
- package/js/setAndForwardRef.ts +64 -0
- package/metro.config.js +45 -0
- package/package.json +61 -53
- package/react-native.config.js +22 -0
- package/typings/index.d.ts +5 -5
- package/windows/Checkbox/Checkbox.cpp +20 -0
- package/windows/Checkbox/Checkbox.h +26 -0
- package/windows/Checkbox/Checkbox.rc +0 -0
- package/windows/Checkbox/Checkbox.vcxproj +146 -0
- package/windows/Checkbox/Checkbox.vcxproj.filters +56 -0
- package/windows/Checkbox/CheckboxView.cpp +186 -0
- package/windows/Checkbox/CheckboxView.h +59 -0
- package/windows/Checkbox/ReactPackageProvider.cpp +24 -0
- package/windows/Checkbox/ReactPackageProvider.h +24 -0
- package/windows/Checkbox/ReactPackageProvider.idl +9 -0
- package/windows/Checkbox/codegen/.clang-format +2 -0
- package/windows/Checkbox/codegen/NativeCheckboxModuleSpec.g.h +31 -0
- package/windows/Checkbox/codegen_manual/react/components/RNCCheckboxSpec/RNCCheckbox.g.h +232 -0
- package/windows/Checkbox/packages.lock.json +151 -0
- package/windows/Checkbox/pch.cpp +1 -0
- package/windows/Checkbox/pch.h +43 -0
- package/windows/Checkbox/resource.h +5 -0
- package/windows/Checkbox/targetver.h +8 -0
- package/windows/Checkbox.sln +43 -0
- package/windows/ExperimentalFeatures.props +33 -0
- package/dist/AndroidCheckBoxNativeComponent.d.ts +0 -2
- package/dist/CheckBox.d.ts +0 -1
- package/dist/CheckBox.js +0 -2
- package/dist/CheckBox.windows.d.ts +0 -77
- package/dist/IOSCheckBoxNativeComponent.d.ts +0 -2
- package/dist/WindowsCheckBoxComponent.d.ts +0 -2
- package/dist/WindowsCheckBoxComponent.js +0 -6
- package/windows/CheckboxWindows/CheckboxView.cpp +0 -116
- package/windows/CheckboxWindows/CheckboxView.h +0 -33
- package/windows/CheckboxWindows/CheckboxView.idl +0 -11
- package/windows/CheckboxWindows/CheckboxViewManager.cpp +0 -73
- package/windows/CheckboxWindows/CheckboxViewManager.h +0 -44
- package/windows/CheckboxWindows/CheckboxWindows.rc +0 -0
- package/windows/CheckboxWindows/CheckboxWindows.vcxproj +0 -189
- package/windows/CheckboxWindows/CheckboxWindows.vcxproj.filters +0 -43
- package/windows/CheckboxWindows/PropertySheet.props +0 -16
- package/windows/CheckboxWindows/ReactPackageProvider.cpp +0 -18
- package/windows/CheckboxWindows/ReactPackageProvider.h +0 -23
- package/windows/CheckboxWindows/ReactPackageProvider.idl +0 -9
- package/windows/CheckboxWindows/packages.config +0 -4
- package/windows/CheckboxWindows/pch.cpp +0 -1
- package/windows/CheckboxWindows/pch.h +0 -4
- package/windows/CheckboxWindows/readme.txt +0 -23
- package/windows/CheckboxWindows/resource.h +0 -14
- package/windows/CheckboxWindows.sln +0 -226
- package/windows/CheckboxWindows62.sln +0 -254
- /package/windows/{CheckboxWindows/CheckboxWindows.def → Checkbox/Checkbox.def} +0 -0
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
3
4
|
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*
|
|
7
|
-
* @flow strict-local
|
|
8
5
|
* @format
|
|
9
6
|
*/
|
|
10
7
|
'use strict';
|
|
@@ -12,10 +9,12 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
12
9
|
var extendStatics = function (d, b) {
|
|
13
10
|
extendStatics = Object.setPrototypeOf ||
|
|
14
11
|
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
15
|
-
function (d, b) { for (var p in b) if (
|
|
12
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
16
13
|
return extendStatics(d, b);
|
|
17
14
|
};
|
|
18
15
|
return function (d, b) {
|
|
16
|
+
if (typeof b !== "function" && b !== null)
|
|
17
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
19
18
|
extendStatics(d, b);
|
|
20
19
|
function __() { this.constructor = d; }
|
|
21
20
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
@@ -47,39 +46,40 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
47
46
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
48
47
|
};
|
|
49
48
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
-
var nullthrows_1 = __importDefault(require("nullthrows"));
|
|
51
49
|
var react_1 = __importDefault(require("react"));
|
|
52
50
|
var react_native_1 = require("react-native");
|
|
53
|
-
var
|
|
51
|
+
var RNCCheckboxNativeComponent_1 = __importDefault(require("./RNCCheckboxNativeComponent"));
|
|
54
52
|
// @ts-ignore setAndForwardRef type does not exist in @types/react-native
|
|
55
53
|
var setAndForwardRef_1 = __importDefault(require("./setAndForwardRef"));
|
|
54
|
+
/**
|
|
55
|
+
* Renders a boolean checkbox input (Windows).
|
|
56
|
+
*
|
|
57
|
+
* This is a controlled component that requires an `onValueChange` callback that
|
|
58
|
+
* updates the `value` prop in order for the component to reflect user actions.
|
|
59
|
+
* If the `value` prop is not updated, the component will continue to render
|
|
60
|
+
* the supplied `value` prop instead of the expected result of any user actions.
|
|
61
|
+
*/
|
|
56
62
|
var CheckBox = /** @class */ (function (_super) {
|
|
57
63
|
__extends(CheckBox, _super);
|
|
58
64
|
function CheckBox() {
|
|
59
65
|
var _this = _super !== null && _super.apply(this, arguments) || this;
|
|
60
66
|
_this._nativeRef = null;
|
|
61
|
-
_this._setNativeRef = setAndForwardRef_1.default({
|
|
67
|
+
_this._setNativeRef = (0, setAndForwardRef_1.default)({
|
|
62
68
|
getForwardedRef: function () { return _this.props.forwardedRef; },
|
|
63
69
|
setLocalRef: function (ref) {
|
|
64
70
|
_this._nativeRef = ref;
|
|
65
71
|
},
|
|
66
72
|
});
|
|
67
73
|
_this._onChange = function (event) {
|
|
68
|
-
var
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
nullthrows_1.default(_this._nativeRef).setNativeProps({ value: value });
|
|
72
|
-
onValueChange && onValueChange(value);
|
|
74
|
+
var _a = _this.props, onChange = _a.onChange, onValueChange = _a.onValueChange;
|
|
75
|
+
onChange && onChange(event);
|
|
76
|
+
onValueChange && onValueChange(event.nativeEvent.value);
|
|
73
77
|
};
|
|
74
78
|
return _this;
|
|
75
79
|
}
|
|
76
80
|
CheckBox.prototype.render = function () {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
var _a = this.props, style = _a.style, onValueChange = _a.onValueChange, props = __rest(_a, ["style", "onValueChange"]);
|
|
80
|
-
return (react_1.default.createElement(WindowsCheckBoxComponent_1.default, __assign({}, props, {
|
|
81
|
-
// @ts-ignore TODO: implement the type of WindowsCheckBoxNativeComponent
|
|
82
|
-
style: [styles.rctCheckBox, style], ref: this._setNativeRef, onChange: this._onChange })));
|
|
81
|
+
var _a = this.props, style = _a.style, value = _a.value, disabled = _a.disabled, tintColor = _a.tintColor, onCheckColor = _a.onCheckColor, onTintColor = _a.onTintColor, onFillColor = _a.onFillColor, testID = _a.testID, props = __rest(_a, ["style", "value", "disabled", "tintColor", "onCheckColor", "onTintColor", "onFillColor", "testID"]);
|
|
82
|
+
return (react_1.default.createElement(RNCCheckboxNativeComponent_1.default, __assign({}, props, { testID: testID, value: value !== null && value !== void 0 ? value : false, disabled: disabled !== null && disabled !== void 0 ? disabled : false, tintColor: tintColor, onCheckColor: onCheckColor, onTintColor: onTintColor, onFillColor: onFillColor, style: [styles.rctCheckBox, style], ref: this._setNativeRef, onChange: this._onChange })));
|
|
83
83
|
};
|
|
84
84
|
return CheckBox;
|
|
85
85
|
}(react_1.default.Component));
|
|
@@ -87,7 +87,6 @@ var styles = react_native_1.StyleSheet.create({
|
|
|
87
87
|
rctCheckBox: {
|
|
88
88
|
height: 32,
|
|
89
89
|
width: 32,
|
|
90
|
-
backgroundColor: 'transparent',
|
|
91
90
|
},
|
|
92
91
|
});
|
|
93
92
|
var CheckBoxWithRef = react_1.default.forwardRef(function CheckBoxWithRef(props, ref) {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
* @format
|
|
8
|
+
*/
|
|
9
|
+
import type { HostComponent } from 'react-native';
|
|
10
|
+
declare let CheckboxNativeComponent: HostComponent<any>;
|
|
11
|
+
export default CheckboxNativeComponent;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
* @format
|
|
8
|
+
*/
|
|
9
|
+
'use strict';
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
var react_native_1 = require("react-native");
|
|
12
|
+
var CheckboxNativeComponent;
|
|
13
|
+
if (react_native_1.Platform.OS === 'android') {
|
|
14
|
+
CheckboxNativeComponent = require('./AndroidCheckBoxNativeComponent').default;
|
|
15
|
+
}
|
|
16
|
+
else if (react_native_1.Platform.OS === 'windows') {
|
|
17
|
+
CheckboxNativeComponent = require('./RNCCheckboxNativeComponent').default;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
CheckboxNativeComponent = require('./IOSCheckBoxNativeComponent').default;
|
|
21
|
+
}
|
|
22
|
+
exports.default = CheckboxNativeComponent;
|
|
@@ -8,4 +8,4 @@
|
|
|
8
8
|
'use strict';
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
10
|
var react_native_1 = require("react-native");
|
|
11
|
-
exports.default = react_native_1.requireNativeComponent('RNCCheckbox');
|
|
11
|
+
exports.default = (0, react_native_1.requireNativeComponent)('RNCCheckbox');
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*
|
|
5
|
+
* @format
|
|
6
|
+
*/
|
|
7
|
+
import type { TurboModule } from 'react-native';
|
|
8
|
+
export interface Spec extends TurboModule {
|
|
9
|
+
}
|
|
10
|
+
declare const _default: Spec | null;
|
|
11
|
+
export default _default;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*
|
|
5
|
+
* @format
|
|
6
|
+
*/
|
|
7
|
+
'use strict';
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
var react_native_1 = require("react-native");
|
|
10
|
+
exports.default = react_native_1.TurboModuleRegistry.get('Checkbox');
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*
|
|
5
|
+
* @format
|
|
6
|
+
*/
|
|
7
|
+
import type { ViewProps, ColorValue, HostComponent, CodegenTypes } from 'react-native';
|
|
8
|
+
type CheckBoxChangeEvent = Readonly<{
|
|
9
|
+
value: boolean;
|
|
10
|
+
}>;
|
|
11
|
+
export interface RNCCheckboxNativeProps extends ViewProps {
|
|
12
|
+
/**
|
|
13
|
+
* If true the user won't be able to toggle the checkbox.
|
|
14
|
+
* Default value is false.
|
|
15
|
+
*/
|
|
16
|
+
disabled?: CodegenTypes.WithDefault<boolean, false>;
|
|
17
|
+
/**
|
|
18
|
+
* The value of the checkbox. If true the checkbox will be turned on.
|
|
19
|
+
* Default value is false.
|
|
20
|
+
*/
|
|
21
|
+
value?: CodegenTypes.WithDefault<boolean, false>;
|
|
22
|
+
/**
|
|
23
|
+
* Used to set the color of the checkbox when it is disabled.
|
|
24
|
+
*/
|
|
25
|
+
tintColor?: ColorValue;
|
|
26
|
+
/**
|
|
27
|
+
* Used to set the color of the check mark when the checkbox is checked.
|
|
28
|
+
*/
|
|
29
|
+
onCheckColor?: ColorValue;
|
|
30
|
+
/**
|
|
31
|
+
* Used to set the tint color of the checkbox when it is checked.
|
|
32
|
+
*/
|
|
33
|
+
onTintColor?: ColorValue;
|
|
34
|
+
/**
|
|
35
|
+
* Used to set the fill color of the checkbox when it is checked.
|
|
36
|
+
*/
|
|
37
|
+
onFillColor?: ColorValue;
|
|
38
|
+
/**
|
|
39
|
+
* The label to display next to the checkbox.
|
|
40
|
+
*/
|
|
41
|
+
label?: string;
|
|
42
|
+
/**
|
|
43
|
+
* Called when the checkbox value changes.
|
|
44
|
+
*/
|
|
45
|
+
onChange?: CodegenTypes.BubblingEventHandler<CheckBoxChangeEvent>;
|
|
46
|
+
}
|
|
47
|
+
declare const _default: HostComponent<RNCCheckboxNativeProps>;
|
|
48
|
+
export default _default;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*
|
|
5
|
+
* @format
|
|
6
|
+
*/
|
|
7
|
+
'use strict';
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
var react_native_1 = require("react-native");
|
|
10
|
+
exports.default = (0, react_native_1.codegenNativeComponent)('RNCCheckbox', {
|
|
11
|
+
excludedPlatforms: ['iOS', 'android'],
|
|
12
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*
|
|
5
|
+
* @format
|
|
6
|
+
*/
|
|
7
|
+
export * from './CheckBox';
|
|
8
|
+
export { default as RNCCheckbox } from './RNCCheckboxNativeComponent';
|
|
9
|
+
export type { RNCCheckboxNativeProps } from './RNCCheckboxNativeComponent';
|
|
10
|
+
export { default as NativeCheckboxModule } from './NativeCheckboxModule';
|
|
11
|
+
export type { Spec as NativeCheckboxModuleSpec } from './NativeCheckboxModule';
|
package/dist/js/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Microsoft Corporation.
|
|
4
|
+
* Licensed under the MIT License.
|
|
5
|
+
*
|
|
6
|
+
* @format
|
|
7
|
+
*/
|
|
8
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
9
|
+
if (k2 === undefined) k2 = k;
|
|
10
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
11
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
12
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
13
|
+
}
|
|
14
|
+
Object.defineProperty(o, k2, desc);
|
|
15
|
+
}) : (function(o, m, k, k2) {
|
|
16
|
+
if (k2 === undefined) k2 = k;
|
|
17
|
+
o[k2] = m[k];
|
|
18
|
+
}));
|
|
19
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
20
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
21
|
+
};
|
|
22
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.NativeCheckboxModule = exports.RNCCheckbox = void 0;
|
|
27
|
+
// Re-export everything from CheckBox.ts
|
|
28
|
+
__exportStar(require("./CheckBox"), exports);
|
|
29
|
+
// Export the native component and its types (for Windows/New Architecture)
|
|
30
|
+
var RNCCheckboxNativeComponent_1 = require("./RNCCheckboxNativeComponent");
|
|
31
|
+
Object.defineProperty(exports, "RNCCheckbox", { enumerable: true, get: function () { return __importDefault(RNCCheckboxNativeComponent_1).default; } });
|
|
32
|
+
// Re-export the native module for direct access
|
|
33
|
+
var NativeCheckboxModule_1 = require("./NativeCheckboxModule");
|
|
34
|
+
Object.defineProperty(exports, "NativeCheckboxModule", { enumerable: true, get: function () { return __importDefault(NativeCheckboxModule_1).default; } });
|
|
@@ -45,7 +45,8 @@ function setAndForwardRef(_a) {
|
|
|
45
45
|
}
|
|
46
46
|
else if (typeof forwardedRef === 'object' && forwardedRef != null) {
|
|
47
47
|
// Handle createRef-based refs
|
|
48
|
-
|
|
48
|
+
// @ts-ignore - We need to set current even though it's readonly
|
|
49
|
+
forwardedRef.current = ref;
|
|
49
50
|
}
|
|
50
51
|
};
|
|
51
52
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
'use strict';
|
|
9
|
+
|
|
10
|
+
import {requireNativeComponent} from 'react-native';
|
|
11
|
+
|
|
12
|
+
export default requireNativeComponent('AndroidCheckBox');
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
'use strict';
|
|
9
|
+
|
|
10
|
+
import nullthrows from 'nullthrows';
|
|
11
|
+
import React from 'react';
|
|
12
|
+
import {
|
|
13
|
+
StyleSheet,
|
|
14
|
+
processColor,
|
|
15
|
+
ViewProps,
|
|
16
|
+
HostInstance,
|
|
17
|
+
NativeSyntheticEvent,
|
|
18
|
+
} from 'react-native';
|
|
19
|
+
// @ts-ignore setAndForwardRef type does not exist in @types/react-native
|
|
20
|
+
import setAndForwardRef from './setAndForwardRef';
|
|
21
|
+
|
|
22
|
+
import AndroidCheckBoxNativeComponent from './AndroidCheckBoxNativeComponent';
|
|
23
|
+
|
|
24
|
+
type CheckBoxEvent = NativeSyntheticEvent<
|
|
25
|
+
Readonly<{
|
|
26
|
+
target: number;
|
|
27
|
+
value: boolean;
|
|
28
|
+
}>
|
|
29
|
+
>;
|
|
30
|
+
|
|
31
|
+
type CommonProps = Readonly<
|
|
32
|
+
ViewProps & {
|
|
33
|
+
/**
|
|
34
|
+
* Used in case the props change removes the component.
|
|
35
|
+
*/
|
|
36
|
+
onChange?: (event: CheckBoxEvent) => void;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Invoked with the new value when the value changes.
|
|
40
|
+
*/
|
|
41
|
+
onValueChange?: (value: boolean) => void;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* If true the user won't be able to toggle the checkbox.
|
|
45
|
+
* Default value is false.
|
|
46
|
+
*/
|
|
47
|
+
disabled?: boolean;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Used to locate this view in end-to-end tests.
|
|
51
|
+
*/
|
|
52
|
+
testID?: string;
|
|
53
|
+
}
|
|
54
|
+
>;
|
|
55
|
+
|
|
56
|
+
type NativeProps = Readonly<
|
|
57
|
+
CommonProps & {
|
|
58
|
+
on?: boolean;
|
|
59
|
+
enabled?: boolean;
|
|
60
|
+
tintColors: {true?: any; false?: any} | undefined;
|
|
61
|
+
}
|
|
62
|
+
>;
|
|
63
|
+
|
|
64
|
+
type CheckBoxNativeType = HostInstance;
|
|
65
|
+
|
|
66
|
+
export type Props = Readonly<
|
|
67
|
+
CommonProps & {
|
|
68
|
+
/**
|
|
69
|
+
* The value of the checkbox. If true the checkbox will be turned on.
|
|
70
|
+
* Default value is false.
|
|
71
|
+
*/
|
|
72
|
+
value?: boolean;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Used to get the ref for the native checkbox
|
|
76
|
+
*/
|
|
77
|
+
forwardedRef?: React.Ref<CheckBoxNativeType>;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Controls the colors the checkbox has in checked and unchecked states.
|
|
81
|
+
* TODO: improve this type
|
|
82
|
+
*/
|
|
83
|
+
tintColors?: {true?: any; false?: any};
|
|
84
|
+
}
|
|
85
|
+
>;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Renders a boolean input (Android only).
|
|
89
|
+
*
|
|
90
|
+
* This is a controlled component that requires an `onValueChange` callback that
|
|
91
|
+
* updates the `value` prop in order for the component to reflect user actions.
|
|
92
|
+
* If the `value` prop is not updated, the component will continue to render
|
|
93
|
+
* the supplied `value` prop instead of the expected result of any user actions.
|
|
94
|
+
*
|
|
95
|
+
* ```
|
|
96
|
+
* import React from 'react';
|
|
97
|
+
* import { AppRegistry, StyleSheet, Text, View, CheckBox } from 'react-native';
|
|
98
|
+
*
|
|
99
|
+
* export default class App extends React.Component {
|
|
100
|
+
* constructor(props) {
|
|
101
|
+
* super(props);
|
|
102
|
+
* this.state = {
|
|
103
|
+
* checked: false
|
|
104
|
+
* }
|
|
105
|
+
* }
|
|
106
|
+
*
|
|
107
|
+
* toggle() {
|
|
108
|
+
* this.setState(({checked}) => {
|
|
109
|
+
* return {
|
|
110
|
+
* checked: !checked
|
|
111
|
+
* };
|
|
112
|
+
* });
|
|
113
|
+
* }
|
|
114
|
+
*
|
|
115
|
+
* render() {
|
|
116
|
+
* const {checked} = this.state;
|
|
117
|
+
* return (
|
|
118
|
+
* <View style={styles.container}>
|
|
119
|
+
* <Text>Checked</Text>
|
|
120
|
+
* <CheckBox value={checked} onChange={this.toggle.bind(this)} />
|
|
121
|
+
* </View>
|
|
122
|
+
* );
|
|
123
|
+
* }
|
|
124
|
+
* }
|
|
125
|
+
*
|
|
126
|
+
* const styles = StyleSheet.create({
|
|
127
|
+
* container: {
|
|
128
|
+
* flex: 1,
|
|
129
|
+
* flexDirection: 'row',
|
|
130
|
+
* alignItems: 'center',
|
|
131
|
+
* justifyContent: 'center',
|
|
132
|
+
* },
|
|
133
|
+
* });
|
|
134
|
+
*
|
|
135
|
+
* // skip this line if using Create React Native App
|
|
136
|
+
* AppRegistry.registerComponent('App', () => App);
|
|
137
|
+
* ```
|
|
138
|
+
*
|
|
139
|
+
* @keyword checkbox
|
|
140
|
+
* @keyword toggle
|
|
141
|
+
*/
|
|
142
|
+
class CheckBox extends React.Component<Props> {
|
|
143
|
+
_nativeRef: React.Ref<CheckBoxNativeType> | null = null;
|
|
144
|
+
_setNativeRef = setAndForwardRef({
|
|
145
|
+
getForwardedRef: () => this.props.forwardedRef,
|
|
146
|
+
setLocalRef: (ref: any) => {
|
|
147
|
+
this._nativeRef = ref;
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
_onChange = (event: CheckBoxEvent) => {
|
|
152
|
+
const value = this.props.value || false;
|
|
153
|
+
|
|
154
|
+
// @ts-ignore
|
|
155
|
+
nullthrows(this._nativeRef).setNativeProps({on: value});
|
|
156
|
+
|
|
157
|
+
// Change the props after the native props are set in case the props
|
|
158
|
+
// change removes the component
|
|
159
|
+
this.props.onChange && this.props.onChange(event);
|
|
160
|
+
this.props.onValueChange &&
|
|
161
|
+
this.props.onValueChange(event.nativeEvent.value);
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
getTintColors(tintColors: {true?: any; false?: any} | undefined) {
|
|
165
|
+
return tintColors
|
|
166
|
+
? {
|
|
167
|
+
true: processColor(tintColors.true),
|
|
168
|
+
false: processColor(tintColors.false),
|
|
169
|
+
}
|
|
170
|
+
: undefined;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
render() {
|
|
174
|
+
const {tintColors, style, ...props} = this.props;
|
|
175
|
+
const disabled = this.props.disabled || false;
|
|
176
|
+
const value = this.props.value || false;
|
|
177
|
+
|
|
178
|
+
const nativeProps = {
|
|
179
|
+
...props,
|
|
180
|
+
onStartShouldSetResponder: () => true,
|
|
181
|
+
onResponderTerminationRequest: () => false,
|
|
182
|
+
enabled: !disabled,
|
|
183
|
+
on: value,
|
|
184
|
+
tintColors: this.getTintColors(tintColors),
|
|
185
|
+
style: [styles.rctCheckBox, style],
|
|
186
|
+
};
|
|
187
|
+
return (
|
|
188
|
+
<AndroidCheckBoxNativeComponent
|
|
189
|
+
{...nativeProps}
|
|
190
|
+
ref={this._setNativeRef}
|
|
191
|
+
// @ts-ignore TODO: implement the type of AndroidCheckBoxNativeComponent
|
|
192
|
+
onChange={this._onChange}
|
|
193
|
+
/>
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const styles = StyleSheet.create({
|
|
199
|
+
rctCheckBox: {
|
|
200
|
+
height: 32,
|
|
201
|
+
width: 32,
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Can't use CheckBoxNativeType because it has different props
|
|
207
|
+
*/
|
|
208
|
+
type CheckBoxType = HostInstance;
|
|
209
|
+
|
|
210
|
+
const CheckBoxWithRef = React.forwardRef(function CheckBoxWithRef(
|
|
211
|
+
props: Props,
|
|
212
|
+
ref: React.Ref<CheckBoxType>,
|
|
213
|
+
) {
|
|
214
|
+
return <CheckBox {...props} forwardedRef={ref} />;
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
export default CheckBoxWithRef;
|