-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
191 lines (158 loc) · 4.65 KB
/
popup.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Make sure to `npm i react-native-element-dropdown`
import React, { useCallback, useRef, useState } from 'react';
import { StyleSheet, View, Modal, Text, TextInput, TouchableOpacity, } from 'react-native';
import { Dropdown } from 'react-native-element-dropdown';
import Calendar from './Calendar';
const MONTH_NAMES = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const SHORT_MONTH_PICKER = MONTH_NAMES.map((val, index) => {
return ({
value: index,
label: val.substring(0, 3)
})
})
const DateSelectorPopup = ({ calendarRef, setSelectorVisible }) => {
const calendarDate = calendarRef.current.getDate()
const [selectorMonth, setSelectorMonth] = useState(calendarDate.month)
const inputRef = useRef(null);
const selectorYearRef = useRef(calendarDate.year)
const closeSelector = useCallback(() => {
calendarRef.current.setDate(1, selectorMonth, selectorYearRef.current)
setSelectorVisible(false)
}, [selectorMonth])
return (<Modal
transparent={true}
animationType="slide"
onRequestClose={closeSelector}>
<View style={Styles.selector_wrapper}>
<Text style={Styles.selector_title}>Select Month & Year</Text>
<View style={Styles.input_wrapper}>
<Dropdown
style={Styles.dropdown}
itemTextStyle={{ textAlign: 'center' }}
selectedTextStyle={{ textAlign: 'center' }}
data={SHORT_MONTH_PICKER}
labelField="label"
valueField="value"
maxHeight={200}
onChange={({ value }) => { setSelectorMonth(value) }}
value={selectorMonth}
/>
<TextInput style={Styles.input}
defaultValue={String(calendarDate.year)}
ref={inputRef}
onChangeText={(val) => {
selectorYearRef.current = parseInt(val)
selectorYearRef.current = selectorYearRef.current ? selectorYearRef.current : 2000
}}
placeholderTextColor="white"
placeholder="Enter Year"
keyboardType='number-pad'
/>
</View>
<View style={Styles.btn_wrapper}>
<TouchableOpacity style={[Styles.selectDateBtn, { backgroundColor: "dodgerblue" }]} activeOpacity={0.6} onPress={closeSelector}>
<Text style={Styles.btn_text}>Select</Text>
</TouchableOpacity>
<TouchableOpacity style={Styles.selectDateBtn} activeOpacity={0.6} onPress={() => {
const currentDate = new Date()
selectorYearRef.current = currentDate.getFullYear()
inputRef.current.setNativeProps({ text: String(selectorYearRef.current) })
setSelectorMonth(currentDate.getMonth())
}}>
<Text style={Styles.btn_text}>Reset</Text>
</TouchableOpacity>
</View>
</View>
</Modal>)
}
export default function App() {
const calendarRef = useRef()
const [selectorVisible, setSelectorVisible] = useState(false)
return (<>
<Calendar
ref={calendarRef}
OnTitlePress={() => {
setSelectorVisible(true)
}}
/>
{/* Date Selector Popup */
selectorVisible && <DateSelectorPopup {...{ setSelectorVisible, calendarRef }} />
}
</>);
}
const Styles = StyleSheet.create({
Style: {
width: "100%",
backgroundColor: 'white',
paddingBottom: 5
},
dateBox: {
flex: 1
},
dateBoxText: {
fontWeight: 'bold'
},
selectDateBtn: {
alignSelf: 'center',
marginBottom: "20%",
backgroundColor: 'gray',
elevation: 5,
borderRadius: 5
},
selector_wrapper: {
height: "95%",
width: "100%",
backgroundColor: "white",
bottom: 0,
position: 'absolute',
borderTopLeftRadius: 20,
borderTopRightRadius: 20
},
selector_title: {
width: "100%",
textAlign: "center",
paddingVertical: 25,
fontWeight: "bold",
fontSize: 18,
borderColor: "lightgray",
borderBottomWidth: 2
},
input_wrapper: {
flexDirection: 'row',
justifyContent: 'space-evenly',
alignItems: 'center',
flex: 1,
overflow: 'hidden'
},
dropdown: {
width: 150,
height: 50,
backgroundColor: "gainsboro",
borderRadius: 5,
paddingRight: 5
},
input: {
width: 150,
height: 50,
backgroundColor: 'gainsboro',
paddingHorizontal: 3,
borderRadius: 5,
borderBottomColor: "rgba(0,0,0,0.3)",
borderBottomWidth: 1.5,
color: "black",
textAlign: "center",
fontSize: 18,
},
btn_wrapper: {
flexDirection: 'row',
justifyContent: 'space-evenly',
alignItems: 'center'
},
btn_text: {
paddingVertical: 15,
paddingHorizontal: 25,
fontSize: 20,
fontWeight: "bold",
color: 'white'
}
});