xref: /MusicFree/src/components/base/listItem.tsx (revision 950202e4d0ef3d640de02a51eedfa777a253f285)
1import React from 'react';
2import {StyleSheet, View} from 'react-native';
3import rpx from '@/utils/rpx';
4import {List} from 'react-native-paper';
5import Tag from './tag';
6import ThemeText from './themeText';
7import IconButton from './iconButton';
8import FastImage from './fastImage';
9import {fontSizeConst} from '@/constants/uiConst';
10
11export interface ILeftProps {
12    /** 序号 */
13    index?: number | string;
14    /** 封面图 */
15    artwork?: string;
16    /** 封面图的兜底 */
17    fallback?: any;
18    /** icon */
19    icon?: Parameters<typeof IconButton>[0];
20    /** 宽度 */
21    width?: number;
22    /** 组件 */
23    component?: () => JSX.Element;
24}
25
26function Left(props?: ILeftProps) {
27    const {
28        index,
29        artwork,
30        fallback,
31        icon,
32        width = rpx(100),
33        component: Component,
34    } = props ?? {};
35
36    return props && Object.keys(props).length ? (
37        Component ? (
38            <Component />
39        ) : (
40            <View style={[leftStyle.artworkWrapper, {width}]}>
41                {index !== undefined ? (
42                    <ThemeText
43                        fontColor="secondary"
44                        style={{
45                            fontStyle: 'italic',
46                            fontSize: Math.min(
47                                (width / `${index}`.length) * 0.8,
48                                fontSizeConst.content,
49                            ),
50                        }}>
51                        {index}
52                    </ThemeText>
53                ) : icon !== undefined ? (
54                    <IconButton {...icon} />
55                ) : (
56                    <FastImage
57                        style={leftStyle.artwork}
58                        uri={
59                            artwork?.startsWith('//')
60                                ? `https:${artwork}`
61                                : artwork
62                        }
63                        emptySrc={fallback}
64                    />
65                )}
66            </View>
67        )
68    ) : (
69        <></>
70    );
71}
72
73const leftStyle = StyleSheet.create({
74    artworkWrapper: {
75        justifyContent: 'center',
76        alignItems: 'center',
77    },
78    artwork: {
79        width: rpx(76),
80        height: rpx(76),
81        borderRadius: rpx(16),
82    },
83});
84
85/** 歌单item */
86interface IListItemProps {
87    /** 标题 */
88    title: string | number;
89    /** 描述 */
90    desc?: string | JSX.Element;
91    /** 标签 */
92    tag?: string;
93    left?: ILeftProps;
94    /** 右侧按钮 */
95    right?: () => JSX.Element;
96    itemPaddingHorizontal?: number;
97    itemPaddingLeft?: number;
98    itemWidth?: number;
99    itemHeight?: number;
100    itemBackgroundColor?: string;
101    onPress?: () => void;
102    onLongPress?: () => void;
103}
104
105export default function ListItem(props: IListItemProps) {
106    const {
107        title,
108        desc,
109        tag,
110        right,
111        itemWidth,
112        itemHeight,
113        onPress,
114        onLongPress,
115        left,
116        itemBackgroundColor,
117        itemPaddingHorizontal = rpx(24),
118        itemPaddingLeft,
119    } = props;
120    return (
121        <List.Item
122            onLongPress={onLongPress}
123            left={() => <Left {...(left ?? {})} />}
124            style={[
125                style.wrapper,
126                {
127                    paddingHorizontal: itemPaddingHorizontal,
128                    paddingLeft: itemPaddingLeft,
129                    width: itemWidth,
130                    height: itemHeight ?? rpx(120),
131                    paddingVertical: 0,
132                    backgroundColor: itemBackgroundColor,
133                },
134            ]}
135            title={() => (
136                <View
137                    style={{
138                        alignItems: 'stretch',
139                        justifyContent: 'center',
140                        height: itemHeight ?? rpx(120),
141                        marginRight: right ? rpx(18) : 0,
142                    }}>
143                    <View style={style.titleWrapper}>
144                        <ThemeText numberOfLines={1} style={style.textWidth}>
145                            {title}
146                        </ThemeText>
147                        {tag ? <Tag tagName={tag} /> : <></>}
148                    </View>
149                    {desc ? (
150                        <ThemeText
151                            fontColor="secondary"
152                            fontSize="description"
153                            numberOfLines={1}
154                            style={[style.textWidth, {marginTop: rpx(18)}]}>
155                            {desc}
156                        </ThemeText>
157                    ) : (
158                        <></>
159                    )}
160                </View>
161            )}
162            titleStyle={{
163                paddingVertical: 0,
164                marginLeft: 0,
165                marginVertical: 0,
166            }}
167            right={right ? right : () => <></>}
168            onPress={onPress}
169        />
170    );
171}
172const style = StyleSheet.create({
173    wrapper: {
174        justifyContent: 'center',
175        width: '100%',
176    },
177    titleWrapper: {
178        flexDirection: 'row',
179        alignItems: 'center',
180        justifyContent: 'space-between',
181    },
182    textWidth: {
183        maxWidth: rpx(460),
184    },
185    artworkWrapper: {
186        width: rpx(76),
187        justifyContent: 'center',
188        alignItems: 'center',
189        marginRight: rpx(12),
190    },
191    artwork: {
192        width: rpx(76),
193        height: rpx(76),
194        borderRadius: rpx(16),
195    },
196});
197