Accordion.js 2.45 KB
Newer Older
1
2
3
import React, { useState } from 'react';
import { Animated, Text, View, StyleSheet, Pressable } from 'react-native';
import AntDesign from 'react-native-vector-icons/AntDesign';
4
5

const Accordion = ({
6
7
8
9
10
11
    title,
    left,
    right,
    children,
    titleStyle = style.text,
    backgroundColor = 'lightgray',
12
}) => {
13
    const [opened, setOpened] = useState(false);
14

15
    const handleOpen = () => { setOpened(!opened) };
16
17

    return (
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
        <View>
            <View>
                <Pressable
                    style={[style.flexRow, style.catBox, { backgroundColor: backgroundColor }]}
                    onPress={handleOpen}
                >
                    <View style={[style.flexRow, style.flexCenter]}>
                        {left ? left : null}
                        <Text style={titleStyle}>
                            {title}
                        </Text>
                    </View>
                    <View style={[style.flexRow, style.flexCenter]}>
                        {right ? right : null}
                        <AntDesign
                            name={opened ? 'caretup' : 'caretdown'}
                            color='black'
                            size={24}
                            style={style.rightIcon}
                        />
                    </View>
                </Pressable>
40
41
            </View>

42
43
44
45
            {opened ?
                children
                : null}
        </View>
46
    )
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
};

export const AccordionItem = ({
    title,
    left,
    right,
    titleStyle = style.text,
    backgroundColor = 'lightgray',
    marginLeft = 20,
}) => {
    return (
        <View style={[style.flexRow, style.flexCenter, style.catBox, { backgroundColor: backgroundColor }]}>
            <View style={[style.flexRow, style.flexCenter, { marginLeft: marginLeft }]}>
                {left ? left : null}
                <Text style={titleStyle}>
                    {title}
                </Text>
            </View>
            {right ? right : null}
        </View>
    );
};
69

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const style = StyleSheet.create({
    flexRow: {
        flexDirection: 'row',
    },
    flexCenter: {
        justifyContent: 'center',
        alignItems: 'center',
    },
    catBox: {
        justifyContent: 'space-between',
        paddingVertical: 10,
    },
    rightIcon: {
        marginHorizontal: 5,
        fontSize: 20,
        color: 'black',
    },
87
    text: {
88
89
90
91
92
93
        fontSize: 20,
        marginHorizontal: 10,
    },
})

export default Accordion