Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for descending date range. #1681

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion docs/form-customization.md
Expand Up @@ -112,7 +112,7 @@ Please note that, even though they are standardized, `datetime-local` and `date`

![](https://i.imgur.com/VF5tY60.png)

You can customize the list of years displayed in the `year` dropdown by providing a ``yearsRange`` property to ``ui:options`` in your uiSchema. Its also possible to remove the `Now` and `Clear` buttons with the `hideNowButton` and `hideClearButton` options.
You can customize the list of years displayed in the `year` dropdown by providing a ``yearsRange`` property to ``ui:options`` in your uiSchema. The range can be descending by specifying the larger value first. Its also possible to remove the `Now` and `Clear` buttons with the `hideNowButton` and `hideClearButton` options.

```jsx
uiSchema: {
Expand All @@ -129,6 +129,14 @@ uiSchema: {
},
```

You can also specify negative values which will be treated relative to the current year, so if it is 2020 and the range is set as follows.

```
yearsRange: [-18, -120],
```

Years from 2002-1900 will be shown.

#### For `number` and `integer` fields

* `updown`: an `input[type=number]` updown selector;
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/components/widgets/AltDateWidget.js
Expand Up @@ -4,6 +4,9 @@ import PropTypes from "prop-types";
import { shouldRender, parseDateString, toDateString, pad } from "../../utils";

function rangeOptions(start, stop) {
if (start < 0) start = new Date().getFullYear() + start;
if (stop < 0) stop = new Date().getFullYear() + stop;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make this change:

if (stop < 0) {
  stop = new Date().getFullYear() + stop;
}

if (start > stop) return rangeOptions(stop, start).reverse();
let options = [];
for (let i = start; i <= stop; i++) {
options.push({ value: i, label: pad(i, 2) });
Expand Down