Skip to content

Commit

Permalink
fix(slash): fix format input date
Browse files Browse the repository at this point in the history
  • Loading branch information
pplancq authored and MartinWeb committed Jan 29, 2025
1 parent 96f5b30 commit 663343f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
26 changes: 6 additions & 20 deletions slash/react/src/Form/Date/Date.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import "@axa-fr/design-system-slash-css/dist/Form/Date/Date.scss";
import { ComponentPropsWithRef, forwardRef, useMemo } from "react";
import { ComponentPropsWithRef, forwardRef } from "react";
import { getComponentClassName } from "../../utilities";
import { formatDateInputValue } from "../../utilities/helpers/date";

type Props = Omit<ComponentPropsWithRef<"input">, "value"> & {
classModifier?: string;
defaultValue?: Date;
value?: Date;
};

const formatDateValue = (dateValue: Date) => {
const formattedDateValue = new globalThis.Date(dateValue);
const monthFormatted = `0${formattedDateValue.getMonth() + 1}`.slice(-2);
const dayFormatted = `0${formattedDateValue.getDate()}`.slice(-2);
return `${formattedDateValue.getFullYear()}-${monthFormatted}-${dayFormatted}`;
defaultValue?: Date | string;
value?: Date | string;
};

const Date = forwardRef<HTMLInputElement, Props>(
Expand All @@ -23,20 +17,12 @@ const Date = forwardRef<HTMLInputElement, Props>(
"af-form__input-date",
);

const currentValue = useMemo(() => {
return value ? formatDateValue(value) : undefined;
}, [value]);

const currentDefaultValue = useMemo(() => {
return defaultValue ? formatDateValue(defaultValue) : undefined;
}, [defaultValue]);

return (
<input
className={componentClassName}
type="date"
defaultValue={currentDefaultValue}
value={currentValue}
defaultValue={formatDateInputValue(defaultValue)}
value={formatDateInputValue(value)}
ref={ref}
disabled={classModifier?.includes("disabled")}
required={classModifier?.includes("required")}
Expand Down
22 changes: 22 additions & 0 deletions slash/react/src/utilities/helpers/__tests__/date.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { formatDateInputValue } from "../date";

describe("formatDateInputValue", () => {
it.each([
["2023-10-05T00:00:00Z", "2023-10-05"],
["2023-10-05", "2023-10-05"],
["0002-10-05", "0002-10-05"],
])("should format Date('%s') object to string '%s'", (date, expected) => {
const formattedDate = formatDateInputValue(new Date(date));
expect(formattedDate).toBe(expected);
});

it("should return the string value as is", () => {
const formattedDate = formatDateInputValue("2023-10-05");
expect(formattedDate).toStrictEqual("2023-10-05");
});

it("should return undefined if no value is provided", () => {
const formattedDate = formatDateInputValue();
expect(formattedDate).toBeUndefined();
});
});
6 changes: 6 additions & 0 deletions slash/react/src/utilities/helpers/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const MAXIMUM_SIZE_DATE = 10;

export const formatDateInputValue = (value?: Date | string) =>
value instanceof Date
? value.toISOString().slice(0, MAXIMUM_SIZE_DATE)
: value;

0 comments on commit 663343f

Please sign in to comment.