【问题标题】:How can I edit the value of the timepicker from the keyboard?如何从键盘编辑时间选择器的值?
【发布时间】:2019-09-13 18:52:59
【问题描述】:

我有一个material-ui-time-picker,我想控制这个输入,它工作得很好,但我想编辑从键盘输入的时间,而不是当我点击时钟上的输入时。

我的代码是:

import React, { Component } from "react";
import { TimePicker } from "material-ui-time-picker";
import { Input as Time, Dialog as Clock } from "@material-ui/core";

openDialog = () => this.setState({ isOpen: true });
  closeDialog = () => this.setState({ isOpen: false });
  handleDialogTimeChange = newValue => {
    const hours = newValue
      .getHours()
      .toString()
      .padStart(2, "0");
    const minutes = newValue
      .getMinutes()
      .toString()
      .padStart(2, "0");
    const textValue = hours + ":" + minutes;
    this.setState({ time: textValue });
  };
  handleKeyboardTimeChange = time => this.setState({ time });
  createDateFromTextValue = value => {
    const splitParts = value.split(":");
    return new Date(1970, 1, 1, splitParts[0], splitParts[1]);
  };
render() {

    return (

      <div>
        <Time
          value={this.state.time}
          onChange={this.handleKeyboardTimeChange}
          endAdornment={
            <InputAdornment position="end">
              <IconButton onClick={this.openDialog}>
                <AccessTime />
              </IconButton>
            </InputAdornment>
          }
          //}
        />
        <Clock maxWidth="xs" open={this.state.isOpen}>
          <TimePicker
            mode="24h"
            value={this.createDateFromTextValue(this.state.time)}
            onChange={this.handleDialogTimeChange}
            autoOk={true}
            cancelLabel=""
            okLabel=""
            placeholder=""
            disableUnderline={true}
          />
        </Clock>
      </div>
    );
  }

我的沙盒:https://codesandbox.io/s/vm9wm19p27

当我运行它时,我得到了这个输入,但是当我编辑他的值时,输入会消失。

我该如何解决?

【问题讨论】:

  • 我在上面的链接上看不到任何可运行的东西
  • @KOTIOS 你能查一下吗?
  • 请在此处添加代码,因为链接似乎已损坏
  • 浏览器输出仍为空白
  • @Ivanofuganda 查看我的帖子,我添加了代码。

标签: javascript reactjs input material-ui timepicker


【解决方案1】:

我已经分叉了您的沙盒并进行了一些调整。虽然我没有修复它 - 我只会告诉你当前的问题。

https://codesandbox.io/s/j24rqql9n9

我修改了两行
在你的构造函数中,我添加了

this.handleKeyboardTimeChange = this.handleKeyboardTimeChange.bind(this)

还有你的handleKeyboardTimeChange:

handleKeyboardTimeChange(event) {
  this.setState({ time: event.target.value });
}

这只是将状态设置为从那里看到的确切值。无需额外验证。

【讨论】:

  • 需要干预的示例场景:gyazo.com/face509b4be5ee955e803ede3104428c
  • 谢谢@Caleb,请问如何更改时钟的颜色?
  • 一种移动的目标。我确信有一个更好的方法是使用 react 组件将其包装在 withStyles 属性中并在编译时注入样式。但是div[class^="Clock-circle"] { background: #f00; } 这行 CSS 可以解决问题。 codesandbox.io/s/j24rqql9n9
  • 如果我正确理解自己的代码,它只会找到以Clock-circle 开头的任何类并应用背景颜色。因此,如果您有多个这些并且您不希望它们都是相同的颜色,这将不适用
  • 谢谢,你能帮帮我吗? stackoverflow.com/questions/55847842/…
猜你喜欢
  • 2010-12-31
  • 1970-01-01
  • 2016-09-23
  • 1970-01-01
  • 1970-01-01
  • 2020-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多