react-datepicker with react-final-form - reactjs

I'm using the react-datepicker inside the react-final-form. I'm using a template with multiple steps https://codesandbox.io/s/km2n35kq3v?file=/index.js. The problem is that I'm not able to integrate the datepicker inside the component. My Code looks like this:
return (
<Field name={props.name} parse={() => true}>
{props => (
<DatePicker
locale="de"
placeholderText="Datum eingeben"
selected={startDate}
dateFormat="P"
openToDate={new Date()}
minDate={new Date()}
disabledKeyboardNavigation
name={props.name}
value={startDate}
onChange={(date) => setStartDate(date)}
/>
)}
</Field>
);
Does anyone knows how I can use it so the data gets passed at the end of the form?
Best regards

I used the wizard form example you sent and added DatePicker similar to yours.
Check the wizard example
But basically, I changed your onChange method to actually use react-final-form field props. Now, it uses this.props.input.onChange, which updates the final form state value, and used this.props.input.value to set the selected state (you can then load initial values into final form):
const RenderDatePicker = ({ name, input, input: { value, onChange } }) => {
return (
<DatePicker
locale="de"
placeholderText="Datum eingeben"
dateFormat="P"
selected={value && isValid(value) ? toDate(value) : null} // needs to be checked if it is valid date
disabledKeyboardNavigation
name={name}
onChange={(date) => {
// On Change, you should use final-form Field Input prop to change the value
if (isValid(date)) {
input.onChange(format(new Date(date), "dd-MM-yyyy"));
} else {
input.onChange(null);
}
}}
/>
);
};
<div>
<label>Date of birth</label>
<Field
name="dateOfBirth"
component={RenderDatePicker}
validate={required}
/>
<Error name="dateOfBirth" />
</div>
Hopefully this helps you.

Related

State is not upating in the react-select library

I'm using react-select library and I'm calling the function in the options and that function takes one parameter. But when countryCode state is updated, it does not update in select tag.
When I pass hardcoded value in the function like options={getSortedState("US")}, it works fine but when I get value from API and set state of parameter then it does not work.
<Controller
control={control}
name="options"
render={({ field }) => (
<Select
value={field.value}
options={getSortedState(countryCode)}
onChange={(state) => field.onChange(state)}
placeholder="Select State"
defaultValue={field.value}
onInputChange={() => {
console.log('A', countryCode);
setCountryCode('');
setCountryCode(countryCode);
}}
// options={getLiDepartments()}
/>
)}
/>

Material UI Slider with React hook form

I'm having a hard time integrating material Ui Slider with React hook form. It's not registering the values. It's printing the value as undefined on console.log. Got an idea where I might be wrong?
<Controller
render={({ field: { value, onChange } }) => (
<CustomSlider
onChange={onChange}
value={value}
max={60}
marks={marks}
className={classes.slider}
defaultValue={10}
/>
)}
control={control}
name="slider"
/>
Here is codesandbox made by author of react-hook-form that contains many examples. Mui integration is also made.
According to example, you are supposted to do something like this:
<Controller
name="MUI_Slider"
control={control}
defaultValue={[0, 10]}
render={(props) => (
<Slider
{...props}
onChange={(_, value) => {
props.onChange(value);
}}
valueLabelDisplay="auto"
max={10}
step={1}
/>
)}
/>
Another question is in which part of your code are you trying to console.log() values.
You can use watch() method or
<button Click={() => console.log(getValues())}>get values</button>

Having an issue with antd datepicker not displaying value

Im having an issue with the datepicker component. I have tried multiple things but the date I am passing will not display in the datepicker. I just set the fields in the beginning of my component. The warrantynotes field shows just fine. I get different errors depending on what I try but mostly :
TypeError: date.clone is not a function
format
form.setFieldsValue({
datepurchased: e.datepurchased,
warrantynotes: e.warrantynotes,
})
<Form.Item
name={'datepurchased'}
label="Date Purchased:"
// defaultValue={moment(warranty.datepurchased)}
// format={dateFormat}
// defaultPickerValue={moment(warranty.datepurchased)}
>
<DatePicker
onChange={handleChange}
// defaultValue={moment(warranty.datepurchased)}
// format={dateFormat}
// defaultPickerValue={moment(warranty.datepurchased)}
></DatePicker>
</Form.Item>
I tried to change your code, use defaultValue instead defaultPickerValue
const { DatePicker, Space } = antd;
function handleChange() {}
const myDate = "2021-01-07";
ReactDOM.render(
<Space direction="vertical" size={12}>
<DatePicker onChange={handleChange} defaultPickerValue={moment()} />
<DatePicker onChange={handleChange} defaultValue={moment()} />
<DatePicker onChange={handleChange} defaultValue={moment(myDate)} />
</Space>,
mountNode
);

React-Quill with Formik is not updating props

I am using Formik and React-Quill in my form,
The value seems to be updating when i use <input> but when i plug-in <ReactQuill /> it's not.
Is there something wrong with the setup?
<Field
name="designation"
value={this.props.values.designation}
render={({ field /* _form */ }) => (
// <input {...field} placeholder="designation" />
<ReactQuill
{...field}
/>
)}
/>
For anybody who is still interested in the answer (like I was), you can find it here:
<Formik initialValues={{ designation: '' }}>
<Field name="designation">
{({ field }) => <ReactQuill value={field.value} onChange={field.onChange(field.name)} />}
</Field>
</Formik>
This helps match formik field to ReactQuill props.
I am using "setFieldValue" to update changes. This is working perfectly fine for dynamic Formik forms.
<ReactQuill
value={values.description}
onChange={v => setFieldValue('description', v)}
/>

How to use date picker in redux form

I'm using date picker in redux form. I want to show only year. But while posting data year field is going NULL in database.
Can anyone help me.
This is my code :
const renderDatePicker = ({input, placeholder, defaultValue, meta: {touched, error} }) => (
<div>
<DateTimePicker {...input} format={"YYYY"} selected={input.value ? moment(input.value) : null} time={false} initialView={"decade"} finalView={"decade"} />
{touched && error && <span>{error}</span>}
</div>
);
<Field name="year" component={renderDatePicker} />
Thanks
you have to pass all props to DateTimePicker props in the correct way. I don't know how is working DateTimePicker component, but when you do
<DateTimePicker **{...input}** format={"YYYY"} selected={input.value ?
moment(input.value) : null} time={false} initialView={"decade"}
finalView={"decade"} />
{touched && error && <span>{error}</span>}
Into DateTimePicker has to assign redux-form input to its input prop, else always you will get null in that case. Also you can check in redux-form with property function 'validate' every time when you change that value you should see it.
edit:
I was looking for some issues like this, because I have to do something looked like in DatePicker for React-Native, in that case we don't have input but I have used in this way:
<Field name="birthday"
component={ props =>
<DatePicker
date={props.input.value}
onDateChange={(date) => props.input.onChange(moment(new Date(date)).format("MM/DD/YYYY"))}
/>
}/>
It's not best way, but this solution could help your case.

Resources