|
| 1 | +import React, { FC } from 'react'; |
| 2 | +import { TouchableOpacity, View, StyleSheet, Animated } from 'react-native'; |
| 3 | +import { useTheme } from '@shopify/restyle'; |
| 4 | +import Flex from '../../Flex'; |
| 5 | +import { px } from '../util'; |
| 6 | +import Icon from '../../Icon'; |
| 7 | +import Text from '../../Typography/Text'; |
| 8 | +import { Theme } from '../../theme'; |
| 9 | +import { TreeNodeProps } from '../type'; |
| 10 | +import Chevron from './Chevron'; |
| 11 | +import { useTreeNode } from './useTreeNode'; |
| 12 | +import CheckBox from '../../CheckBox'; |
| 13 | + |
| 14 | +const TreeNode: FC<TreeNodeProps> = (props) => { |
| 15 | + const theme = useTheme<Theme>(); |
| 16 | + const { |
| 17 | + icon: customIcon, |
| 18 | + level, |
| 19 | + disabled, |
| 20 | + checkable, |
| 21 | + expanded = false, |
| 22 | + title, |
| 23 | + checked = false, |
| 24 | + data, |
| 25 | + showIcon, |
| 26 | + } = props; |
| 27 | + const { progress, style, handlerCheck, onClick } = useTreeNode(props); |
| 28 | + |
| 29 | + const iconRender = (checked: boolean) => { |
| 30 | + if (customIcon) { |
| 31 | + return customIcon(checked); |
| 32 | + } |
| 33 | + // return ( |
| 34 | + // <Icon |
| 35 | + // name={checked ? 'up' : 'down'} |
| 36 | + // color={checked ? theme.colors.primary200 : '#999999'} |
| 37 | + // /> |
| 38 | + // ); |
| 39 | + return ( |
| 40 | + <CheckBox |
| 41 | + checked={checked} |
| 42 | + onChange={(checked) => { |
| 43 | + console.log(checked); |
| 44 | + }} |
| 45 | + /> |
| 46 | + ); |
| 47 | + }; |
| 48 | + |
| 49 | + return ( |
| 50 | + <Animated.View style={[{ overflow: 'hidden' }, style()]}> |
| 51 | + <TouchableOpacity |
| 52 | + disabled={disabled} |
| 53 | + onPress={() => { |
| 54 | + onClick?.({ expanded, key: data.key, title, checked, disabled }); |
| 55 | + }} |
| 56 | + > |
| 57 | + <View |
| 58 | + style={{ |
| 59 | + height: px(55), |
| 60 | + backgroundColor: '#F5F5F5', |
| 61 | + borderBottomWidth: StyleSheet.hairlineWidth, |
| 62 | + borderBottomColor: '#CCCCCC', |
| 63 | + paddingHorizontal: px(12), |
| 64 | + }} |
| 65 | + > |
| 66 | + <Flex style={{ marginLeft: level * px(16), alignItems: 'center', flex: 1 }}> |
| 67 | + <TouchableOpacity disabled={disabled} onPress={handlerCheck}> |
| 68 | + {checkable && iconRender(checked)} |
| 69 | + </TouchableOpacity> |
| 70 | + |
| 71 | + <View style={{ flex: 1, marginLeft: px(4) }}> |
| 72 | + <Text |
| 73 | + style={{ fontSize: px(14), lineHeight: px(19) }} |
| 74 | + color={disabled ? 'rgba(255, 255, 255, 0.25)' : 'rgba(255, 255, 255, 0.8)'} |
| 75 | + > |
| 76 | + {title} |
| 77 | + </Text> |
| 78 | + </View> |
| 79 | + {!!data.children && !!showIcon && ( |
| 80 | + <Chevron {...{ progress }}> |
| 81 | + <Icon name={checked ? 'up' : 'down'} color="#999999" /> |
| 82 | + </Chevron> |
| 83 | + )} |
| 84 | + </Flex> |
| 85 | + </View> |
| 86 | + </TouchableOpacity> |
| 87 | + </Animated.View> |
| 88 | + ); |
| 89 | +}; |
| 90 | +TreeNode.displayName = 'TreeNode'; |
| 91 | + |
| 92 | +export default TreeNode; |
0 commit comments