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

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

11
12
13
14
15
16
17
18
19
const SelectForm = ({
    inputTitle,
    placeholder,
    data,
    selectedData,
    onValueChange,
    subData,
    selectedSubData,
    onSubValueChange,
20
    onUpdateDataPress,
21
22
}) => {
    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

    const onPressOption = (item) => {
35
36
37
        if (item.id === 0) {
            return null
        }
38
39
        if (subOption !== undefined) {
            if (subOptionShow) {
40
41
                onValueChange(selectedOption)
                onSubValueChange(item)
42
43
                modalClose()
            } else {
44
                setSelectedOption(item)
45
46
47
                setSubOptionByOptionId(item)
            }
        } else {
48
            onValueChange(item)
49
50
51
52
53
            modalClose()
        }
    }

    const setSubOptionByOptionId = (item) => {
54
        const newOption = subData.filter((subItem) => {
55
56
57
58
            if (subItem.foreign_id === item.id)
                return true;
        })
        if (newOption.length === 0) {
59
60
61
            onValueChange(item)
            onSubValueChange(INIT_SUBCATEGORY)
            modalClose()
62
        } else {
63
64
65
66
67
68
69
70
71
72
            if (newOption.length % 3 == 0) {
                setSubOption(newOption)
                setSubOptionShow(true)
            } else {
                for (let i = 0; i < (newOption.length % 3); i++) {
                    newOption.push(INIT_SUBCATEGORY)
                }
                setSubOption(newOption)
                setSubOptionShow(true)
            }
73
74
        }
    }
75
    
76
77
78
79
80
81
82
83
84
    const renderOptionItem = ({ item }) => (
        <TouchableOpacity onPress={() => onPressOption(item)} style={style.option}>
            <Text style={style.optionText} >
                {item.value}
            </Text>
        </TouchableOpacity>
    );

    useEffect(() => {
85
86
        setOption(data)
        setSubOption(subData)
87
88
89
        setSubOptionShow(false)
    }, [modalOpen])

90
    return (
91
92
93
        <View>
            <View style={style.container}>
                <View style={style.inputTitleArea}>
94
                    <Text style={style.inputTitle}>{inputTitle}</Text>
95
96
97
98
                </View>
                <View style={style.selectBox}>
                    <TouchableWithoutFeedback onPress={onPressSelectBox}>
                        <Text style={style.textStyle}>
99
100
101
102
103
                            {selectedData.value ?
                                selectedSubData?.value ?
                                    selectedData.value + ' < ' + selectedSubData.value
                                    : selectedData.value
                                : placeholder}
104
105
106
                        </Text>
                    </TouchableWithoutFeedback>
                </View>
107
            </View>
108
109
110
111
112
113
114

            <Modal
                transparent
                swipeDirection="down"
                animationType="slide"
                visible={modalOpen}
                onRequestClose={modalClose}
115
            >
116
117
                <View style={style.selectModalContainer}>
                    <TouchableWithoutFeedback onPress={modalClose}>
118
                        <View style={{ flex: 1 }} />
119
120
121
122
123
124
                    </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); }} />
125
                                    <Text style={style.modalHeaderText}>{selectedOption.value}</Text>
126
127
                                </View>
                                :
128
                                <Text style={style.modalHeaderText}>{inputTitle}</Text>}
129
                            <View style={{ flexDirection: "row" }}>
130
                                <MaterialCommunityIcons name='playlist-edit' size={35} color='white' onPress={() => { modalClose(); setTimeout(() => { onUpdateDataPress() }, 500) }} />
131
132
133
                                <MaterialCommunityIcons name='close' size={35} color='white' onPress={modalClose} />
                            </View>
                        </View>
134
                        <View style={[style.modalBody]}>
135
136
137
138
139
140
141
142
143
144
                            <FlatList
                                data={subOptionShow ? subOption : option}
                                renderItem={renderOptionItem}
                                numColumns={3}
                                keyExtractor={item => item.id.toString()}
                            />
                        </View>
                    </View>
                </View>
            </Modal>
145
146
147
148
        </View>
    );
};

149
const style = StyleSheet.create({
150
    container: {
151
        height: 50,
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
        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,
    },
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
205
206
207
208
209
210
211
212
213
214
215
216
    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,
    }
217
218
219
})

export default SelectForm