简体   繁体   English

Material ui DatePicker 仅启用年份

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM