Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTimePicker #96

Merged
merged 6 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@freakycoder/react-native-header-view": "^1.2.0",
"@gorhom/bottom-sheet": "^4.4.3",
"@react-native-async-storage/async-storage": "^1.17.10",
"@react-native-community/datetimepicker": "6.2.0",
"@react-native-masked-view/masked-view": "0.2.7",
"@react-navigation/native": "^6.0.13",
"@react-navigation/stack": "^6.3.2",
Expand All @@ -41,6 +42,7 @@
"react-native-app-intro-slider": "^4.0.4",
"react-native-gesture-handler": "~2.5.0",
"react-native-imaged-card-view": "^0.0.11",
"react-native-modal-datetime-picker": "^14.0.1",
"react-native-maps": "0.31.1",
"react-native-reanimated": "~2.9.1",
"react-native-safe-area-context": "4.3.1",
Expand Down Expand Up @@ -75,4 +77,4 @@
"@react-navigation/core": "~6.2.2"
},
"private": true
}
}
39 changes: 39 additions & 0 deletions src/components/FreeClass/DateTimePicker/DateTimeBox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React, { FC } from "react"
import { View } from "react-native"
import { Text } from "components/Text"
import { usePalette } from "utils/colors"

export interface DateTimeBoxProps {
value?: string
}
/**
* custom box which contains a 2 digit number, used
* inside {@link DateTimePicker}
*/
export const DateTimeBox: FC<DateTimeBoxProps> = props => {
const { isLight } = usePalette()
return (
<View
style={{
width: 38,
height: 27,
borderColor: isLight ? "#454773" : "#fff",
borderWidth: 0.5,
borderRadius: 5,
justifyContent: "center",
alignItems: "center",
}}
>
<Text
style={{
fontSize: 20,
fontWeight: "400",
color: isLight ? "#454773" : "#fff",
opacity: 0.4,
}}
>
{props.value}
</Text>
</View>
)
}
137 changes: 137 additions & 0 deletions src/components/FreeClass/DateTimePicker/DateTimePicker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import React, { FC, useState } from "react"
import { Pressable, View } from "react-native"
import { BodyText } from "components/Text"
import { usePalette } from "utils/colors"
import { DateTimeBox } from "./DateTimeBox"
import { destructureDate } from "utils/dates"
import DateTimePickerModal from "react-native-modal-datetime-picker"
import { StyleSheet } from "react-native"
export interface DateTimePickerProps {
date: Date
setDate: (date: Date) => void
}

/**
* custom DateTime picker
*/
export const DateTimePicker: FC<DateTimePickerProps> = props => {
const { year, month, day, hour, minute } = destructureDate(props.date)
const { isLight, primary } = usePalette()

const [isDatePickerVisible, setDatePickerVisibility] = useState(false)

//there would be also "date-time" but we don't use it
const [dateMode, setDateMode] = useState<"date" | "time">("date")

const showDatePicker = () => {
setDateMode("date")
setDatePickerVisibility(true)
}

const showTimePicker = () => {
setDateMode("time")
setDatePickerVisibility(true)
}

const hideDateOrTimePicker = () => {
setDatePickerVisibility(false)
}

const handleConfirm = (date: Date) => {
hideDateOrTimePicker()
props.setDate(date)
}
return (
<View>
<View
style={{
marginTop: 46,
width: "100%",
height: 27,
flexDirection: "row",
justifyContent: "center",
alignItems: "center",
}}
>
<View>
<BodyText
style={{
color: isLight ? primary : "#fff",
fontSize: 14,
marginRight: 12,
}}
>
Data
</BodyText>
</View>
<Pressable
onPress={showDatePicker}
style={{ flexDirection: "row" }}
>
<DateTimeBox value={day} />
toto04 marked this conversation as resolved.
Show resolved Hide resolved
<BodyText
style={[
{ color: isLight ? primary : "#fff" },
styles.dot,
]}
>
.
</BodyText>
<DateTimeBox value={month} />
<BodyText
style={[
{ color: isLight ? primary : "#fff" },
styles.dot,
]}
>
.
</BodyText>
<DateTimeBox value={year} />
</Pressable>
<View>
<BodyText
style={{
color: isLight ? primary : "#fff",
fontSize: 14,
marginLeft: 28,
marginRight: 12,
}}
>
Ora
</BodyText>
</View>
<Pressable
onPress={showTimePicker}
style={{ flexDirection: "row" }}
>
<DateTimeBox value={hour} />
<BodyText
style={[
{ color: isLight ? primary : "#fff" },
styles.colon,
]}
>
:
</BodyText>
<DateTimeBox value={minute} />
</Pressable>
</View>
<DateTimePickerModal
isVisible={isDatePickerVisible}
mode={dateMode}
onConfirm={handleConfirm}
onCancel={hideDateOrTimePicker}
toto04 marked this conversation as resolved.
Show resolved Hide resolved
date={props.date}
/>
</View>
)
}

const styles = StyleSheet.create({
dot: {
alignSelf: "flex-end",
marginHorizontal: 4,
fontSize: 14,
},
colon: { alignSelf: "center", marginHorizontal: 4, fontSize: 14 },
})
21 changes: 9 additions & 12 deletions src/pages/FreeClass/CampusChoice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { View, Pressable, FlatList } from "react-native"
import { usePalette } from "utils/colors"
import { NavBar } from "components/NavBar"
import { Title, BodyText } from "components/Text"
import { DateTimePicker } from "components/FreeClass/DateTimePicker/DateTimePicker"

export interface CampusItem {
id: number
Expand All @@ -22,6 +23,10 @@ export const CampusChoice: RootStackScreen<"CampusChoice"> = () => {
{ id: 4, name: ["Mancinelli"] },
])

//non-ISO format for simplicity (local timezone) and
// compatibility with `handleConfirm` function
const [date, setDate] = useState<Date>(new Date())

return (
<View
style={{
Expand Down Expand Up @@ -64,18 +69,10 @@ export const CampusChoice: RootStackScreen<"CampusChoice"> = () => {
>
<Title style={{ fontSize: 40 }}>Campus</Title>
</View>
<View
//this view is for the date and the time
style={{
marginTop: 46,
backgroundColor: "red",
width: 260,
height: 27,
alignSelf: "center",
}}
>
<BodyText>Data Picker</BodyText>
</View>
<DateTimePicker
date={date}
setDate={(date: Date) => setDate(date)}
/>
<View
style={{
paddingBottom: 35,
Expand Down
13 changes: 13 additions & 0 deletions src/utils/dates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,16 @@ export function getIsoStringFromDaysPassed(n: number): string {

return newDate.toISOString()
}

/**
* given a `Date` object (NON-ISO), returns the destructured Date
* ready for use of {@link DateTimePicker}
*/
export function destructureDate(date: Date) {
const year = date.getFullYear().toString().substring(2, 4) //only last two digits
const month = (date.getMonth() + 1).toString().padStart(2, "0")
const day = date.getDate().toString().padStart(2, "0")
const hour = date.getHours().toString().padStart(2, "0")
const minute = date.getMinutes().toString().padStart(2, "0")
return { year, month, day, hour, minute }
}
14 changes: 14 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2013,6 +2013,13 @@
prompts "^2.4.0"
semver "^6.3.0"

"@react-native-community/datetimepicker@6.2.0":
version "6.2.0"
resolved "https://registry.yarnpkg.com/@react-native-community/datetimepicker/-/datetimepicker-6.2.0.tgz#50bc629e70e4030c48205c01efbc65a8f35d82d4"
integrity sha512-w1ZS+wYO3qSASliRp+B7mPphOhtVm++rhSbj1WsgXdgLgDXSkDxpMnAXXQu9M0XdcgBwVJ6wDExeIwuzU5Jtfg==
dependencies:
invariant "^2.2.4"

"@react-native-masked-view/masked-view@0.2.7":
version "0.2.7"
resolved "https://registry.yarnpkg.com/@react-native-masked-view/masked-view/-/masked-view-0.2.7.tgz#d19f1fc646df9440c23c834b4ff3f2b8fe15e175"
Expand Down Expand Up @@ -9496,6 +9503,13 @@ react-native-imaged-card-view@^0.0.11:
resolved "https://registry.yarnpkg.com/react-native-imaged-card-view/-/react-native-imaged-card-view-0.0.11.tgz#c1912ab00c4e07ab2c0a56801f930374f484cc20"
integrity sha512-xVvI0mWP1SR+J61P5X649eDZXunCXtSMZ+GkGsY03vEPofnfah8q3Am2NDX9KcO6SzdNtxRJeLmDogyfj63ReQ==

react-native-modal-datetime-picker@^14.0.1:
version "14.0.1"
resolved "https://registry.yarnpkg.com/react-native-modal-datetime-picker/-/react-native-modal-datetime-picker-14.0.1.tgz#d9c6df4ff85bf1cfbe108c756dc26dcca4cc5f2f"
integrity sha512-wQt4Pjxt2jiTsVhLMG0E7WrRTYBEQx2d/nUrFVCbRqJ7lrXocXaT5UZsyMpV93TnKcyut62OprbO88wYq/vh0g==
dependencies:
prop-types "^15.7.2"

react-native-maps@0.31.1:
version "0.31.1"
resolved "https://registry.yarnpkg.com/react-native-maps/-/react-native-maps-0.31.1.tgz#574be035a52287e3ab1c1da35e2ce3e2116f0fd6"
Expand Down