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

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

React datetime picker misbehaving

问题

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

import React from "react";
import Datetime from "react-datetime";
import moment from "moment";

import "./styles.css";

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      fromDateTime: "",
    };
  }
  render() {
    return (
      <div className="App">
        <Datetime
          value={this.state.fromDateTime}
          dateFormat="YYYY-MM-DD"
          timeFormat="HH:mm"
          onChange={(momentObj) => {
            if (momentObj && momentObj.isValid && momentObj.isValid()) {
              const formattedDateTime = momentObj.format("YYYY-MM-DD HH:mm");
              this.setState({ fromDateTime: formattedDateTime });
            }
          }}
        />
      </div>
    );
  }
}

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

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

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

英文:

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

import React from &quot;react&quot;;
import Datetime from &quot;react-datetime&quot;;
import moment from &quot;moment&quot;;

import &quot;./styles.css&quot;;

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
        fromDateTime: &quot;&quot;,
    };
  } 
  render() {
    return (
      &lt;div className=&quot;App&quot;&gt;
        &lt;Datetime
          value={this.state.fromDateTime}
          dateFormat=&quot;YYYY-MM-DD&quot;
          timeFormat=&quot;HH:mm&quot;
          onChange={(momentObj) =&gt; {
            if (momentObj &amp;&amp; momentObj.isValid &amp;&amp; momentObj.isValid()) {
              const formattedDateTime = momentObj.format(&quot;YYYY-MM-DD HH:mm&quot;);
              this.setState({ fromDateTime: formattedDateTime });
            }
          }}
        /&gt;
      &lt;/div&gt;
    );
  }
}

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。

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

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

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 :

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

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:

确定