Skip to content

Commit

Permalink
refactor(mobile): add generic date picker field (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkdev98 authored Jul 19, 2024
1 parent ea1f395 commit e4f2af0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import DateTimePicker from '@react-native-community/datetimepicker'
import * as Haptics from 'expo-haptics'
import { Calendar } from 'lucide-react-native'
import { useRef, useState } from 'react'
import { useController } from 'react-hook-form'
import { Keyboard, View } from 'react-native'
import { useSafeAreaInsets } from 'react-native-safe-area-context'
import { FullWindowOverlay } from 'react-native-screens'
Expand All @@ -23,9 +22,13 @@ import { Text } from '../ui/text'
function SpinnerDatePicker({
value,
onChange,
maximumDate,
minimumDate,
}: {
value: Date
onChange: (date: Date | undefined) => void
maximumDate?: Date
minimumDate?: Date
}) {
const { i18n } = useLingui()
const [date, setDate] = useState<Date | undefined>(value)
Expand All @@ -39,6 +42,8 @@ function SpinnerDatePicker({
onChange={(_, selectedDate) => {
setDate(selectedDate)
}}
maximumDate={maximumDate}
minimumDate={minimumDate}
/>
<Button
className="mx-6"
Expand All @@ -53,17 +58,20 @@ function SpinnerDatePicker({
)
}

export function SelectDateField({
onSelect,
export function DatePicker({
value = new Date(),
onChange,
maximumDate,
minimumDate,
}: {
onSelect?: (date?: Date) => void
value?: Date
onChange?: (date?: Date) => void
maximumDate?: Date
minimumDate?: Date
}) {
const { bottom } = useSafeAreaInsets()
const { colorScheme } = useColorScheme()
const sheetRef = useRef<BottomSheetModal>(null)
const {
field: { onChange, onBlur, value },
} = useController({ name: 'date' })

return (
<>
Expand Down Expand Up @@ -108,10 +116,10 @@ export function SelectDateField({
onChange={async (date) => {
sheetRef.current?.close()
await sleep(500)
onChange(date)
onBlur()
onSelect?.(date)
onChange?.(date)
}}
maximumDate={maximumDate}
minimumDate={minimumDate}
/>
</BottomSheetView>
</BottomSheetModal>
Expand Down
15 changes: 13 additions & 2 deletions apps/mobile/components/transaction/transaction-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Animated, {
useAnimatedKeyboard,
useAnimatedStyle,
} from 'react-native-reanimated'
import { DatePicker } from '../common/date-picker'
import { InputField } from '../form-fields/input-field'
import { SubmitButton } from '../form-fields/submit-button'
import { NumericPad } from '../numeric-pad'
Expand All @@ -22,7 +23,6 @@ import { Button } from '../ui/button'
import { Text } from '../ui/text'
import { SelectAccountField } from './select-account-field'
import { SelectCategoryField } from './select-category-field'
import { SelectDateField } from './select-date-field'

type TransactionFormProps = {
onSubmit: (data: TransactionFormValues) => void
Expand Down Expand Up @@ -93,7 +93,18 @@ export const TransactionForm = ({
<Button size="icon" variant="secondary" onPress={onCancel}>
<XIcon className="size-6 text-primary" />
</Button>
<SelectDateField />
<Controller
name="date"
control={form.control}
render={({ field: { onChange, value } }) => (
<DatePicker
value={value}
onChange={onChange}
minimumDate={new Date(Date.now() - 365 * 24 * 60 * 60 * 1000)}
maximumDate={new Date(Date.now() + 365 * 24 * 60 * 60 * 1000)}
/>
)}
/>
<View className="flex-row items-center gap-4">
{onDelete ? (
<Button size="icon" variant="secondary" onPress={onDelete}>
Expand Down

0 comments on commit e4f2af0

Please sign in to comment.