Skip to content

Commit ea461a2

Browse files
Bugfixes with am/pm switch
1 parent 0456dcc commit ea461a2

File tree

3 files changed

+61
-40
lines changed

3 files changed

+61
-40
lines changed

src/Time/TimeInputs.tsx

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ import {
1010
} from 'react-native'
1111
import { useTheme } from 'react-native-paper'
1212

13-
import { clockTypes, PossibleClockTypes, PossibleInputTypes } from './timeUtils'
13+
import {
14+
clockTypes,
15+
PossibleClockTypes,
16+
PossibleInputTypes,
17+
toHourInputFormat,
18+
toHourOutputFormat,
19+
} from './timeUtils'
1420
import TimeInput from './TimeInput'
1521
import AmPmSwitcher from './AmPmSwitcher'
1622
import { useLatest } from '../utils'
@@ -78,7 +84,7 @@ function TimeInputs({
7884
<TimeInput
7985
ref={startInput}
8086
placeholder={'00'}
81-
value={toInputFormat(hours, is24Hour)}
87+
value={toHourInputFormat(hours, is24Hour)}
8288
clockType={clockTypes.hours}
8389
pressed={focused === clockTypes.hours}
8490
onPress={onFocusInput}
@@ -87,7 +93,7 @@ function TimeInputs({
8793
onSubmitEditing={onSubmitStartInput}
8894
blurOnSubmit={false}
8995
onChanged={(newHoursFromInput) => {
90-
let newHours = toOutputFormat(newHoursFromInput, hours, is24Hour)
96+
let newHours = toHourOutputFormat(newHoursFromInput, hours, is24Hour)
9197
if (newHoursFromInput > 24) {
9298
newHours = 24
9399
}
@@ -135,29 +141,6 @@ function TimeInputs({
135141
)
136142
}
137143

138-
function toInputFormat(hours: number, is24Hour: boolean): number {
139-
if (is24Hour) {
140-
return hours
141-
}
142-
if (hours > 12) {
143-
return hours - 12
144-
}
145-
return hours
146-
}
147-
function toOutputFormat(
148-
newHours: number,
149-
previousHours: number,
150-
is24Hour: boolean
151-
): number {
152-
if (is24Hour) {
153-
return newHours
154-
}
155-
if (previousHours > 12 && newHours <= 12) {
156-
return newHours + 12
157-
}
158-
return newHours
159-
}
160-
161144
const styles = StyleSheet.create({
162145
spaceBetweenInputsAndSwitcher: { width: 12 },
163146
inputContainer: {

src/Time/TimePicker.tsx

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
import * as React from 'react'
22
import { View, StyleSheet, useWindowDimensions } from 'react-native'
33

4-
import { inputTypes, PossibleClockTypes, PossibleInputTypes } from './timeUtils'
4+
import {
5+
inputTypes,
6+
PossibleClockTypes,
7+
PossibleInputTypes,
8+
toHourInputFormat,
9+
toHourOutputFormat,
10+
} from './timeUtils'
511

612
import AnalogClock, { circleSize } from './AnalogClock'
713
import TimeInputs from './TimeInputs'
814

15+
type onChangeFunc = ({
16+
hours,
17+
minutes,
18+
focused,
19+
}: {
20+
hours: number
21+
minutes: number
22+
focused?: undefined | PossibleClockTypes
23+
}) => any
24+
925
function TimePicker({
1026
hours,
1127
minutes,
@@ -19,32 +35,30 @@ function TimePicker({
1935
hours: number
2036
minutes: number
2137
onFocusInput: (type: PossibleClockTypes) => any
22-
onChange: ({
23-
hours,
24-
minutes,
25-
focused,
26-
}: {
27-
hours: number
28-
minutes: number
29-
focused?: undefined | PossibleClockTypes
30-
}) => any
38+
onChange: onChangeFunc
3139
}) {
3240
const dimensions = useWindowDimensions()
3341
const isLandscape = dimensions.width > dimensions.height
3442

3543
// method to check whether we have 24 hours in clock or 12
3644
const is24Hour = React.useMemo(() => {
37-
return false
3845
const formatter = new Intl.DateTimeFormat(undefined, {
3946
hour: '2-digit',
4047
minute: '2-digit',
4148
timeZone: 'UTC',
4249
})
4350
const formatted = formatter.format(new Date(Date.UTC(2020, 1, 1, 23)))
4451
return formatted.includes('23')
45-
// return false
4652
}, [])
4753

54+
const onInnerChange = React.useCallback<onChangeFunc>(
55+
(params) => {
56+
params.hours = toHourOutputFormat(params.hours, hours, is24Hour)
57+
onChange(params)
58+
},
59+
[onChange, hours, is24Hour]
60+
)
61+
4862
return (
4963
<View style={isLandscape ? styles.rootLandscape : styles.rootPortrait}>
5064
<TimeInputs
@@ -59,11 +73,11 @@ function TimePicker({
5973
{inputType === inputTypes.picker ? (
6074
<View style={styles.clockContainer}>
6175
<AnalogClock
62-
hours={hours}
76+
hours={toHourInputFormat(hours, is24Hour)}
6377
minutes={minutes}
6478
focused={focused}
6579
is24Hour={is24Hour}
66-
onChange={onChange}
80+
onChange={onInnerChange}
6781
/>
6882
</View>
6983
) : null}

src/Time/timeUtils.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,27 @@ export function useInputColors(highlighted: boolean) {
195195

196196
return { backgroundColor, color }
197197
}
198+
199+
export function toHourInputFormat(hours: number, is24Hour: boolean): number {
200+
if (is24Hour) {
201+
return hours
202+
}
203+
if (hours > 12) {
204+
return hours - 12
205+
}
206+
return hours
207+
}
208+
209+
export function toHourOutputFormat(
210+
newHours: number,
211+
previousHours: number,
212+
is24Hour: boolean
213+
): number {
214+
if (is24Hour) {
215+
return newHours
216+
}
217+
if (previousHours > 12 && newHours <= 12) {
218+
return newHours + 12
219+
}
220+
return newHours
221+
}

0 commit comments

Comments
 (0)