xref: /MusicFree/src/components/base/listItem.tsx (revision aeaa4fceaffc196c37357860db06107210775676)
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,
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    itemWidth?: number;
98    itemHeight?: number;
99    itemBackgroundColor?: string;
100    onPress?: () => void;
101    onLongPress?: () => void;
102}
103
104export default function ListItem(props: IListItemProps) {
105    const {
106        title,
107        desc,
108        tag,
109        right,
110        itemWidth,
111        itemHeight,
112        onPress,
113        onLongPress,
114        left,
115        itemBackgroundColor,
116        itemPaddingHorizontal = rpx(24),
117    } = props;
118    return (
119        <List.Item
120            onLongPress={onLongPress}
121            left={() => <Left {...(left ?? {})} />}
122            style={[
123                style.wrapper,
124                {
125                    paddingHorizontal: itemPaddingHorizontal,
126                    width: itemWidth,
127                    height: itemHeight ?? rpx(120),
128                    paddingVertical: 0,
129                    backgroundColor: itemBackgroundColor,
130                },
131            ]}
132            title={() => (
133                <View
134                    style={{
135                        alignItems: 'stretch',
136                        justifyContent: 'center',
137                        height: itemHeight ?? rpx(120),
138                        marginRight: right ? rpx(18) : 0,
139                    }}>
140                    <View style={style.titleWrapper}>
141                        <ThemeText numberOfLines={1} style={style.textWidth}>
142                            {title}
143                        </ThemeText>
144                        {tag ? <Tag tagName={tag} /> : <></>}
145                    </View>
146                    {desc ? (
147                        <ThemeText
148                            fontColor="secondary"
149                            fontSize="description"
150                            numberOfLines={1}
151                            style={[style.textWidth, {marginTop: rpx(18)}]}>
152                            {desc}
153                        </ThemeText>
154                    ) : (
155                        <></>
156                    )}
157                </View>
158            )}
159            titleStyle={{
160                paddingVertical: 0,
161                marginLeft: 0,
162                marginVertical: 0,
163            }}
164            right={right ? right : () => <></>}
165            onPress={onPress}
166        />
167    );
168}
169const style = StyleSheet.create({
170    wrapper: {
171        justifyContent: 'center',
172        width: '100%',
173    },
174    titleWrapper: {
175        flexDirection: 'row',
176        alignItems: 'center',
177        justifyContent: 'space-between',
178    },
179    textWidth: {
180        maxWidth: rpx(460),
181    },
182    artworkWrapper: {
183        width: rpx(76),
184        justifyContent: 'center',
185        alignItems: 'center',
186        marginRight: rpx(12),
187    },
188    artwork: {
189        width: rpx(76),
190        height: rpx(76),
191        borderRadius: rpx(16),
192    },
193});
194