forked from Cero-Studio/ReactNativeWheelPicker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimePicker.js
More file actions
160 lines (145 loc) · 4.1 KB
/
TimePicker.js
File metadata and controls
160 lines (145 loc) · 4.1 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
import React from 'react';
import PropTypes from 'prop-types';
import {
View,
StyleSheet,
} from 'react-native';
import WheelPicker from './WheelPicker';
import moment from 'moment';
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
flexDirection: 'row',
},
wheelPicker: {
height: 150,
width: null,
flex: 1,
},
});
class TimePicker extends React.Component {
constructor(props) {
super(props);
this.selectedDate = this.props.initDate ? new Date(this.props.initDate) : new Date();
const time12format = hourTo12Format(this.selectedDate.getHours());
this.hours = this.props.hours ? this.props.hours : getHoursArray();
this.minutes = this.props.minutes ? this.props.minutes : getFiveMinutesArray();
this.initHourInex = time12format[0] - 1;
this.initMinuteInex = Math.round(this.selectedDate.getMinutes() / 5);
this.initAmInex = time12format[1] === 'AM' ? 0 : 1;
}
render() {
return (
<View style={styles.container}>
<WheelPicker
style={styles.wheelPicker}
isAtmospheric
isCyclic
isCurved
visibleItemCount={6}
data={this.hours}
selectedItemTextColor={'black'}
onItemSelected={data => this.onHourSelected(data)}
selectedItemPosition={this.initHourInex}
/>
<WheelPicker
style={styles.wheelPicker}
isAtmospheric
isCyclic
isCurved
visibleItemCount={6}
data={this.minutes}
selectedItemTextColor={'black'}
onItemSelected={data => this.onMinuteSelected(data)}
selectedItemPosition={this.initMinuteInex}
/>
<WheelPicker
style={styles.wheelPicker}
isAtmospheric
isCurved
visibleItemCount={6}
data={getAmArray()}
selectedItemTextColor={'black'}
onItemSelected={data => this.onAmSelected(data)}
selectedItemPosition={this.initAmInex}
/>
</View>
);
}
onHourSelected(event) {
const time12format = hourTo12Format(this.selectedDate.getHours());
const newTime12Format = `${event.data} ${time12format[1]}`;
const selectedHour24format = hourTo24Format(newTime12Format);
this.selectedDate.setHours(selectedHour24format);
this.onTimeSelected();
}
onMinuteSelected(event) {
this.selectedDate.setMinutes(event.data);
this.onTimeSelected();
}
onAmSelected(event) {
const time12format = hourTo12Format(this.selectedDate.getHours());
const newTime12Format = `${time12format[0]} ${event.data}`;
const selectedHour24format = hourTo24Format(newTime12Format);
this.selectedDate.setHours(selectedHour24format);
this.onTimeSelected();
}
onTimeSelected() {
if (this.props.onTimeSelected) {
this.props.onTimeSelected(this.selectedDate);
}
}
}
TimePicker.propTypes = {
initDate: PropTypes.string,
onTimeSelected: PropTypes.func,
hours: PropTypes.array,
minutes: PropTypes.array,
};
// it takes in format '12 AM' and return 24 format
function hourTo24Format(hour) {
return parseInt(moment(hour, ['h A']).format('H'), 10);
}
// it takes in format 23 and return [11,'PM'] format
function hourTo12Format(hour) {
const currDate = new Date();
currDate.setHours(hour);
return dateTo12Hour(currDate.toISOString());
}
const dateTo12Hour = (dateString) => {
const localDate = new Date(dateString);
let hour = localDate.getHours();
if (hour === 12) {
return [('12'), ('PM')];
} if (hour === 0) {
return [('12'), ('AM')];
}
const afterMidday = hour % 12 === hour;
hour = afterMidday ? hour : hour % 12;
const amPm = afterMidday ? 'AM' : 'PM';
return [(hour.toString()), (amPm)];
};
function getHoursArray() {
const arr = [];
for (let i = 1; i < 13; i++) {
arr.push(i);
}
return arr;
}
function getFiveMinutesArray() {
const arr = [];
arr.push('00');
arr.push('05');
for (let i = 10; i < 60; i += 5) {
arr.push(`${i}`);
}
return arr;
}
function getAmArray() {
const arr = [];
arr.push('AM');
arr.push('PM');
return arr;
}
module.exports = TimePicker;