refinedev/refine

[BUG] `<DateField value={undefined} />` is still displaying the date

aliemir opened this issue · 6 comments

Describe the bug

Under the hood, <DateField /> components of Refine's UI implementations use dayjs. As an expected behavior of dayjs, when you construct a Dayjs object with an empty/undefined value it will use the current date time. This causes <DateField value={undefined} /> to display current date instead of rendering nothing.

import { useShow } from "@refinedev/core";
import { DateField } from "@refinedev/antd";
import { Typography } from "antd";

export const PostDetails = () => {
  const { query } = useShow();

  return (
    <Typography.Title level={5}>Title</Typography.Title>
    <Typography.Text>{data.data?.title}</Typography.Text>
    <Typography.Title level={5}>Created At</Typography.Title>
    {/* Assume `createdAt` is not present in the response */}
    <DateField value={data.data?.createdAt} />
  );
}

This will render the current date.

import dayjs from "dayjs";

console.log(dayjs().format("DD-MM-YYYY")); // 31-07-2024

console.log(dayjs(Date.now()).format("DD-MM-YYYY")); // 31-07-2024

Steps To Reproduce

  1. Import DateField from any UI implementation of Refine.
  2. Pass undefined or a reference to a non-existing property in an object to the value prop.
  3. Check to see <DateField value={undefined} /> rendering the current date.

Expected behavior

<DateField value={undefined} /> should return null and display nothing.

Implementations should be updated to check for undefined values and return null in that case.

Packages

  • @refinedev/antd
  • @refinedev/mui
  • @refinedev/chakra-ui
  • @refinedev/mantine

Additional Context

Tests for this fix can be implemented in @refinedev/ui-tests and it will run on all implementations.

i would like to work on this!

Hey @JayBhensdadia, assigning this to you. Let us know if you have any issues. Don't forget to check our Contributing Guide before you start.

Hi @aliemir
I've just fixed this issue.
Can you possibly check following PR?
#6217

Hey @JayBhensdadia, @webscriptmaster put up a PR with a valid fix for this issue. We're linking this issue with the PR and hoping to include it in our next release.

Maybe you can check our issue list to find yourself one to work on. We'll be happy to see your contribution 🙏

Hi @JayBhensdadia @aliemir Thanks you very much.
I will try to contribute more often.

is this issue is still open