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

Added date-field reusable component; Migrated old DateFormFields to new UI components #10049

Merged
Show file tree
Hide file tree
Changes from 12 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
2 changes: 2 additions & 0 deletions public/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,7 @@
"prn_reason": "PRN Reason",
"procedure_suggestions": "Procedure Suggestions",
"procedures_select_placeholder": "Select procedures to add details",
"proceed": "Proceed",
"professional_info": "Professional Information",
"professional_info_note": "View or update user's professional information",
"professional_info_note_self": "View or update your professional information",
Expand Down Expand Up @@ -2011,6 +2012,7 @@
"start_time": "Start Time",
"start_time_must_be_before_end_time": "Start time must be before end time",
"state": "State",
"state_reason_for_archiving": "State reason for archiving <strong>{{name}}</strong> file?",
"status": "Status",
"stop": "Stop",
"stop_recording": "Stop Recording",
Expand Down
59 changes: 0 additions & 59 deletions src/components/Form/FormFields/DateFormField.tsx

This file was deleted.

99 changes: 10 additions & 89 deletions src/components/Patient/PatientRegistration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import SectionNavigator from "@/CAREUI/misc/SectionNavigator";
import Autocomplete from "@/components/ui/autocomplete";
import { Button } from "@/components/ui/button";
import { Checkbox } from "@/components/ui/checkbox";
import DateField from "@/components/ui/date-field";
import {
Form,
FormControl,
Expand Down Expand Up @@ -513,95 +514,15 @@ export default function PatientRegistration(
render={({ field }) => (
<FormItem>
<FormControl>
<div className="flex items-center gap-2">
<div className="flex flex-col gap-1">
<FormLabel required>{t("day")}</FormLabel>

<Input
type="number"
placeholder="DD"
{...field}
min={1}
max={31}
value={
form.watch("date_of_birth")?.split("-")[2]
}
onChange={(e) => {
form.setValue(
"date_of_birth",
`${form.watch("date_of_birth")?.split("-")[0]}-${form.watch("date_of_birth")?.split("-")[1]}-${e.target.value}`,
);
const day = parseInt(e.target.value);
if (
e.target.value.length === 2 &&
day >= 1 &&
day <= 31
) {
document
.getElementById("dob-month-input")
?.focus();
}
}}
data-cy="dob-day-input"
/>
</div>

<div className="flex flex-col gap-1">
<FormLabel required>{t("month")}</FormLabel>

<Input
type="number"
id="dob-month-input"
placeholder="MM"
{...field}
value={
form.watch("date_of_birth")?.split("-")[1]
}
min={1}
max={12}
onChange={(e) => {
form.setValue(
"date_of_birth",
`${form.watch("date_of_birth")?.split("-")[0]}-${e.target.value}-${form.watch("date_of_birth")?.split("-")[2]}`,
);
const month = parseInt(e.target.value);
if (
e.target.value.length === 2 &&
month >= 1 &&
month <= 12
) {
document
.getElementById("dob-year-input")
?.focus();
}
}}
data-cy="dob-month-input"
/>
</div>

<div className="flex flex-col gap-1">
<FormLabel required>{t("year")}</FormLabel>

<Input
type="number"
id="dob-year-input"
placeholder="YYYY"
{...field}
value={
form.watch("date_of_birth")?.split("-")[0]
}
min={1900}
max={new Date().getFullYear()}
onChange={(e) =>
form.setValue(
"date_of_birth",
`${e.target.value}-${form.watch("date_of_birth")?.split("-")[1]}-${form.watch("date_of_birth")?.split("-")[2]}`,
)
}
data-cy="dob-year-input"
/>
</div>
</div>
<DateField
date={
field.value ? new Date(field.value) : undefined
}
onChange={(date) =>
field.onChange(date?.toISOString())
}
id="dob"
/>
</FormControl>
<FormMessage />
</FormItem>
Expand Down
143 changes: 143 additions & 0 deletions src/components/ui/date-field.tsx
rithviknishad marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { useState } from "react";
import { useTranslation } from "react-i18next";

import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";

import dayjs from "@/Utils/dayjs";

interface DateFieldProps {
date?: Date;
onChange?: (date?: Date) => void;
disabled?: boolean;
id: string;
}

export default function DateField({
date,
onChange,
disabled,
id,
}: DateFieldProps) {
const { t } = useTranslation();

const [day, setDay] = useState(
date ? date.getDate().toString().padStart(2, "0") : "",
);
const [month, setMonth] = useState(
date ? (date.getMonth() + 1).toString().padStart(2, "0") : "",
);
const [year, setYear] = useState(date ? date.getFullYear().toString() : "");
rithviknishad marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens when the parent component changes the date value? this wouldn't get updated right?


const isValidDate = (year: string, month: string, day: string): boolean => {
const parsedDate = dayjs(`${year}-${month}-${day}`, "YYYY-MM-DD", true);
return parsedDate.isValid();
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we keep this function outside the component?


const handleDayChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const newDay = e.target.value;
setDay(newDay);

if (
newDay.length === 2 &&
parseInt(newDay) >= 1 &&
parseInt(newDay) <= 31
) {
if (isValidDate(year, month, newDay) && onChange) {
const updatedDate = new Date(
parseInt(year),
parseInt(month) - 1,
parseInt(newDay),
);
onChange(updatedDate);
}
document.getElementById(`${id}-month-input`)?.focus();
}
};

const handleMonthChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const newMonth = e.target.value;
setMonth(newMonth);

if (
newMonth.length === 2 &&
parseInt(newMonth) >= 1 &&
parseInt(newMonth) <= 12
) {
if (isValidDate(year, newMonth, day) && onChange) {
const updatedDate = new Date(
parseInt(year),
parseInt(newMonth) - 1,
parseInt(day),
);
onChange(updatedDate);
}

document.getElementById(`${id}-year-input`)?.focus();
}
};

const handleYearChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const newYear = e.target.value;
setYear(newYear);

if (newYear.length === 4 && parseInt(newYear) >= 1900) {
if (isValidDate(newYear, month, day) && onChange) {
const updatedDate = new Date(
parseInt(newYear),
parseInt(month) - 1,
parseInt(day),
);
onChange(updatedDate);
}
}
};

return (
<div className="flex items-center gap-2">
<div className="flex flex-col gap-1">
<Label>{t("day")}</Label>
<Input
type="number"
placeholder="DD"
value={day}
onChange={handleDayChange}
min={1}
max={31}
id={`${id}-day-input`}
className="w-[10rem]"
disabled={disabled}
/>
</div>

<div className="flex flex-col gap-1">
<Label>{t("month")}</Label>
<Input
type="number"
placeholder="MM"
value={month}
onChange={handleMonthChange}
min={1}
max={12}
id={`${id}-month-input`}
className="w-[10rem]"
disabled={disabled}
/>
</div>

<div className="flex flex-col gap-1">
<Label>{t("year")}</Label>
<Input
type="number"
placeholder="YYYY"
value={year}
onChange={handleYearChange}
min={1900}
id={`${id}-year-input`}
className="w-[10rem]"
disabled={disabled}
/>
</div>
</div>
);
}
4 changes: 3 additions & 1 deletion src/components/ui/date-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import {
interface DatePickerProps {
date?: Date;
onChange?: (date?: Date) => void;
disabled?: (date: Date) => boolean;
}

export function DatePicker({ date, onChange }: DatePickerProps) {
export function DatePicker({ date, onChange, disabled }: DatePickerProps) {
rithviknishad marked this conversation as resolved.
Show resolved Hide resolved
const [open, setOpen] = useState(false);

return (
Expand All @@ -44,6 +45,7 @@ export function DatePicker({ date, onChange }: DatePickerProps) {
setOpen(false);
}}
initialFocus
disabled={disabled}
/>
</PopoverContent>
</Popover>
Expand Down
Loading
Loading