SelectForm.js 6.38 KB
Newer Older
1
2
import React, { useState, useEffect } from 'react';
import { TouchableOpacity } from 'react-native';
3
import { StyleSheet, TouchableWithoutFeedback, View, Text, Modal, FlatList } from 'react-native';
4
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
5

6
7
8
9
10
const INIT_SUBCATEGORY = {
    id: 0,
    value: '',
    foreign_id: 0,
}
11

12
13
14
15
16
17
18
19
20
21
22
const SelectForm = ({
    inputTitle,
    placeholder,
    data,
    selectedData,
    onValueChange,
    subData,
    selectedSubData,
    onSubValueChange,
}) => {
    const [option, setOption] = useState([])
23
24
    const [subOption, setSubOption] = useState([])

25
    const [selectedOption, setSelectedOption] = useState({})
26
27
28
29
30
31

    const [subOptionShow, setSubOptionShow] = useState(false)
    const [modalOpen, setModalOpen] = useState(false)

    const onPressSelectBox = () => { setModalOpen(true) }

32
    const modalClose = () => { setModalOpen(false); }
33
34
35
36

    const onPressOption = (item) => {
        if (subOption !== undefined) {
            if (subOptionShow) {
37
38
                onValueChange(selectedOption)
                onSubValueChange(item)
39
40
                modalClose()
            } else {
41
                setSelectedOption(item)
42
43
44
                setSubOptionByOptionId(item)
            }
        } else {
45
            onValueChange(item)
46
47
48
49
50
            modalClose()
        }
    }

    const setSubOptionByOptionId = (item) => {
51
        const newOption = subData.filter((subItem) => {
52
53
54
55
            if (subItem.foreign_id === item.id)
                return true;
        })
        if (newOption.length === 0) {
56
57
58
            onValueChange(item)
            onSubValueChange(INIT_SUBCATEGORY)
            modalClose()
59
60
61
62
63
64
65
66
67
68
69
70
71
72
        } else {
            setSubOption(newOption)
            setSubOptionShow(true)
        }
    }
    const renderOptionItem = ({ item }) => (
        <TouchableOpacity onPress={() => onPressOption(item)} style={style.option}>
            <Text style={style.optionText} >
                {item.value}
            </Text>
        </TouchableOpacity>
    );

    useEffect(() => {
73
74
        setOption(data)
        setSubOption(subData)
75
76
77
        setSubOptionShow(false)
    }, [modalOpen])

78
    return (
79
80
81
        <View>
            <View style={style.container}>
                <View style={style.inputTitleArea}>
82
                    <Text style={style.inputTitle}>{inputTitle}</Text>
83
84
85
86
                </View>
                <View style={style.selectBox}>
                    <TouchableWithoutFeedback onPress={onPressSelectBox}>
                        <Text style={style.textStyle}>
87
88
89
90
91
                            {selectedData.value ?
                                selectedSubData?.value ?
                                    selectedData.value + ' < ' + selectedSubData.value
                                    : selectedData.value
                                : placeholder}
92
93
94
                        </Text>
                    </TouchableWithoutFeedback>
                </View>
95
            </View>
96
97
98
99
100
101
102

            <Modal
                transparent
                swipeDirection="down"
                animationType="slide"
                visible={modalOpen}
                onRequestClose={modalClose}
103
            >
104
105
                <View style={style.selectModalContainer}>
                    <TouchableWithoutFeedback onPress={modalClose}>
106
                        <View style={{ flex: 1 }} />
107
108
109
110
111
112
                    </TouchableWithoutFeedback>
                    <View style={style.selectModal}>
                        <View style={style.modalHeader}>
                            {subOptionShow ?
                                <View style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'center' }} >
                                    <MaterialCommunityIcons name='arrow-left' size={35} color='white' onPress={() => { setSubOptionShow(false); }} />
113
                                    <Text style={style.modalHeaderText}>{selectedOption.value}</Text>
114
115
                                </View>
                                :
116
                                <Text style={style.modalHeaderText}>{inputTitle}</Text>}
117
118
119
120
121
                            <View style={{ flexDirection: "row" }}>
                                <MaterialCommunityIcons name='playlist-edit' size={35} color='white' onPress={() => console.log('카테고리 편집')} />
                                <MaterialCommunityIcons name='close' size={35} color='white' onPress={modalClose} />
                            </View>
                        </View>
122
                        <View style={[style.modalBody]}>
123
124
125
126
127
128
129
130
131
132
                            <FlatList
                                data={subOptionShow ? subOption : option}
                                renderItem={renderOptionItem}
                                numColumns={3}
                                keyExtractor={item => item.id.toString()}
                            />
                        </View>
                    </View>
                </View>
            </Modal>
133
134
135
136
        </View>
    );
};

137
const style = StyleSheet.create({
138
    container: {
139
        height: 50,
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
        flexDirection: 'row',
        alignItems: "center",
        marginHorizontal: 10,
        marginVertical: 3,
        borderWidth: 1.5,
        borderStyle: "solid",
        borderColor: "#1467ff",
        borderRadius: 5,
        backgroundColor: "#f5f5f5",
    },
    inputTitleArea: {
        flex: 1,
    },
    inputTitle: {
        alignSelf: "center",
        color: "#1467ff",
        fontSize: 20,
    },
    selectStyle: {
        flex: 3,
        fontSize: 20,
    },
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
    selectBox: {
        flex: 3,
    },
    textStyle: {
        fontSize: 20,
    },
    selectModalContainer: {
        flex: 1,
        justifyContent: 'flex-end',
    },
    selectModal: {
        flex: 1,
    },
    modalHeader: {
        flex: 1,
        backgroundColor: '#4f4f4f',
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'space-between',
    },
    modalHeaderText: {
        color: 'white',
        marginLeft: 25,
        fontSize: 20,
    },
    modalBody: {
        flex: 5,
        flexDirection: 'row',
        backgroundColor: '#f0f0f0',
    },
    option: {
        flex: 1,
        height: 50,
        borderWidth: 0.8,
        borderStyle: 'solid',
        borderColor: '#575757',
        backgroundColor: '#adadad',
        justifyContent: 'center',
        alignItems: 'center',
    },
    optionText: {
        fontSize: 20,
    }
205
206
207
})

export default SelectForm