UNPKG

@tra-tech/react-native-kitra

Version:
125 lines (124 loc) 5.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _reactNative = require("react-native"); var _react = require("react"); var _reactNativeReanimated = _interopRequireWildcard(require("react-native-reanimated")); var _KitraProvider = require("../../core/KitraProvider"); var _Icon = _interopRequireDefault(require("../Icons/Icon")); 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; } const sizes = { small: { width: 57, height: 24, iconSize: 12, iconwithWidth: 71.54 }, medium: { width: 65, height: 30, iconSize: 15, iconwithWidth: 82.39 }, large: { width: 73, height: 33, iconSize: 17, iconwithWidth: 93.24 } }; const Icons = _reactNativeReanimated.default.createAnimatedComponent(_Icon.default); const Button = _reactNativeReanimated.default.createAnimatedComponent(_reactNative.TouchableOpacity); const Chip = _ref => { var _typographySize$size, _typographySize$size2; let { theme, typography, label, value, textStyle, style, icon, colorStyle = { backgroundColor: theme === null || theme === void 0 ? void 0 : theme.primary, selectBackgroundColor: theme === null || theme === void 0 ? void 0 : theme.white, selectTitleColor: theme === null || theme === void 0 ? void 0 : theme.primary, titleColor: theme === null || theme === void 0 ? void 0 : theme.white }, size = 'small', onChange, disable = false } = _ref; const offset = (0, _reactNativeReanimated.useSharedValue)(0); const [select, setSelect] = (0, _react.useState)(value || false); const typographySize = { small: typography === null || typography === void 0 ? void 0 : typography.body.xsmedium, medium: typography === null || typography === void 0 ? void 0 : typography.body.smedium, large: typography === null || typography === void 0 ? void 0 : typography.body.medium }; const animated = (0, _reactNativeReanimated.useAnimatedStyle)(() => { const colorAnimated = (0, _reactNativeReanimated.interpolateColor)(offset.value, [0, 1], [colorStyle.backgroundColor, colorStyle.selectBackgroundColor]); return { backgroundColor: colorAnimated }; }); const textAnimated = (0, _reactNativeReanimated.useAnimatedStyle)(() => { const colorAnimated = (0, _reactNativeReanimated.interpolateColor)(offset.value, [0, 1], [colorStyle.selectBackgroundColor, colorStyle.backgroundColor]); return { color: colorAnimated }; }); (0, _react.useEffect)(() => { offset.value = (0, _reactNativeReanimated.withTiming)(Number(!!select)); if (onChange) onChange(select); }, [select]); (0, _react.useEffect)(() => { setSelect(!!value); }, [value]); return /*#__PURE__*/React.createElement(Button, { disabled: disable, testID: "chip", onPress: () => { setSelect(prev => !prev); }, activeOpacity: 1, style: [animated, { justifyContent: 'center', alignItems: 'center', height: sizes[size].height, borderWidth: 1, borderRadius: 40, opacity: disable ? 0.7 : 1, borderColor: colorStyle.backgroundColor, width: icon ? sizes[size].iconwithWidth : sizes[size].width }, style] }, /*#__PURE__*/React.createElement(_reactNative.View, { style: { flexDirection: (icon === null || icon === void 0 ? void 0 : icon.iconPosition) === 'right' ? 'row' : 'row-reverse', alignItems: 'center' } }, /*#__PURE__*/React.createElement(_reactNativeReanimated.default.Text, { style: [textAnimated, { fontSize: (_typographySize$size = typographySize[size]) === null || _typographySize$size === void 0 ? void 0 : _typographySize$size.fontSize, lineHeight: (_typographySize$size2 = typographySize[size]) === null || _typographySize$size2 === void 0 ? void 0 : _typographySize$size2.lineHeight, fontWeight: '500' }, textStyle] }, label), icon ? /*#__PURE__*/React.createElement(Icons, { type: icon.iconType, name: icon.iconName, size: sizes[size].iconSize, animatedProps: textAnimated, style: [icon.iconPosition === 'right' ? { marginLeft: 6 } : { marginRight: 6 }] }) : null)); }; var _default = (0, _KitraProvider.applyDefaults)(Chip); exports.default = _default; //# sourceMappingURL=Chip.js.map