React日期时间选择器行为异常

huangapple go评论102阅读模式
英文:

React datetime picker misbehaving

问题

我有以下来自react-datetime库的<Datetime>选择器。

  1. import React from "react";
  2. import Datetime from "react-datetime";
  3. import moment from "moment";
  4. import "./styles.css";
  5. export default class App extends React.Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {
  9. fromDateTime: "",
  10. };
  11. }
  12. render() {
  13. return (
  14. <div className="App">
  15. <Datetime
  16. value={this.state.fromDateTime}
  17. dateFormat="YYYY-MM-DD"
  18. timeFormat="HH:mm"
  19. onChange={(momentObj) => {
  20. if (momentObj && momentObj.isValid && momentObj.isValid()) {
  21. const formattedDateTime = momentObj.format("YYYY-MM-DD HH:mm");
  22. this.setState({ fromDateTime: formattedDateTime });
  23. }
  24. }}
  25. />
  26. </div>
  27. );
  28. }
  29. }

我可以复制粘贴日期2023-06-15 00:30到输入框中。但一旦我复制粘贴了,就不能使用删除和退格按钮来修改日期。为什么会这样?

您可以在这个codesandbox中尝试这种行为。

注:在codesandbox中,日历始终是打开的。但在我的笔记本电脑上不是这样的。当我点击输入框内部时,日历会打开,当我点击外部时,它会关闭。

英文:

I have following &lt;Datetime&gt; picker from react-datetime library.

  1. import React from &quot;react&quot;;
  2. import Datetime from &quot;react-datetime&quot;;
  3. import moment from &quot;moment&quot;;
  4. import &quot;./styles.css&quot;;
  5. export default class App extends React.Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {
  9. fromDateTime: &quot;&quot;,
  10. };
  11. }
  12. render() {
  13. return (
  14. &lt;div className=&quot;App&quot;&gt;
  15. &lt;Datetime
  16. value={this.state.fromDateTime}
  17. dateFormat=&quot;YYYY-MM-DD&quot;
  18. timeFormat=&quot;HH:mm&quot;
  19. onChange={(momentObj) =&gt; {
  20. if (momentObj &amp;&amp; momentObj.isValid &amp;&amp; momentObj.isValid()) {
  21. const formattedDateTime = momentObj.format(&quot;YYYY-MM-DD HH:mm&quot;);
  22. this.setState({ fromDateTime: formattedDateTime });
  23. }
  24. }}
  25. /&gt;
  26. &lt;/div&gt;
  27. );
  28. }
  29. }

I can copy pasted date 2023-06-15 00:30 into the input field. But once I copy pasted, I cannot use delete and backspace button to modify that date. Why is it so?

You can try out this behavior in [https://codesandbox.io/s/keen-tereshkova-h7pmjc?file=/src/App.jsthis codesandbox].

PS: In codesandbox, the calendar is always open. This is not the case on my laptop. Calendar opens when I click inside input box and closes when I click outside.

答案1

得分: 1

一次,如果您在日历上单击日期,它将返回一个不再是日期的值,例如2021-06-15 00:0。

因此,将非日期值更改为可更新的,如:

  1. if (momentObj && momentObj.isValid && momentObj.isValid()) {
  2. const formattedDateTime = momentObj.format("YYYY-MM-DD HH:mm");
  3. this.setState({ fromDateTime: formattedDateTime });
  4. } else {
  5. this.setState({ fromDateTime: momentObj.toString() });
  6. }
英文:

Once, if you click the date on calendar, it returns value which is no longer date like 2021-06-15 00:0.

So, change the non-date values ​​to be updatable. like :

  1. if (momentObj &amp;&amp; momentObj.isValid &amp;&amp; momentObj.isValid()) {
  2. const formattedDateTime = momentObj.format(&quot;YYYY-MM-DD HH:mm&quot;);
  3. this.setState({ fromDateTime: formattedDateTime });
  4. } else {
  5. this.setState({ fromDateTime: momentObj.toString() });
  6. }

huangapple
  • 本文由 发表于 2023年6月15日 06:42:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定