Accordion.js 4.75 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import React, { useEffect, useRef, useState } from 'react';
import { Animated, Text, View, StyleSheet, TouchableOpacity, FlatList } from 'react-native';

const renderOptionItem = ({ item }) => (
    <View style={[style.flexRow, style.catBox]}>
        <View style={style.flexRow}>
            {/* { item.deletable && <AntDesign name='minuscircle' style={style.cancelIcon} onPress={() => { handleDelete(item) }} />} */}
            <Text style={style.optionText} >
                {item.value}
            </Text>
        </View>
        {/* <View style={[style.flexRow, style.flexCenter]}>
        { item.deletable && <AntDesign name='edit' style={style.icon} onPress={() => { setOption(item); setModalOpen(true) }} /> }
        <AntDesign name='right' style={style.rightIcon} onPress={() => console.log('서브 카테고리 더보기')}/>
      </View> */}
    </View>
);

const Accordion = ({
    item,
    subItems,
    children
}) => {
    let layoutHeight = 0;
    const [bodyMounted, setBodyMounted] = useState(false);
    const [bodyHeight, setBodyHeight] = useState(0);
    let dropDownAnimValueList = useRef(new Animated.Value(0)).current;

    const handleBodyLayout = (e) => {
        if (bodyMounted) return;

        console.log(e.nativeEvent.layout)

        const { height } = e.nativeEvent.layout;
        layoutHeight = height;
        setBodyMounted(true);
        setBodyHeight(height);
    }

    const [open, setOpen] = useState(false);

    // const openList = () => {
    //     Animated.timing(dropDownAnimValueList, {
    //         toValue: 0,
    //         duration: 500,
    //         useNativeDriver: true,
    //     })
    // }

    // const closeList = () => {
    //     Animated.timing(dropDownAnimValueList, {
    //         toValue: -bodyHeight,
    //         duration: 500,
    //         useNativeDriver: true,
    //     })
    // }

    useEffect(() => {
        if (bodyMounted) dropDownAnimValueList.setValue(open ? -layoutHeight : 0);
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [bodyMounted]);

    useEffect(() => {
        // if (shouldAnimate) {
        //     if (!opened) {
        //       Animated.timing(dropDownAnimValueList, {
        //         toValue: 0,
        //         duration: animDuration || 300,
        //         useNativeDriver: true,
        //       }).start();

        //       return;
        //     }

        //     Animated.timing(dropDownAnimValueList, {
        //       toValue: -bodyHeight,
        //       duration: animDuration || 300,
        //       useNativeDriver: true,
        //     }).start();
        //   } else {
        const targetValue = open ? -bodyHeight : 0;

        dropDownAnimValueList.setValue(targetValue);
        //   }
        // if (open) dropDownAnimValueList.setValue(open ? -layoutHeight : 0);
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [open]);

    console.log(open)

    return (
        <Animated.View style={[
            {
                overflow: 'hidden',
                transform: [{
                    translateY: 0,
                }],
            },
        ]}>
            <View style={[style.boxContainer]}>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
101
                <TouchableOpacity onPress={() => { setOpen(!open) }}>
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
                    {children}
                </TouchableOpacity>
            </View>

            <Animated.View style={{
                height: !bodyMounted ? undefined : bodyHeight,
                transform: [
                    {
                        translateY: dropDownAnimValueList,
                    },
                ],
            }}
                onLayout={handleBodyLayout}>
                <FlatList
                    data={subItems}
                    renderItem={renderOptionItem}
                    keyExtractor={item => item.id.toString()}
                />
            </Animated.View>
        </Animated.View>
    )
}

125
126
const Accordion

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const style = StyleSheet.create({
    boxContainer: {
        width: '100%',
        position: 'relative',
    },
    flexRow: {
        flexDirection: 'row',
    },
    flexCenter: {
        justifyContent: 'center',
        alignItems: 'center',
    },
    catBox: {
        justifyContent: 'space-between',
        paddingVertical: 10,
        backgroundColor: 'lightgray',
    },
    zIndex: {
        position: 'absolute',
        zIndex: 15,
        elevation: 1000,
    },
    icon: {
        marginHorizontal: 5,
        fontSize: 30,
        color: 'black',
    },
    cancelIcon: {
        marginHorizontal: 5,
        fontSize: 30,
        color: 'red',
    },
    rightIcon: {
        marginHorizontal: 5,
        fontSize: 20,
        color: 'black',
    },
    optionText: {
        fontSize: 20,
        marginHorizontal: 10,
    },
    items: {
        overflow: "hidden"
    }
})

export default Accordion