如何在MUI的DatePicker组件中以降序显示年份列表

huangapple go评论54阅读模式
英文:

How to show year list in a descending order for DatePicker component in MUI

问题

我想要显示日期选择器的年份列表,按降序排列,但无法实现。它首先显示最小日期,然后逐渐降到最大日期。

我编写的代码如下。

```html
<MuiPickersUtilsProvider utils={DateFnsUtils}>
    <DatePicker
        views={['year', 'month']}
        label="Month"
        value={selectedDate}
        onChange={(value) => setSelectedDate(value)}
        animateYearScrolling
        minDate={'01-01-2000'}
        maxDate={new Date()}
    />
</MuiPickersUtilsProvider>

<details>
<summary>英文:</summary>

[![MUI Datepicker][1]][1]


  [1]: https://i.stack.imgur.com/nUc4G.png

I want to show the datepicker year list in Descending order but am unable to do. It shows min date at first and then goes down to the max date.

The code I wrote is written below.

<MuiPickersUtilsProvider utils={DateFnsUtils}>
<DatePicker
views={['year', 'month']}
label="Month"
value={selectedDate}
onChange={(value) => setSelectedDate(value)}
animateYearScrolling
minDate={'01-01-2000'}
maxDate={new Date()}
/>
</MuiPickersUtilsProvider>


</details>


# 答案1
**得分**: 1

你可以参考这个[实现][1]在CodeSandbox上实现日期选择器中年份按降序排列的功能。如果你需要更多信息,请查看GitHub上的[功能请求帖子][2]。

```javascript
import React from "react";
import { MuiPickersUtilsProvider } from "material-ui-pickers";
import MomentUtils from "@date-io/moment";
import { DatePicker } from "material-ui-pickers";
import moment from "moment";
moment.locale();

class LocalizedUtils extends MomentUtils {
  getYearRange(start, end) {
    const startDate = this.moment(end).startOf("year");
    const endDate = this.moment(start).endOf("year");
    const years = [];

    let current = startDate;
    while (current.isAfter(endDate)) {
      years.push(current);
      current = current.clone().subtract(1, "year");
    }

    return years;
  }
}

class App extends React.Component {
  state = { date: new Date() };

  handleDateChange = date => {
    this.setState({ date });
  };

  render() {
    return (
      <MuiPickersUtilsProvider utils={LocalizedUtils}>
        <DatePicker
          clearable
          onChange={this.handleDateChange}
          showTabs={false}
          variant="outlined"
          minDate={new Date("01-01-1900")}
        />
      </MuiPickersUtilsProvider>
    );
  }
}

export default App;
英文:

You can refer to this implementation on codesandbox to achieve the above mentioned descending order of years in datepicker. If you want more information on this, refer to this feature request thread on github.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

import React from &quot;react&quot;;
import { MuiPickersUtilsProvider } from &quot;material-ui-pickers&quot;;
import MomentUtils from &quot;@date-io/moment&quot;;
import { DatePicker } from &quot;material-ui-pickers&quot;;
import moment from &quot;moment&quot;;
moment.locale();

class LocalizedUtils extends MomentUtils {
  getYearRange(start, end) {
    const startDate = this.moment(end).startOf(&quot;year&quot;);
    const endDate = this.moment(start).endOf(&quot;year&quot;);
    const years = [];

    let current = startDate;
    while (current.isAfter(endDate)) {
      years.push(current);
      current = current.clone().subtract(1, &quot;year&quot;);
    }

    return years;
  }
}

class App extends React.Component {
  state = { date: new Date() };

  handleDateChange = date =&gt; {
    this.setState({ date });
  };

  render() {
    return (
      &lt;MuiPickersUtilsProvider utils={LocalizedUtils}&gt;
        &lt;DatePicker
          clearable
          onChange={this.handleDateChange}
          showTabs={false}
          variant=&quot;outlined&quot;
          minDate={new Date(&quot;01-01-1900&quot;)}
        /&gt;
      &lt;/MuiPickersUtilsProvider&gt;
    );
  }
}

export default App;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月8日 19:12:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定