Skip to content

Commit 1bcdce7

Browse files
committed
Cache date formatters
fixup cache
1 parent 3d60c38 commit 1bcdce7

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

src/DateTimeInput.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import AmPm from 'react-time-picker/dist/TimeInput/AmPm';
2222
import Divider from './Divider';
2323
import NativeInput from './DateTimeInput/NativeInput';
2424

25-
import { getFormatter, formatDate } from './shared/dateFormatter';
25+
import { getFormatter, getNumberFormatter, formatDate } from './shared/dateFormatter';
2626
import {
2727
convert12to24,
2828
convert24to12,
@@ -152,6 +152,8 @@ function renderCustomInputs(placeholder, elementFunctions, allowMultipleInstance
152152
}, []);
153153
}
154154

155+
const formatNumber = getNumberFormatter({ useGrouping: false });
156+
155157
export default class DateTimeInput extends PureComponent {
156158
static getDerivedStateFromProps(nextProps, prevState) {
157159
const { minDate, maxDate } = nextProps;
@@ -249,9 +251,7 @@ export default class DateTimeInput extends PureComponent {
249251

250252
// eslint-disable-next-line class-methods-use-this
251253
get formatNumber() {
252-
const options = { useGrouping: false };
253-
254-
return getFormatter(options);
254+
return formatNumber;
255255
}
256256

257257
get dateDivider() {

src/shared/dateFormatter.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,49 @@
11
import getUserLocale from 'get-user-locale';
22

3+
const formatterCache = new Map();
4+
35
export function getFormatter(options) {
4-
return (locale, date) => date.toLocaleString(locale || getUserLocale(), options);
6+
return (locale, date) => {
7+
const localeWithDefault = locale || getUserLocale();
8+
9+
if (!formatterCache.has(localeWithDefault)) {
10+
formatterCache.set(localeWithDefault, new Map());
11+
}
12+
13+
const formatterCacheLocale = formatterCache.get(localeWithDefault);
14+
15+
if (!formatterCacheLocale.has(options)) {
16+
formatterCacheLocale.set(
17+
options,
18+
new Intl.DateTimeFormat(localeWithDefault, options).format,
19+
);
20+
}
21+
22+
return formatterCacheLocale.get(options)(date);
23+
};
24+
}
25+
26+
const numberFormatterCache = new Map();
27+
28+
export function getNumberFormatter(options) {
29+
return (locale, date) => {
30+
const localeWithDefault = locale || getUserLocale();
31+
32+
if (!numberFormatterCache.has(localeWithDefault)) {
33+
numberFormatterCache.set(localeWithDefault, new Map());
34+
}
35+
36+
const numberFormatterCacheLocale = numberFormatterCache.get(localeWithDefault);
37+
38+
if (!numberFormatterCacheLocale.has(options)) {
39+
numberFormatterCacheLocale.set(
40+
options,
41+
new Intl.NumberFormat(localeWithDefault, options).format,
42+
);
43+
}
44+
45+
return numberFormatterCacheLocale.get(options)(date);
46+
};
547
}
648

749
const formatDateOptions = { day: 'numeric', month: 'numeric', year: 'numeric' };

0 commit comments

Comments
 (0)