0

The project dependencies are as follows:

"dependencies": {
    "@mantine/core": "^7.6.2",
    "@mantine/dates": "^7.6.2",
    "@mantine/form": "^7.6.2",
    "@mantine/hooks": "^7.6.2",
    "dayjs": "^1.11.10",
    "next": "14.1.0",
    "react": "^18",
    "react-dom": "^18",
    "uuid": "^9.0.1"
  }

MantineForm.tsx file:

export default function MantineForm() {
    const form = useForm({
        initialValues: {
            visitorName: "",
            firmName: "",
            purposeOfVisit: `${PurposeOfVisit.Subcontractor}`,
            vehicleLicense: "",
            email: "",
            phone: 0,
            visitDateTime: new Date(),
            isSafetyInstructionsGiven: false,
            isGeneralVisitGuideGiven: false,
            isTaskingDocumentReceived: false,
        },

        // validate: {},
    });

and inside MantineForm component, error is in here

<DateTimePicker
     value={form.values.visitDateTime}
     onChange={(val) => form.setFieldValue("visitDateTime", val)}
     label="Ziyaret Tarih ve Saati"
     clearable
     valueFormat="DD.MM.YYYY - HH:mm"
     locale="tr"
/>

val in the part of form.setFieldValue("visitDateTime", val) here gives the below error

Argument of type 'DateValue' is not assignable to parameter of type 'Date | ((prevValue: Date) => Date)'. Type 'null' is not assignable to type 'Date | ((prevValue: Date) => Date)'.ts(2345)

How can I solve this type problem?

2 Answers 2

0

It simply means that val can be null, which is not acceptable as second argument while calling form.setFieldValue.

To fix this: You can simply convert your onChnage to:

onChange={(val) => val && form.setFieldValue("visitDateTime", val)}

Another option can be to remove the prop clearable in DateTimePicker, so that you always receive some valid value.

Sign up to request clarification or add additional context in comments.

Comments

0
<DateTimePicker
     value={form.values.visitDateTime}
     onChange={(val) => form.setFieldValue("visitDateTime", val === null ? null : val)}
     label="Ziyaret Tarih ve Saati"
     clearable
     valueFormat="DD.MM.YYYY - HH:mm"
     locale="tr"
/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.