Skip to content

Commit

Permalink
feat(app): implement a common datetime picker for the user input (#73)
Browse files Browse the repository at this point in the history
* feat(app): add react-native-modal-datetime-picker component package

* feat(app): implement date-time input using button as a base
  • Loading branch information
SeanCassiere committed Jun 5, 2023
1 parent cc29114 commit 3d1c763
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
2 changes: 2 additions & 0 deletions apps/expo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@expo/vector-icons": "^13.0.0",
"@expo/webpack-config": "^18.0.1",
"@hookform/resolvers": "^3.0.0",
"@react-native-community/datetimepicker": "6.7.3",
"@react-native-community/netinfo": "9.3.7",
"@react-native-masked-view/masked-view": "0.2.8",
"@react-navigation/drawer": "^6.6.2",
Expand All @@ -44,6 +45,7 @@
"react-hook-form": "^7.43.9",
"react-native": "0.71.4",
"react-native-gesture-handler": "~2.9.0",
"react-native-modal-datetime-picker": "^15.0.1",
"react-native-numeric-input": "^1.9.1",
"react-native-reanimated": "~2.14.4",
"react-native-safe-area-context": "4.5.0",
Expand Down
71 changes: 71 additions & 0 deletions apps/expo/src/components/DateTimeInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { useState } from "react";
import { Text, View } from "react-native";
import DateTimePickerModal, {
type ReactNativeModalDateTimePickerProps,
} from "react-native-modal-datetime-picker";
import { Button, type IButtonProps } from "native-base";
import { Controller, type Control } from "react-hook-form";

import { DateFormatter } from "../utils/dates";

const DateTimeInput = (props: {
control: Control<any>;
name: string;
label: string;
dateTimePickerProps?: ReactNativeModalDateTimePickerProps;
buttonProps?: IButtonProps;
}) => {
const [isDatePickerVisible, setDatePickerVisibility] = useState(false);

const { control, name, label, buttonProps, dateTimePickerProps } = props;

return (
<Controller
control={control}
name={name}
render={({
field: { onChange, onBlur, value },
fieldState: { error },
}) => (
<View>
<Text style={{ paddingBottom: 6, fontSize: 15, fontWeight: "400" }}>
{label}
</Text>
<Button
{...buttonProps}
variant="outline"
size="md"
justifyContent="flex-start"
_text={{
...buttonProps?._text,
color: "black",
}}
onPress={() => {
setDatePickerVisibility(true);
}}
>
{value instanceof Date
? DateFormatter.dateTimePickerView(value)
: "no-value-yet"}
</Button>
{error && <Text style={{ color: "red" }}>{error.message}</Text>}
<DateTimePickerModal
{...dateTimePickerProps}
isVisible={isDatePickerVisible}
mode="datetime"
date={value instanceof Date ? value : undefined}
onConfirm={(date) => {
setDatePickerVisibility(false);
onChange({ target: { value: date, name } });
}}
onCancel={() => {
setDatePickerVisibility(false);
}}
/>
</View>
)}
/>
);
};

export default DateTimeInput;
3 changes: 3 additions & 0 deletions apps/expo/src/utils/dates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ export const DateFormatter = {
rentalListView: (date: Date) => {
return format(date, "dd/MM/yyyy HH:mm a");
},
dateTimePickerView: (date: Date) => {
return format(date, "dd/MM/yyyy HH:mm a");
},
};
23 changes: 23 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3d1c763

Please sign in to comment.