UNPKG

@tra-tech/react-native-kitra

Version:
73 lines (72 loc) 4.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.CheckBox = void 0; var _react = require("react"); var _reactNative = require("react-native"); var _reactNativeReanimated = _interopRequireWildcard(require("react-native-reanimated")); var _KitraProvider = require("../../core/KitraProvider"); var _Octicons = _interopRequireDefault(require("../Icons/Octicons")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } const AnimatedTouchableOpacity = _reactNativeReanimated.default.createAnimatedComponent(_reactNative.TouchableOpacity); const CheckBox = _ref => { let { theme, value, onChange, onPress, style, disabled, iconColor = theme === null || theme === void 0 ? void 0 : theme.white, ...props } = _ref; const [status, setStatus] = (0, _react.useState)(value || false); const animatedValue = (0, _reactNativeReanimated.useSharedValue)(0); const animatedStyle = (0, _reactNativeReanimated.useAnimatedStyle)(() => { const borderColorInterpolation = (0, _reactNativeReanimated.interpolateColor)(animatedValue.value, [0, 1], [disabled ? theme === null || theme === void 0 ? void 0 : theme.disabledDark : theme === null || theme === void 0 ? void 0 : theme.disabledLight, 'transparent']); const backgroundColorInterpolation = (0, _reactNativeReanimated.interpolateColor)(animatedValue.value, [0, 1], [theme === null || theme === void 0 ? void 0 : theme.disabledLightDark, disabled ? theme === null || theme === void 0 ? void 0 : theme.disabledDark : theme === null || theme === void 0 ? void 0 : theme.focused]); return { backgroundColor: backgroundColorInterpolation, borderColor: borderColorInterpolation }; }); const toggle = () => { onPress === null || onPress === void 0 ? void 0 : onPress(!status); setStatus(prev => !prev); }; (0, _react.useEffect)(() => { animatedValue.value = (0, _reactNativeReanimated.withTiming)(Number(!!status)); if (onChange) onChange(status); }, [status]); (0, _react.useEffect)(() => { setStatus(!!value); }, [value]); return /*#__PURE__*/React.createElement(AnimatedTouchableOpacity, _extends({ activeOpacity: 1, onPress: toggle, disabled: disabled, style: [styles.checkBox, animatedStyle, style] }, props), status && /*#__PURE__*/React.createElement(_reactNativeReanimated.default.View, null, /*#__PURE__*/React.createElement(_Octicons.default, { color: iconColor, name: "check", size: 12 }))); }; exports.CheckBox = CheckBox; var _default = (0, _KitraProvider.applyDefaults)(CheckBox); exports.default = _default; const styles = _reactNative.StyleSheet.create({ checkBox: { height: 20, width: 20, borderWidth: 1, alignItems: 'center', justifyContent: 'center', borderRadius: 4 } }); //# sourceMappingURL=CheckBox.js.map