Skip to content

Commit

Permalink
fix(date-field): handle undefined and falsy values (refinedev#6217)
Browse files Browse the repository at this point in the history
Co-authored-by: Ali Emir Şen <senaliemir@gmail.com>
  • Loading branch information
webscriptmaster and aliemir committed Aug 1, 2024
1 parent 77f2ee8 commit aefd093
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 12 deletions.
12 changes: 12 additions & 0 deletions .changeset/silly-lions-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"@refinedev/chakra-ui": patch
"@refinedev/mantine": patch
"@refinedev/antd": patch
"@refinedev/mui": patch
---

fix(date-field): falsy values should render empty string

Previously, `<DateField value={undefined} />` was rendering the current date. After this change, it will render empty string if a falsy value is provided.

[Resolves #6216](https://github.com/refinedev/refine/issues/6216)
8 changes: 5 additions & 3 deletions packages/antd/src/components/fields/date/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ export const DateField: React.FC<DateFieldProps> = ({

return (
<Typography.Text {...rest}>
{dayjs(value)
.locale(locales || defaultLocale)
.format(dateFormat)}
{value
? dayjs(value)
.locale(locales || defaultLocale)
.format(dateFormat)
: ""}
</Typography.Text>
);
};
8 changes: 5 additions & 3 deletions packages/chakra-ui/src/components/fields/date/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ export const DateField: React.FC<DateFieldProps> = ({
}) => {
return (
<Text {...rest}>
{dayjs(value)
.locale(locales || defaultLocale)
.format(dateFormat)}
{value
? dayjs(value)
.locale(locales || defaultLocale)
.format(dateFormat)
: ""}
</Text>
);
};
8 changes: 5 additions & 3 deletions packages/mantine/src/components/fields/date/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ export const DateField: React.FC<DateFieldProps> = ({
}) => {
return (
<Text {...rest}>
{dayjs(value)
.locale(locales || defaultLocale)
.format(dateFormat)}
{value
? dayjs(value)
.locale(locales || defaultLocale)
.format(dateFormat)
: ""}
</Text>
);
};
8 changes: 5 additions & 3 deletions packages/mui/src/components/fields/date/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ export const DateField: React.FC<DateFieldProps> = ({
}) => {
return (
<Typography variant="body2" {...rest}>
{dayjs(value)
.locale(locales || defaultLocale)
.format(dateFormat)}
{value
? dayjs(value)
.locale(locales || defaultLocale)
.format(dateFormat)
: ""}
</Typography>
);
};
16 changes: 16 additions & 0 deletions packages/ui-tests/src/tests/fields/date.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ export const fieldDateTests = (
getByText("5/20/2021");
});

it("renders empty with given null", () => {
const { getByTestId } = render(
<DateField value={null} data-testid="date-field" />,
);

expect(getByTestId("date-field").textContent).toBe("");
});

it("renders empty with given undefined", () => {
const { getByTestId } = render(
<DateField value={undefined} data-testid="date-field" />,
);

expect(getByTestId("date-field").textContent).toBe("");
});

it("renders invalid date with given incorrect date", () => {
const { getByText } = render(<DateField value={new Date("test")} />);

Expand Down

0 comments on commit aefd093

Please sign in to comment.