xref: /MusicFree/src/pages/fileSelector/fileItem.tsx (revision 5589cdf32b2bb0f641e5ac7bf1f6152cd6b9b70e)
1import React, {memo} from 'react';
2import {Pressable, StyleSheet, View} from 'react-native';
3import rpx from '@/utils/rpx';
4import ThemeText from '@/components/base/themeText';
5import useTextColor from '@/hooks/useTextColor';
6import Checkbox from '@/components/base/checkbox';
7import {TouchableOpacity} from 'react-native-gesture-handler';
8import Icon from '@/components/base/icon.tsx';
9
10const ITEM_HEIGHT = rpx(96);
11
12interface IProps {
13    type: 'folder' | 'file';
14    path: string;
15    parentPath: string;
16    checked?: boolean;
17    onItemPress: (currentChecked?: boolean) => void;
18    onCheckedChange: (checked: boolean) => void;
19}
20function FileItem(props: IProps) {
21    const {
22        type,
23        path,
24        parentPath,
25        checked,
26        onItemPress,
27        onCheckedChange: onCheckChange,
28    } = props;
29
30    const textColor = useTextColor();
31
32    // 返回逻辑
33
34    return (
35        <View style={styles.container}>
36            <Pressable
37                onPress={() => {
38                    onItemPress(checked);
39                }}
40                style={styles.pathWrapper}>
41                <Icon
42                    name={
43                        type === 'folder'
44                            ? 'folder-outline'
45                            : 'document-outline'
46                    }
47                    color={textColor}
48                    style={styles.folderIcon}
49                />
50                <ThemeText
51                    style={styles.path}
52                    numberOfLines={1}
53                    ellipsizeMode="tail">
54                    {path.substring(
55                        parentPath === '/' ? 1 : parentPath.length + 1,
56                    )}
57                </ThemeText>
58            </Pressable>
59            <TouchableOpacity
60                onPress={() => {
61                    onCheckChange(!checked);
62                }}
63                style={styles.checkIcon}>
64                <Checkbox checked={checked} />
65            </TouchableOpacity>
66        </View>
67    );
68}
69
70export default memo(
71    FileItem,
72    (prev, curr) =>
73        prev.checked === curr.checked &&
74        prev.parentPath === curr.parentPath &&
75        prev.path === curr.path,
76);
77
78const styles = StyleSheet.create({
79    container: {
80        width: '100%',
81        height: ITEM_HEIGHT,
82        paddingHorizontal: rpx(24),
83        flexDirection: 'row',
84        alignItems: 'center',
85        justifyContent: 'space-between',
86    },
87    folderIcon: {
88        fontSize: rpx(32),
89        marginRight: rpx(14),
90    },
91    pathWrapper: {
92        flexDirection: 'row',
93        flex: 1,
94        alignItems: 'center',
95        height: '100%',
96        marginRight: rpx(60),
97    },
98    path: {
99        height: '100%',
100        textAlignVertical: 'center',
101    },
102    checkIcon: {
103        padding: rpx(14),
104    },
105});
106