Skip to content

Commit 87c3419

Browse files
committed
Add support for localized years
Closes #317
1 parent 96fe075 commit 87c3419

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

src/DateInput.jsx

+16-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
getEnd,
1616
} from './shared/dates';
1717
import { isMaxDate, isMinDate } from './shared/propTypes';
18-
import { between } from './shared/utils';
18+
import { between, getYearOffset } from './shared/utils';
1919

2020
const defaultMinDate = new Date();
2121
defaultMinDate.setFullYear(1, 0, 1);
@@ -377,9 +377,17 @@ export default class DateInput extends PureComponent {
377377
*/
378378
onChange = (event) => {
379379
const { name, value } = event.target;
380+
const { locale } = this.props;
380381

381382
this.setState(
382-
{ [name]: value },
383+
() => {
384+
if (name === 'year') {
385+
const offset = getYearOffset(locale);
386+
return { [name]: value !== null ? `${value - offset}` : null };
387+
}
388+
389+
return { [name]: value };
390+
},
383391
this.onChangeExternal,
384392
);
385393
}
@@ -434,7 +442,8 @@ export default class DateInput extends PureComponent {
434442

435443
const values = {};
436444
formElements.forEach((formElement) => {
437-
values[formElement.name] = formElement.value;
445+
const { [formElement.name]: value } = this.state;
446+
values[formElement.name] = value;
438447
});
439448

440449
if (formElements.every((formElement) => !formElement.value)) {
@@ -534,7 +543,9 @@ export default class DateInput extends PureComponent {
534543
}
535544

536545
renderYear = (currentMatch, index) => {
537-
const { autoFocus, yearAriaLabel, yearPlaceholder } = this.props;
546+
const {
547+
autoFocus, locale, yearAriaLabel, yearPlaceholder,
548+
} = this.props;
538549
const { year } = this.state;
539550

540551
return (
@@ -544,6 +555,7 @@ export default class DateInput extends PureComponent {
544555
ariaLabel={yearAriaLabel}
545556
autoFocus={index === 0 && autoFocus}
546557
inputRef={this.yearInput}
558+
locale={locale}
547559
placeholder={yearPlaceholder}
548560
value={year}
549561
valueType={this.valueType}

src/DateInput/YearInput.jsx

+13-3
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,26 @@ import {
1010
isRef,
1111
isValueType,
1212
} from '../shared/propTypes';
13-
import { safeMax, safeMin } from '../shared/utils';
13+
import { getYearOffset, safeMax, safeMin } from '../shared/utils';
1414

1515
export default function YearInput({
16+
locale,
1617
maxDate,
1718
minDate,
1819
placeholder = '----',
20+
value,
1921
valueType,
2022
...otherProps
2123
}) {
2224
const maxYear = safeMin(275760, maxDate && getYear(maxDate));
2325
const minYear = safeMax(1, minDate && getYear(minDate));
2426

27+
const offset = getYearOffset(locale);
28+
29+
const localizedMaxYear = maxYear + offset;
30+
const localizedMinYear = minYear + offset;
31+
const localizedValue = value !== null ? Number(value) + offset : null;
32+
2533
const yearStep = (() => {
2634
if (valueType === 'century') {
2735
return 10;
@@ -32,11 +40,12 @@ export default function YearInput({
3240

3341
return (
3442
<Input
35-
max={maxYear}
36-
min={minYear}
43+
max={localizedMaxYear}
44+
min={localizedMinYear}
3745
name="year"
3846
placeholder={placeholder}
3947
step={yearStep}
48+
value={localizedValue}
4049
{...otherProps}
4150
/>
4251
);
@@ -47,6 +56,7 @@ YearInput.propTypes = {
4756
className: PropTypes.string.isRequired,
4857
disabled: PropTypes.bool,
4958
inputRef: isRef,
59+
locale: PropTypes.string,
5060
maxDate: isMaxDate,
5161
minDate: isMinDate,
5262
onChange: PropTypes.func,

src/shared/utils.js

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { getFormatter } from './dateFormatter';
2+
13
/**
24
* Returns a value no smaller than min and no larger than max.
35
*
@@ -15,6 +17,13 @@ export function between(value, min, max) {
1517
return value;
1618
}
1719

20+
export function getYearOffset(locale) {
21+
const now = new Date();
22+
return Number(
23+
getFormatter({ year: 'numeric' })(locale, now).match(/\d{1,}/)[0],
24+
) - now.getFullYear();
25+
}
26+
1827
function isValidNumber(num) {
1928
return num !== null && num !== false && !Number.isNaN(Number(num));
2029
}

0 commit comments

Comments
 (0)