react-native-paper
Version:
Material design for React Native
136 lines (134 loc) • 5.52 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.CheckboxAndroid = void 0;
var React = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _utils = require("./utils");
var _theming = require("../../core/theming");
var _MaterialCommunityIcon = _interopRequireDefault(require("../MaterialCommunityIcon"));
var _TouchableRipple = _interopRequireDefault(require("../TouchableRipple/TouchableRipple"));
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); }
// From https://material.io/design/motion/speed.html#duration
const ANIMATION_DURATION = 100;
/**
* Checkboxes allow the selection of multiple options from a set.
* This component follows platform guidelines for Android, but can be used
* on any platform.
*
* @extends TouchableRipple props https://callstack.github.io/react-native-paper/docs/components/TouchableRipple
*/
const CheckboxAndroid = _ref => {
let {
status,
theme: themeOverrides,
disabled,
onPress,
testID,
...rest
} = _ref;
const theme = (0, _theming.useInternalTheme)(themeOverrides);
const {
current: scaleAnim
} = React.useRef(new _reactNative.Animated.Value(1));
const isFirstRendering = React.useRef(true);
const {
animation: {
scale
}
} = theme;
React.useEffect(() => {
// Do not run animation on very first rendering
if (isFirstRendering.current) {
isFirstRendering.current = false;
return;
}
const checked = status === 'checked';
_reactNative.Animated.sequence([_reactNative.Animated.timing(scaleAnim, {
toValue: 0.85,
duration: checked ? ANIMATION_DURATION * scale : 0,
useNativeDriver: false
}), _reactNative.Animated.timing(scaleAnim, {
toValue: 1,
duration: checked ? ANIMATION_DURATION * scale : ANIMATION_DURATION * scale * 1.75,
useNativeDriver: false
})]).start();
}, [status, scaleAnim, scale]);
const checked = status === 'checked';
const indeterminate = status === 'indeterminate';
const {
rippleColor,
selectionControlColor
} = (0, _utils.getAndroidSelectionControlColor)({
theme,
disabled,
checked,
customColor: rest.color,
customUncheckedColor: rest.uncheckedColor
});
const borderWidth = scaleAnim.interpolate({
inputRange: [0.8, 1],
outputRange: [7, 0]
});
const icon = indeterminate ? 'minus-box' : checked ? 'checkbox-marked' : 'checkbox-blank-outline';
return /*#__PURE__*/React.createElement(_TouchableRipple.default, _extends({}, rest, {
borderless: true,
rippleColor: rippleColor,
onPress: onPress,
disabled: disabled,
accessibilityRole: "checkbox",
accessibilityState: {
disabled,
checked
},
accessibilityLiveRegion: "polite",
style: styles.container,
testID: testID,
theme: theme
}), /*#__PURE__*/React.createElement(_reactNative.Animated.View, {
style: {
transform: [{
scale: scaleAnim
}]
}
}, /*#__PURE__*/React.createElement(_MaterialCommunityIcon.default, {
allowFontScaling: false,
name: icon,
size: 24,
color: selectionControlColor,
direction: "ltr"
}), /*#__PURE__*/React.createElement(_reactNative.View, {
style: [_reactNative.StyleSheet.absoluteFill, styles.fillContainer]
}, /*#__PURE__*/React.createElement(_reactNative.Animated.View, {
style: [styles.fill, {
borderColor: selectionControlColor
}, {
borderWidth
}]
}))));
};
exports.CheckboxAndroid = CheckboxAndroid;
CheckboxAndroid.displayName = 'Checkbox.Android';
const styles = _reactNative.StyleSheet.create({
container: {
borderRadius: 18,
width: 36,
height: 36,
padding: 6
},
fillContainer: {
alignItems: 'center',
justifyContent: 'center'
},
fill: {
height: 14,
width: 14
}
});
var _default = CheckboxAndroid; // @component-docs ignore-next-line
exports.default = _default;
//# sourceMappingURL=CheckboxAndroid.js.map