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

enhanced Date Time component #9379

Closed
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
81 changes: 44 additions & 37 deletions src/components/Common/DateInputV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ const DateInputV2: React.FC<Props> = ({
isOpen,
popOverClassName,
}) => {
const is24Hour =
Intl.DateTimeFormat(undefined, { hour: "numeric" }).resolvedOptions()
.hour12 === false;
const dateFormat = `DD/MM/YYYY${allowTime ? (is24Hour ? " HH:mm" : " hh:mm a") : ""}`;

const [dayCount, setDayCount] = useState<Array<number>>([]);
const [blankDays, setBlankDays] = useState<Array<number>>([]);

Expand All @@ -61,7 +66,7 @@ const DateInputV2: React.FC<Props> = ({

const [popOverOpen, setPopOverOpen] = useState(false);

const hours = dayjs(value).hour() % 12;
const hours = is24Hour ? dayjs(value).hour() : dayjs(value).hour() % 12;
const minutes = dayjs(value).minute();
const ampm = dayjs(value).hour() > 11 ? "PM" : "AM";

Expand Down Expand Up @@ -159,7 +164,10 @@ const DateInputV2: React.FC<Props> = ({
newMinutes?: typeof minutes;
newAmpm?: typeof ampm;
}) => {
const { newHours = hours, newMinutes = minutes, newAmpm = ampm } = options;
const { newHours = hours, newMinutes = minutes } = options;
const { newAmpm = is24Hour ? (newHours > 11 ? "PM" : "AM") : ampm } =
options;

handleChange(
new Date(
datePickerHeaderDate.getFullYear(),
Expand Down Expand Up @@ -336,8 +344,6 @@ const DateInputV2: React.FC<Props> = ({
isOpen && popoverButtonRef.current?.click();
}, [isOpen]);

const dateFormat = `DD/MM/YYYY${allowTime ? " hh:mm a" : ""}`;

const getPosition = () => {
const viewportWidth = document.documentElement.clientWidth;
const viewportHeight = document.documentElement.clientHeight;
Expand Down Expand Up @@ -368,7 +374,7 @@ const DateInputV2: React.FC<Props> = ({
className={`${containerClassName ?? "container mx-auto text-black"}`}
data-cui-dateinput
data-cui-dateinput-value={JSON.stringify(
dayjs(value).format("YYYY-MM-DDTHH:mm"),
dayjs(value).format(dateFormat),
)}
>
<Popover className="relative">
Expand Down Expand Up @@ -704,8 +710,8 @@ const DateInputV2: React.FC<Props> = ({
name: "Hours",
value: hours,
options: Array.from(
{ length: 12 },
(_, i) => i + 1,
{ length: is24Hour ? 24 : 12 },
(_, i) => (is24Hour ? i : i + 1),
),
onChange: (val: any) => {
handleTimeChange({
Expand Down Expand Up @@ -766,36 +772,37 @@ const DateInputV2: React.FC<Props> = ({
}
}}
>
{[
...input.options,
...(input.name === "am/pm"
? []
: input.options),
...(input.name === "am/pm"
? []
: input.options),
].map((option, j) => (
<button
type="button"
key={j}
className={`flex aspect-square w-9 shrink-0 items-center justify-center rounded-md border transition-all ${(input.name === "Hours" && option === 12 ? [0, 12].includes(input.value) : input.value === option) ? "border-primary-600 bg-primary-500 font-bold text-white" : "border-gray-200 hover:bg-secondary-300"} text-sm`}
onClick={() =>
input.onChange(option as any)
}
data-selected={
(input.name === "Hours" && option === 12
? [0, 12].includes(input.value)
: input.value === option) &&
j + 1 >= input.options.length &&
j + 1 <= input.options.length * 2
}
>
{option.toLocaleString("en-US", {
minimumIntegerDigits: 2,
useGrouping: false,
})}
</button>
))}
{(!is24Hour || input.name !== "am/pm") &&
[
...input.options,
...(input.name === "am/pm"
? []
: input.options),
...(input.name === "am/pm"
? []
: input.options),
].map((option, j) => (
<button
type="button"
key={j}
className={`flex aspect-square w-9 shrink-0 items-center justify-center rounded-md border transition-all ${(input.name === "Hours" && option === 12 ? [0, 12].includes(input.value) : input.value === option) ? "border-primary-600 bg-primary-500 font-bold text-white" : "border-gray-200 hover:bg-secondary-300"} text-sm`}
onClick={() =>
input.onChange(option as any)
}
data-selected={
(input.name === "Hours" && option === 12
? [0, 12].includes(input.value)
: input.value === option) &&
j + 1 >= input.options.length &&
j + 1 <= input.options.length * 2
}
Comment on lines +788 to +798
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Simplify complex conditional logic.

The data-selected attribute contains complex nested conditions that are hard to understand and maintain. Consider extracting this logic into a helper function for better readability.

+ const isOptionSelected = (option: number, value: number, name: string) => {
+   if (name === "Hours" && option === 12) {
+     return [0, 12].includes(value);
+   }
+   return value === option;
+ };

- data-selected={
-   (input.name === "Hours" && option === 12
-     ? [0, 12].includes(input.value)
-     : input.value === option) &&
-   j + 1 >= input.options.length &&
-   j + 1 <= input.options.length * 2
- }
+ data-selected={
+   isOptionSelected(option, input.value, input.name) &&
+   j + 1 >= input.options.length &&
+   j + 1 <= input.options.length * 2
+ }

Committable suggestion skipped: line range outside the PR's diff.

>
{option.toLocaleString("en-US", {
minimumIntegerDigits: 2,
useGrouping: false,
})}
</button>
))}
</div>
))}
</div>
Expand Down
31 changes: 13 additions & 18 deletions src/components/Common/DateTextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export default function DateTextInput(props: {
onFinishInitialTyping?: () => unknown;
error?: string;
}) {
const is24Hour =
Intl.DateTimeFormat(undefined, { hour: "numeric" }).resolvedOptions()
.hour12 === false;
const { value, onChange, allowTime, error, onFinishInitialTyping } = props;

const [editingText, setDirtyEditingText] = useState({
Expand Down Expand Up @@ -71,7 +74,7 @@ export default function DateTextInput(props: {
};

const goToInput = (i: number) => {
if (i < 0 || i > 4) return;
if (i < 0 || i > 5) return;
(
document.querySelectorAll(
`[data-time-input]`,
Expand Down Expand Up @@ -116,7 +119,7 @@ export default function DateTextInput(props: {
date: `${value ? formatUnfocused(value.getDate(), 0) : ""}`,
month: `${value ? formatUnfocused(value.getMonth() + 1, 1) : ""}`,
year: `${value ? formatUnfocused(value.getFullYear(), 2, 4) : ""}`,
hour: `${value ? formatUnfocused(value.getHours(), 3) : ""}`,
hour: `${value ? formatUnfocused(is24Hour ? value.getHours() : value.getHours() % 12 || 12, 3) : ""}`,
minute: `${value ? formatUnfocused(value.getMinutes(), 4) : ""}`,
});
}, [value]);
Expand Down Expand Up @@ -155,22 +158,14 @@ export default function DateTextInput(props: {
data-time-input={i}
onChange={(e) => {
const value = e.target.value;
if (
(value.endsWith("/") ||
value.endsWith(" ") ||
value.endsWith(":") ||
value.length > (key === "year" ? 3 : 1)) &&
i < 4
) {
goToInput(i + 1);
} else {
setEditingText({
...editingText,
[key]: value
.replace(/\D/g, "")
.slice(0, key === "year" ? 4 : 2),
});
}
if (value.length > (key === "year" ? 3 : 1)) goToInput(i + 1);

setEditingText({
...editingText,
[key]: value
.replace(/\D/g, "")
.slice(0, key === "year" ? 4 : 2),
});
}}
onBlur={(e) => handleBlur(e.target.value, key)}
/>
Expand Down
Loading