xref: /MusicFree/src/components/panels/types/simpleSelect.tsx (revision 5589cdf32b2bb0f641e5ac7bf1f6152cd6b9b70e)
1import React, {Fragment} from 'react';
2import {ScrollView, StyleSheet} from 'react-native';
3import rpx from '@/utils/rpx';
4import {useSafeAreaInsets} from 'react-native-safe-area-context';
5import PanelBase from '../base/panelBase';
6import {hidePanel} from '../usePanel';
7import ListItem from '@/components/base/listItem';
8import PanelHeader from '../base/panelHeader';
9
10interface ICandidateItem {
11    title?: string;
12    value: any;
13}
14
15interface ISimpleSelectProps {
16    height?: number;
17    header?: string;
18    candidates?: Array<ICandidateItem>;
19    onPress?: (item: ICandidateItem) => void;
20}
21
22export default function SimpleSelect(props: ISimpleSelectProps) {
23    const {
24        height = rpx(520),
25        header = '',
26        candidates = [],
27        onPress,
28    } = props ?? {};
29
30    const safeAreaInsets = useSafeAreaInsets();
31
32    return (
33        <PanelBase
34            height={height}
35            renderBody={() => (
36                <>
37                    <PanelHeader title={header} hideButtons />
38
39                    <ScrollView
40                        style={[
41                            styles.body,
42                            {marginBottom: safeAreaInsets.bottom},
43                        ]}>
44                        {candidates.map((it, index) => {
45                            return (
46                                <Fragment key={`frag-${index}`}>
47                                    <ListItem
48                                        heightType="small"
49                                        withHorizontalPadding
50                                        onPress={() => {
51                                            onPress?.(it);
52                                            hidePanel();
53                                        }}>
54                                        <ListItem.Content
55                                            title={it.title ?? it.value}
56                                        />
57                                    </ListItem>
58                                </Fragment>
59                            );
60                        })}
61                    </ScrollView>
62                </>
63            )}
64        />
65    );
66}
67
68const styles = StyleSheet.create({
69    header: {
70        width: '100%',
71        flexDirection: 'row',
72        padding: rpx(24),
73    },
74    body: {
75        flex: 1,
76    },
77    item: {
78        height: rpx(96),
79        justifyContent: 'center',
80    },
81});
82