reactjs - KeyboardDatePicker MaterialUI - 自定义遮罩格式
问题描述
当使用 Material UI 的 KeyboardDatePicker 时,当格式为 dd/MMM/yyyy(例如 1983 年 4 月 26 日)时,它不允许用户输入字母。我将如何创建掩码来创建自定义格式?
到目前为止,我的尝试是这样的:
<KeyboardDatePicker
mask={[
/\d/,
/\d/,
"/",
/[a-zA-Z]/,
/[a-zA-Z]/,
/[a-zA-Z]/,
"/",
/\d/,
/\d/
]}
format="dd/MMM/yyyy"
placeholder="DD/MMM/YYYY"
label="Date of birth"
openTo="year"
views={["year", "month", "date"]}
value={selectedDate}
/>
解决方案
掩码只能接受string
,所以在您的情况下,正则表达式数组将不起作用。
还有另一个标志refuse
可以用来拒绝输入中的模式。我们可以利用它来允许字母输入。
这是代码框:
https://codesandbox.io/s/material-ui-pickers-keyboard-birthdate-forked-hmtgv?file=/src/index.js
const refusePattern = () => {
return /[^a-zA-Z0-9]+/gi; //reject the pattern which is not a-z, A-Z and 0-9
};
<KeyboardDatePicker
...
refuse={refusePattern()}
/>
推荐阅读
- java - 存根存储库时获取 NullPointerException
- vb.net - 返回符合条件的列表框项数组 - Vb.net
- php - 以下代码行如何在 PHP 中工作?
- python - 如何绕过 pyplot 等高线函数错误“在数据范围内未找到等高线级别”
- swift - 如何将视频作为文件从本地 URL (macOS) 复制到 NSPasteboard?
- kotlin - 在遍历两个映射时是否被认为是不好的约定,我不检查其中一个映射中是否存在键?
- node.js - 错误“DocumentNotFoundError:找不到查询文档”{_id:xxx}
- javascript - 如何将 JavaScript 动态数据转换为 HTML 并渲染?
- jquery - ajax执行后jQuery变量不更新
- javascript - 从 REACT 17 中文件 functionComponentA 中的函数组件 B 中的文件 fileA 中的类组件 ClassComponentA 调用名为 functionA 的函数