Skip to content

Commit

Permalink
Rollback DateField to Date type
Browse files Browse the repository at this point in the history
  • Loading branch information
ljmotta committed Sep 19, 2024
1 parent 9cb4f22 commit 6461640
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
12 changes: 6 additions & 6 deletions packages/uniforms-patternfly/src/DateField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ import { TextInput, TextInputProps } from "@patternfly/react-core/dist/js/compon
import wrapField from "./wrapField";

export type DateFieldProps = FieldProps<
string,
Date,
TextInputProps,
{
inputRef?: React.RefObject<HTMLInputElement>;
labelProps?: object;
max?: string;
min?: string;
max?: Date;
min?: Date;
type?: "date" | "datetime-local";
}
>;
Expand All @@ -54,16 +54,16 @@ const dateParse = (value: string, onChange: DateFieldProps["onChange"]) => {
if (splitedValue.length > 1) {
// A year can't be bigger than 9999;
splitedValue[0] = parseInt(splitedValue[0]) > 9999 ? "9999" : splitedValue[0];
onChange(new DateConstructor(`${splitedValue.join("-")}Z`).toISOString());
onChange(new DateConstructor(`${splitedValue.join("-")}Z`));
return;
}
onChange(undefined);
} else {
const date = new DateConstructor(`${value}Z`);
if (date.getFullYear() < 10000) {
onChange(date.toISOString());
onChange(date);
} else {
onChange(date.toISOString());
onChange(date);
}
}
};
Expand Down
48 changes: 30 additions & 18 deletions packages/uniforms-patternfly/tests/DateField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ test("<DateField> - renders a input which correctly reacts on change", () => {
const input = screen.getByTestId("date-field") as HTMLInputElement;
fireEvent.change(input, { target: { value: "2000-04-04T10:20" } });

expect(onChange).toHaveBeenLastCalledWith("x", "2000-04-04T10:20:00.000Z");
expect(onChange).toHaveBeenLastCalledWith("x", new Date("2000-04-04T10:20:00.000Z"));
});

test("<DateField> - renders a input which correctly reacts on change (empty value)", () => {
const onChange = jest.fn();

render(
usingUniformsContext(
<DateField name="x" value={"2000-04-04T00:00:00.000Z"} />,
<DateField name="x" value={new Date("2000-04-04T00:00:00.000Z")} />,
{ x: { type: "string" } },
{ onChange }
)
Expand Down Expand Up @@ -145,16 +145,16 @@ test("<DateField> - renders a input which correctly reacts on change (valid)", (

render(
usingUniformsContext(
<DateField name="x" value={`2000-04-04T00:00:00.000Z`} />,
<DateField name="x" value={new Date("2000-04-04T00:00:00.000Z")} />,
{ x: { type: "string" } },
{ onChange }
)
);

const input = screen.getByTestId("date-field") as HTMLInputElement;
fireEvent.change(input, { target: { value: `2000-04-04T10:30` } });
fireEvent.change(input, { target: { value: "2000-04-04T10:30" } });

expect(onChange).toHaveBeenLastCalledWith("x", `2000-04-04T10:30:00.000Z`);
expect(onChange).toHaveBeenLastCalledWith("x", new Date("2000-04-04T10:30:00.000Z"));
});

test("<DateField> - renders a input which correctly reacts on change (year bigger than 9999)", () => {
Expand All @@ -165,44 +165,56 @@ test("<DateField> - renders a input which correctly reacts on change (year bigge
const input = screen.getByTestId("date-field") as HTMLInputElement;
fireEvent.change(input, { target: { value: "121212-12-12T12:12" } });

expect(onChange).toHaveBeenLastCalledWith("x", "9999-12-12T12:12:00.000Z");
expect(onChange).toHaveBeenLastCalledWith("x", new Date("9999-12-12T12:12:00.000Z"));
});

test("<DateField> - test max property - valid", () => {
render(
usingUniformsContext(<DateField name="x" max={"1999-01-01T00:00:00Z"} value={"1998-12-31T00:00:00Z"} />, {
x: { type: "string" },
})
usingUniformsContext(
<DateField name="x" max={new Date("1999-01-01T00:00:00Z")} value={new Date("1998-12-31T00:00:00Z")} />,
{
x: { type: "string" },
}
)
);

expect(screen.queryByTestId("Should be before")).toBeNull();
});

test("<DateField> - test max property - invalid", () => {
render(
usingUniformsContext(<DateField name="x" max={"1999-01-01T00:00:00.000Z"} value={"1999-01-02T00:00:00.000Z"} />, {
x: { type: "string" },
})
usingUniformsContext(
<DateField name="x" max={new Date("1999-01-01T00:00:00.000Z")} value={new Date("1999-01-02T00:00:00.000Z")} />,
{
x: { type: "string" },
}
)
);

expect(screen.getByText(`Should be before 1999-01-01T00:00:00.000Z`)).toBeInTheDocument();
});

test("<DateField> - test min property - valid", () => {
render(
usingUniformsContext(<DateField name="x" min={"1999-01-01T00:00:00Z"} value={"1999-01-02T00:00:00Z"} />, {
x: { type: "string" },
})
usingUniformsContext(
<DateField name="x" min={new Date("1999-01-01T00:00:00Z")} value={new Date("1999-01-02T00:00:00Z")} />,
{
x: { type: "string" },
}
)
);

expect(screen.queryByTestId("Should be after")).toBeNull();
});

test("<DateField> - test min property - invalid", () => {
render(
usingUniformsContext(<DateField name="x" min={"1999-01-01T00:00:00.000Z"} value={"1998-12-31T00:00:00.000Z"} />, {
x: { type: "string" },
})
usingUniformsContext(
<DateField name="x" min={new Date("1999-01-01T00:00:00.000Z")} value={new Date("1998-12-31T00:00:00.000Z")} />,
{
x: { type: "string" },
}
)
);

expect(screen.getByText(`Should be after 1999-01-01T00:00:00.000Z`)).toBeInTheDocument();
Expand Down

0 comments on commit 6461640

Please sign in to comment.