diff --git a/docs/form-customization.md b/docs/form-customization.md index d27565613d..2c9177c708 100644 --- a/docs/form-customization.md +++ b/docs/form-customization.md @@ -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: { @@ -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; diff --git a/packages/core/src/components/widgets/AltDateWidget.js b/packages/core/src/components/widgets/AltDateWidget.js index 084e8ef053..9549de44f5 100644 --- a/packages/core/src/components/widgets/AltDateWidget.js +++ b/packages/core/src/components/widgets/AltDateWidget.js @@ -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; + 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) });