材料 UI 日期选取器在 Redux 窗体中将空值保存为 Date(0)



我有一个Redux表单,它使用material-uu日期选择器来保存日期。如果有人没有选择日期并提交表单,则日期将保存为 1970-01-02。不幸的是,文档似乎没有包含任何用于处理该问题的 props,我无法通过验证来解决它 - 这些字段必须是可选的。

当尚未给出值时,是否有任何选项可以强制组件不输入 Date(0(?还是应该使用其他日期选择器工具?我们正在使用材料用户界面 v.0.19.4。我已经尝试使用空状态,它只会在更改时发生变化,但它没有帮助。

这就是田野的样子。

<Field
name="endOfPartnership"
type="text"
component={DatePicker}
className={css.fullWidth}
floatingLabelFocusStyle={styles.floatingLabelColor}
underlineFocusStyle={styles.floatingLabelColor}
floatingLabelText={intl.formatMessage(defineMessages.endOfPartnership)}
fullWidth
formatDate={formatDate}
minDate={minDate}
/>

我找到了定义行为的位置,仅供您参考。从他们的源代码。

https://github.com/mui-org/material-ui/blob/v0.x/src/DatePicker/DatePicker.js

有这条线。

value={this.state.date ? formatDate(this.state.date)

formatDate在哪里,

formatDate = (date) => {
if (this.props.locale) {
const DateTimeFormat = this.props.DateTimeFormat || dateTimeFormat;
return new DateTimeFormat(this.props.locale, {
day: 'numeric',
month: 'numeric',
year: 'numeric',
}).format(date);
} else {
return formatIso(date);
}
};

我想这就是为什么日期总是"格式良好"的原因。

函数将日期解析为字符串似乎存在问题。

有人在日期转换方面遇到了问题,并做出了一个非常丑陋的解决方法。我们切换到 moment.js这使我们能够避免上一个问题,我可以确认它现在可以在没有提供输入时完美运行 => materialUI 日期选择器正常工作。

谢谢克里斯托弗!

最新更新