UNPKG

@tra-tech/react-native-kitra

Version:
115 lines 3.87 kB
import { TouchableOpacity, View } from 'react-native'; import { useEffect, useState } from 'react'; import Animated, { interpolateColor, useAnimatedStyle, useSharedValue, withTiming } from 'react-native-reanimated'; import { applyDefaults } from '../../core/KitraProvider'; import Icon from '../Icons/Icon'; 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 = Animated.createAnimatedComponent(Icon); const Button = Animated.createAnimatedComponent(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 = useSharedValue(0); const [select, setSelect] = 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 = useAnimatedStyle(() => { const colorAnimated = interpolateColor(offset.value, [0, 1], [colorStyle.backgroundColor, colorStyle.selectBackgroundColor]); return { backgroundColor: colorAnimated }; }); const textAnimated = useAnimatedStyle(() => { const colorAnimated = interpolateColor(offset.value, [0, 1], [colorStyle.selectBackgroundColor, colorStyle.backgroundColor]); return { color: colorAnimated }; }); useEffect(() => { offset.value = withTiming(Number(!!select)); if (onChange) onChange(select); }, [select]); 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(View, { style: { flexDirection: (icon === null || icon === void 0 ? void 0 : icon.iconPosition) === 'right' ? 'row' : 'row-reverse', alignItems: 'center' } }, /*#__PURE__*/React.createElement(Animated.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)); }; export default applyDefaults(Chip); //# sourceMappingURL=Chip.js.map