Using DateInput in formik form

1 Answer 981 Views
DateInput
Wreeecks
Top achievements
Rank 2
Iron
Iron
Iron
Wreeecks asked on 21 Jan 2022, 05:55 AM | edited on 21 Jan 2022, 05:56 AM

Is there an elegant way of integrating dateinput with formik?

My code below works however it's  tedious (not DRY :( ) to implement. It will require a state for every date input.  I hope you don't mind sharing your code. Thanks

 

const ClaimsForm = () => {
    const [value, setValue] = React.useState<Date | null>(new Date());
    const changeDate = (event: DateInputChangeEvent) => {
      setValue(event.value);
    };
    
    const initialValues = {
        decisionDate: value
    };

    const handleSubmit = (values: any) => {
        console.log(values)
    }

  return (
        <Formik
            initialValues={initialValues}
            onSubmit={handleSubmit}
            enableReinitialize={true}
            >
        
        {({ isSubmitting }) => (
            <Form>  
                <DateInput name='decisionDate' value={value} onChange={changeDate}  />
            </Form>
        )}
  )
}


1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 21 Jan 2022, 09:02 AM

Hello,

This is the approach we recommend when using our components with Formik:

https://stackblitz.com/edit/react-cgbtoa-ckj763?file=app%2Fmain.jsx

The developer may have one general KendoReactDatePickerField component and reuse it in multiple forms.

Regards,
Stefan
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Wreeecks
Top achievements
Rank 2
Iron
Iron
Iron
commented on 24 Jan 2022, 08:28 AM

Thanks for your response stefan.

I'm still struggling implementing formik using kendo ui components, specifically Autocomplete. 

It seems that formik "onChange" event when autocomplete sets the value of an input box. Can you share with me a snippet on how integrate these 2 components? TIA!

 

Stefan
Telerik team
commented on 24 Jan 2022, 09:24 AM

Hello,

I used the same approach and added an AutomComlete:

https://stackblitz.com/edit/react-cgbtoa-pu8tl4?file=app%2Fmain.jsx

If there is an issue, please share an example with the issue and I will take a look at it.

Wreeecks
Top achievements
Rank 2
Iron
Iron
Iron
commented on 27 Jan 2022, 11:42 PM

Hi Stefan,

Not sure if this is a bug, in the example you've given above, TextArea changes are not saved in formik values..

to replicate:

1. First, add submit button.

2. Submit the form without touching the fields. A popup appears with all the form values.

3. Now, update textarea (type whatever you want) then submit again.   The popup still appears but the TextArea is value is gone.

 

Stefan
Telerik team
commented on 28 Jan 2022, 07:05 AM

Hello,

I can confirm that this is an issue with the current version of the KendoReact TextArea.

I have logged this for fixing, and the status can be monitored here:

https://github.com/telerik/kendo-react/issues/1205

Currently, there is no workaround available, if one is found it will be added to the issue.

Additionally, I have added some Telerik points to your account for bringing this to our attention.

Apologies for the inconvenience this may have caused you.
Wreeecks
Top achievements
Rank 2
Iron
Iron
Iron
commented on 31 Jan 2022, 06:46 AM

Thanks for letting me know. 

Just in case anyone is looking for a workaround, check the code below. It's using formik's field. 


<Field as="textarea" row='4' name="notes" placeholder="Textarea" className='form-control mb-3'/>
<ErrorMessage name="notes" render={ msg => 
    <small className="text-warning">{msg}</small>
  } />  

Tags
DateInput
Asked by
Wreeecks
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Stefan
Telerik team
Share this question
or