1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import React from 'react'
|
7 | import { View, StyleSheet } from 'react-native'
|
8 | import WheelPicker from './WheelPicker'
|
9 | import {
|
10 | hourTo24Format,
|
11 | hourTo12Format,
|
12 | getHoursArray,
|
13 | getFiveMinutesArray,
|
14 | getAmArray,
|
15 | } from './Utils'
|
16 |
|
17 | const AM = 'AM'
|
18 | const HOUR = 60
|
19 |
|
20 | type Event = {
|
21 | data: string | number,
|
22 | position: number,
|
23 | }
|
24 |
|
25 | type Props = {
|
26 | initDate: string,
|
27 | onTimeSelected: Date => void,
|
28 | hours: Array<number>,
|
29 | minutes: Array<string>,
|
30 | format24: boolean,
|
31 | }
|
32 |
|
33 | type State = {
|
34 | selectedDate: Date,
|
35 | hours: Array<number>,
|
36 | minutes: Array<string>,
|
37 | selectedHourIndex: number,
|
38 | selectedMinuteIndex: number,
|
39 | selectedAmIndex: number,
|
40 | }
|
41 |
|
42 | export default class TimePicker extends React.Component<Props, State> {
|
43 | constructor(props: Props) {
|
44 | super(props)
|
45 | const { initDate, format24, minutes } = props
|
46 | const selectedDate = initDate ? new Date(initDate) : new Date()
|
47 | const time12format = hourTo12Format(selectedDate.getHours())
|
48 | const time24format = selectedDate.getHours()
|
49 | const hours = this.props.hours || getHoursArray(format24)
|
50 | const selectedHourIndex = format24 ? time24format : Number(time12format[0]) - 1
|
51 | const minutesCount = minutes ? minutes.length : 12
|
52 |
|
53 | const selectedMinuteIndex = Math.round(
|
54 | selectedDate.getMinutes() / (HOUR / minutesCount)
|
55 | )
|
56 | const selectedAmIndex = time12format[1] === AM ? 0 : 1
|
57 | this.state = {
|
58 | selectedDate,
|
59 | hours,
|
60 | minutes: minutes || getFiveMinutesArray(),
|
61 | selectedHourIndex,
|
62 | selectedMinuteIndex,
|
63 | selectedAmIndex,
|
64 | }
|
65 | }
|
66 |
|
67 | render() {
|
68 | const { hours, selectedHourIndex, minutes, selectedMinuteIndex } = this.state
|
69 |
|
70 | return (
|
71 | <View style={[styles.container, { backgroundColor: this.props.backgroundColor }]}>
|
72 | <WheelPicker
|
73 | isCyclic
|
74 | style={styles.wheelPicker}
|
75 | {...this.props}
|
76 | data={hours}
|
77 | onItemSelected={this.onHourSelected}
|
78 | selectedItem={selectedHourIndex}
|
79 | initPosition={selectedHourIndex}
|
80 | />
|
81 | <WheelPicker
|
82 | style={styles.wheelPicker}
|
83 | isCyclic
|
84 | {...this.props}
|
85 | data={minutes}
|
86 | onItemSelected={this.onMinuteSelected}
|
87 | selectedItem={selectedMinuteIndex}
|
88 | initPosition={selectedMinuteIndex}
|
89 | />
|
90 | {!this.props.format24 && this.renderAm()}
|
91 | </View>
|
92 | )
|
93 | }
|
94 |
|
95 | renderAm() {
|
96 | const { itemTextColor, selectedItemTextColor } = this.props
|
97 | const { selectedAmIndex } = this.state
|
98 | return (
|
99 | <WheelPicker
|
100 | style={styles.wheelPicker}
|
101 | {...this.props}
|
102 | data={getAmArray()}
|
103 | onItemSelected={this.onAmSelected}
|
104 | selectedItem={selectedAmIndex}
|
105 | initPosition={selectedAmIndex}
|
106 | />
|
107 | )
|
108 | }
|
109 |
|
110 | onHourSelected = (position: number) => {
|
111 | this.setState({selectedHourIndex: position})
|
112 | const { selectedDate, hours } = this.state
|
113 | const selectedHour = hours[position]
|
114 |
|
115 | if (this.props.format24) {
|
116 | selectedDate.setHours(Number(selectedHour))
|
117 | } else {
|
118 | const time12format = hourTo12Format(selectedDate.getHours())
|
119 | const newTime12Format = `${selectedHour} ${time12format[1]}`
|
120 | const selectedHour24format = hourTo24Format(newTime12Format)
|
121 | selectedDate.setHours(selectedHour24format)
|
122 | }
|
123 | this.onTimeSelected(selectedDate)
|
124 | }
|
125 |
|
126 | onMinuteSelected = (position: number) => {
|
127 | this.setState({selectedMinuteIndex: position})
|
128 | const selectedDate = this.state.selectedDate
|
129 | selectedDate.setMinutes(Number(this.state.minutes[position]))
|
130 | this.onTimeSelected(selectedDate)
|
131 | }
|
132 |
|
133 | onAmSelected = (position: number) => {
|
134 | this.setState({selectedAmIndex: position})
|
135 | const selectedDate = this.state.selectedDate
|
136 | const time12format = hourTo12Format(selectedDate.getHours())
|
137 | const newTime12Format = `${time12format[0]} ${getAmArray()[position]}`
|
138 | const selectedHour24format = hourTo24Format(newTime12Format)
|
139 | selectedDate.setHours(selectedHour24format)
|
140 | this.onTimeSelected(selectedDate)
|
141 | }
|
142 |
|
143 | onTimeSelected(selectedDate: Date) {
|
144 | if (this.props.onTimeSelected) {
|
145 | this.props.onTimeSelected(selectedDate)
|
146 | }
|
147 | }
|
148 | }
|
149 |
|
150 | const styles = StyleSheet.create({
|
151 | container: {
|
152 | alignItems: 'center',
|
153 | flexDirection: 'row',
|
154 | },
|
155 | wheelPicker: {
|
156 | height: 150,
|
157 | width: null,
|
158 | flex: 1,
|
159 | },
|
160 | })
|
161 |
|