[英]react-datepicker with react-final-form
I'm using the react-datepicker inside the react-final-form.我在 react-final-form 中使用 react-datepicker。 I'm using a template with multiple steps https://codesandbox.io/s/km2n35kq3v?file=/index.js .
我正在使用包含多个步骤的模板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.我使用了您发送的向导表单示例,并添加了与您的类似的 DatePicker。 Check the wizard example
检查向导示例
But basically, I changed your onChange
method to actually use react-final-form
field
props.但基本上,我更改了您的
onChange
方法以实际使用react-final-form
field
道具。 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):现在,它使用
this.props.input.onChange
更新最终形式 state 值,并使用this.props.input.value
设置选定的 state(然后您可以将初始值加载到最终形式):
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.希望这对你有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.