-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathApp.js
More file actions
231 lines (209 loc) · 6.15 KB
/
Copy pathApp.js
File metadata and controls
231 lines (209 loc) · 6.15 KB
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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import React, { Component } from 'react';
import {
StyleSheet,
Text,
View,
Button,
TextInput,
Switch,
} from 'react-native';
import { addDays } from 'date-fns/addDays';
import { format } from 'date-fns/format';
import { subDays } from 'date-fns/subDays';
import CalendarPicker from './CalendarPicker';
export default class App extends Component {
constructor(props) {
super(props);
let minDate = subDays(new Date(), 15);
let day = minDate;
let customDatesStyles = [];
for (let i = 0; i < 30; i++) {
customDatesStyles.push({
date: day,
// Random colors
style: { backgroundColor: '#' + ('#00000' + (Math.random() * (64 << 22) | 32768).toString(16)).slice(-6) },
textStyle: { color: 'black' }, // sets the font color
containerStyle: [], // extra styling for day container
});
day = addDays(day, 1);
}
this.state = {
customDatesStyles,
enableRangeSelect: false,
minDate,
maxDate: addDays(new Date(), 90),
minRangeDuration: "1",
maxRangeDuration: "5",
selectedStartDate: null,
};
this.onDateChange = this.onDateChange.bind(this);
this.clear = this.clear.bind(this);
this.toggleEnableRange = this.toggleEnableRange.bind(this);
this.onMinRangeDuration = this.onMinRangeDuration.bind(this);
this.onMaxRangeDuration = this.onMaxRangeDuration.bind(this);
this.onSelectToday = this.onSelectToday.bind(this);
this.calendarRef = React.createRef(null);
}
onDateChange(date, type) {
if (type === "START_DATE") {
this.setState({
selectedStartDate: date,
});
}
else {
this.setState({
selectedEndDate: date,
});
}
}
clear() {
this.setState({
selectedStartDate: null,
selectedEndDate: null,
});
}
toggleEnableRange(text) {
this.setState({
enableRangeSelect: !this.state.enableRangeSelect,
selectedStartDate: null,
selectedEndDate: null,
});
}
onMinRangeDuration(val) {
let parsedVal = parseInt(val);
this.setState({
minRangeDuration: val && !isNaN(parsedVal) ? parsedVal + "" : undefined,
selectedStartDate: null,
selectedEndDate: null,
});
}
onMaxRangeDuration(val) {
let parsedVal = parseInt(val);
this.setState({
maxRangeDuration: val && !isNaN(parsedVal) ? parsedVal + "" : undefined,
selectedStartDate: null,
selectedEndDate: null,
});
}
onSelectToday() {
const today = new Date()
this.calendarRef.current.handleOnPressDay({ day: today.getDate(), month: today.getMonth(), year: today.getFullYear() })
}
customDayHeaderStylesCallback({ dayOfWeek, month, year }) {
switch (dayOfWeek) {
case 4: // Thursday
return {
style: {
borderRadius: 12,
backgroundColor: 'cyan',
},
textStyle: {
color: 'blue',
fontWeight: 'bold',
}
};
}
}
render() {
const {
customDatesStyles,
enableRangeSelect,
minDate,
maxDate,
minRangeDuration,
maxRangeDuration,
selectedStartDate,
selectedEndDate,
} = this.state;
const formattedStartDate = selectedStartDate ? format(selectedStartDate, 'yyyy-MM-dd') : '';
const formattedEndDate = selectedEndDate ? format(selectedEndDate, 'yyyy-MM-dd') : '';
return (
<View style={styles.container}>
<CalendarPicker
ref={this.calendarRef}
scrollable
selectedStartDate={selectedStartDate}
selectedEndDate={selectedEndDate}
onDateChange={this.onDateChange}
initialDate={minDate}
customDatesStyles={customDatesStyles}
customDayHeaderStyles={this.customDayHeaderStylesCallback}
minDate={minDate}
maxDate={maxDate}
allowRangeSelection={enableRangeSelect}
allowBackwardRangeSelect={enableRangeSelect}
minRangeDuration={minRangeDuration && parseInt(minRangeDuration)}
maxRangeDuration={maxRangeDuration && parseInt(maxRangeDuration)}
headerWrapperStyle={styles.headerWrapperStyle}
/>
<View style={styles.topSpacing}>
<Text style={styles.text}>Selected (Start) date: {formattedStartDate}</Text>
{!!formattedEndDate &&
<Text style={styles.text}>Selected End date: {formattedEndDate}</Text>
}
</View>
<View style={styles.topSpacing}>
<Button onPress={this.clear} title="Clear Selection" />
</View>
<View style={styles.topSpacing}>
<Button onPress={this.onSelectToday} title="Select today" />
</View>
<View style={styles.topSpacing}>
<Text style={styles.text}>Range select:</Text>
</View>
<Switch
trackColor={{ false: "#767577", true: "#81b0ff" }}
thumbColor={enableRangeSelect ? "#f5dd4b" : "#f4f3f4"}
ios_backgroundColor="#3e3e3e"
onValueChange={this.toggleEnableRange}
value={enableRangeSelect}
/>
{enableRangeSelect &&
<View>
<Text style={styles.text}>minRangeDuration:</Text>
<TextInput
style={styles.textInput}
onChangeText={this.onMinRangeDuration}
value={minRangeDuration || ""}
keyboardType={"number-pad"}
/>
<Text style={styles.text}>maxRangeDuration:</Text>
<TextInput
style={styles.textInput}
onChangeText={this.onMaxRangeDuration}
value={maxRangeDuration || ""}
keyboardType={"number-pad"}
/>
</View>
}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#FFFFFF',
marginTop: 100,
alignItems: 'center',
},
topSpacing: {
marginTop: 20
},
text: {
fontSize: 24,
},
textInput: {
height: 40,
fontSize: 24,
borderColor: 'gray',
borderWidth: 1,
},
headerWrapperStyle: {
backgroundColor: '#ffbdab',
borderTopLeftRadius: 20,
borderTopRightRadius: 20,
paddingTop: 20,
paddingBottom: 20,
}
});