-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mobile): [Transaction] add select date field
- Loading branch information
Quốc Khánh
committed
Jul 10, 2024
1 parent
cd4f517
commit 31a14da
Showing
5 changed files
with
168 additions
and
8 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
apps/mobile/components/transaction/select-date-field.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { formatDateShort } from '@/lib/date' | ||
import { sleep } from '@/lib/utils' | ||
import { | ||
BottomSheetBackdrop, | ||
BottomSheetModal, | ||
BottomSheetView, | ||
} from '@gorhom/bottom-sheet' | ||
import { t } from '@lingui/macro' | ||
import { useLingui } from '@lingui/react' | ||
import DateTimePicker from '@react-native-community/datetimepicker' | ||
import { Calendar } from 'lucide-react-native' | ||
import { useRef, useState } from 'react' | ||
import { useController } from 'react-hook-form' | ||
import { View } from 'react-native' | ||
import { useSafeAreaInsets } from 'react-native-safe-area-context' | ||
import { FullWindowOverlay } from 'react-native-screens' | ||
import { Button } from '../ui/button' | ||
import { Text } from '../ui/text' | ||
|
||
function SpinnerDatePicker({ | ||
value, | ||
onChange, | ||
}: { | ||
value: Date | ||
onChange: (date: Date | undefined) => void | ||
}) { | ||
const { i18n } = useLingui() | ||
const [date, setDate] = useState<Date | undefined>(value) | ||
|
||
return ( | ||
<View className="gap-4"> | ||
<DateTimePicker | ||
value={value} | ||
mode="date" | ||
display="spinner" | ||
onChange={(_, selectedDate) => { | ||
setDate(selectedDate) | ||
}} | ||
/> | ||
<Button | ||
className="mx-6" | ||
onPress={() => { | ||
onChange(date) | ||
}} | ||
> | ||
<Text>{t(i18n)`Save`}</Text> | ||
</Button> | ||
</View> | ||
) | ||
} | ||
|
||
export function SelectDateField({ | ||
onSelect, | ||
}: { | ||
onSelect?: (date?: Date) => void | ||
}) { | ||
const { bottom } = useSafeAreaInsets() | ||
const sheetRef = useRef<BottomSheetModal>(null) | ||
const { | ||
field: { onChange, onBlur, value }, | ||
} = useController({ name: 'date' }) | ||
|
||
return ( | ||
<> | ||
<Button | ||
variant="outline" | ||
className="!px-3" | ||
onPress={() => { | ||
sheetRef.current?.present() | ||
}} | ||
> | ||
<Calendar className="w-5 h-5 text-primary" /> | ||
<Text>{formatDateShort(value)}</Text> | ||
</Button> | ||
<BottomSheetModal | ||
ref={sheetRef} | ||
index={0} | ||
enableDynamicSizing | ||
enablePanDownToClose | ||
keyboardBehavior="extend" | ||
backdropComponent={(props) => ( | ||
<BottomSheetBackdrop | ||
{...props} | ||
appearsOnIndex={0} | ||
disappearsOnIndex={-1} | ||
enableTouchThrough | ||
/> | ||
)} | ||
containerComponent={(props) => ( | ||
<FullWindowOverlay>{props.children}</FullWindowOverlay> | ||
)} | ||
> | ||
<BottomSheetView | ||
style={{ | ||
paddingBottom: bottom, | ||
}} | ||
> | ||
<SpinnerDatePicker | ||
value={value} | ||
onChange={async (date) => { | ||
sheetRef.current?.close() | ||
await sleep(500) | ||
onChange(date) | ||
onBlur() | ||
onSelect?.(date) | ||
}} | ||
/> | ||
</BottomSheetView> | ||
</BottomSheetModal> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { t } from '@lingui/macro' | ||
import { format } from 'date-fns/format' | ||
import { isSameYear } from 'date-fns/isSameYear' | ||
import { isToday } from 'date-fns/isToday' | ||
import { isTomorrow } from 'date-fns/isTomorrow' | ||
import { isYesterday } from 'date-fns/isYesterday' | ||
|
||
export function formatDateShort(date: Date) { | ||
if (isToday(date)) { | ||
return t`Today` | ||
} | ||
|
||
if (isYesterday(date)) { | ||
return t`Yesterday` | ||
} | ||
|
||
if (isTomorrow(date)) { | ||
return t`Tomorrow` | ||
} | ||
|
||
if (isSameYear(date, new Date())) { | ||
return format(date, 'MMM d') | ||
} | ||
|
||
return format(date, 'P') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.