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