在 TextField type=time 材质 ui 中设置默认选取时间
I would like to set a default time to a textfield type time of Material UI.
My requeriment is that before clicking the picker, no time have been settted up, but when clicking, appears 08:00 as the default time to pick.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过给它一个设置为状态的
值
,将您的TextField
变成一个受控组件,并更新它onChange
。该 API 没有焦点或点击处理程序属性,但是您可以用
span
包装TextField
并在其上设置点击处理程序。从那里,创建一个函数,如果事件目标中没有值,则将状态更新为字符串值“08:00”。这将允许您以很少的开销重用
onClick
和onChange
的函数。onClick 之所以有效,是因为事件冒泡。您将单击输入,但这会冒泡到跨度的事件侦听器。不应该有机会单击跨度,因为它将适合内容,并且输入将覆盖在其上。
Turn your
TextField
into a controlled component, by giving it avalue
that is set to state, and update itonChange
.There are no focus or click handler props for that API, but you can wrap the
TextField
with aspan
and set a click handler on that.From there, create a function to update the state to be a string value of
"08:00"
if there is no value in the event target. This will allow you to reuse the function foronClick
andonChange
with little overhead.The onClick works because of event bubbling. You will be clicking the input, but that bubbles up to the span's event listener. There should be no chance of clicking on the span, because it will fit the content, and input will lay over it.