[英]Material ui DatePicker enable only year
Is there any way to show only years in Material UI DatePicker with react ?有什么方法可以在Material UI DatePicker中只显示几年的react吗? I want to allow the user to be able to select only the
year
and not months or days .我想让用户能够 select 只有
year
而不是几个月或几天。 Note: The option openToYearSelection
does not achieve this neither does autoOk
help.注意:选项
openToYearSelection
不能实现这一点, autoOk
也没有帮助。
I came across the same issue and the way i have sorted it out is by using material-ui-pickers library Here is an example:我遇到了同样的问题,我解决的方法是使用material-ui-pickers库这里是一个例子:
import React, { Fragment, useState } from "react";
import { DatePicker, InlineDatePicker } from "material-ui-pickers";
function YearMonthPicker(props) {
const [selectedDate, handleDateChange] = useState(new Date());
return (
<Fragment>
<div className="picker">
<DatePicker
views={["year"]}
label="Year only"
value={selectedDate}
onChange={handleDateChange}
animateYearScrolling
/>
</div>
</Fragment>
);
}
export default YearMonthPicker;
Use views prop for mui DatePicker.为 mui DatePicker 使用 views 道具。
<DatePicker views={['year']} />
Also you can disable some years using shouldDisableYear prop.您也可以使用shouldDisableYear道具禁用一些年份。
https://mui.com/x/api/date-pickers/date-picker/ https://mui.com/x/api/date-pickers/date-picker/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.