Skip to content

Commit 19b8d6f

Browse files
author
Nathan Reyes
committed
Support component imports. Closes #105
2 parents a3bb6db + 0072895 commit 19b8d6f

File tree

3 files changed

+49
-27
lines changed

3 files changed

+49
-27
lines changed

src/lib.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11
import Calendar from './components/Calendar';
22
import DatePicker from './components/DatePicker';
33
import Popover from './components/Popover';
4-
import { mergeDefaults } from './utils/defaults';
4+
import getLocaleDefaults from './utils/locales';
5+
import defaults, { mergeDefaults } from './utils/defaults';
56

67
const components = {
78
Calendar,
89
DatePicker,
910
Popover,
1011
};
1112

13+
const setupCalendar = userDefaults => {
14+
// Merge user and locale defaults with built-in defaults
15+
const locale = userDefaults
16+
? userDefaults.locale
17+
: new Intl.DateTimeFormat().resolvedOptions().locale;
18+
return mergeDefaults(defaults, getLocaleDefaults(locale), userDefaults);
19+
};
20+
1221
const VCalendar = {
1322
...components,
1423
install: (Vue, options) => {
15-
const defaults = mergeDefaults(options);
24+
// Setup plugin with options
25+
const resolvedDefaults = setupCalendar(options);
1626
Object.keys(components).forEach(k =>
17-
Vue.component(`${defaults.componentPrefix}${k}`, components[k]),
27+
Vue.component(`${resolvedDefaults.componentPrefix}${k}`, components[k]),
1828
);
1929
},
2030
};
2131

2232
export default VCalendar;
2333

24-
export { Calendar, DatePicker, Popover };
34+
export { setupCalendar, Calendar, DatePicker, Popover };
2535

2636
// Use automatically when global Vue instance detected
2737
if (typeof window !== 'undefined' && window.Vue) {

src/utils/defaults.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { POPOVER_VISIBILITIES } from './constants';
22
import { isObject, isFunction } from './typeCheckers';
3-
import setupLocale from './locales';
43

54
const defaults = {
65
componentPrefix: 'v',
@@ -127,17 +126,11 @@ const defaults = {
127126
navYearCell: null,
128127
},
129128
};
130-
// Uncomment this line when running unit tests
131-
if (process.env.NODE_ENV === 'test') setupLocale(null, defaults);
132129

133130
export default defaults;
134131

135132
export const resolveDefault = (def, args) =>
136133
(isObject(def) && def) || (isFunction(def) && def(args)) || def;
137134

138-
export const mergeDefaults = otherDefaults => {
139-
// Setup locale defaults if needed
140-
setupLocale(otherDefaults && otherDefaults.locale, defaults);
141-
// Assign the defaults
142-
return Object.assign(defaults, otherDefaults);
143-
};
135+
export const mergeDefaults = (...defaultArgs) =>
136+
Object.assign(defaults, ...defaultArgs);

src/utils/locales.js

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ const locales = {
7272
th: { L: 'DD/MM/YYYY' },
7373
// Turkish
7474
tr: { dow: 2, L: 'DD.MM.YYYY' },
75+
// Ukrainian
76+
uk: { dow: 2, L: 'DD.MM.YYYY' },
7577
};
7678
locales.en = locales['en-US'];
7779
locales.zh = locales['zh-CN'];
@@ -92,18 +94,35 @@ const getDayNames = (locale, length) => {
9294
return getWeekdayDates({ utc: true }).map(d => dtf.format(d));
9395
};
9496

95-
export default (locale, defaults) => {
96-
locale = locale || new Intl.DateTimeFormat().resolvedOptions().locale;
97-
const searchLocales = [locale, locale.substring(0, 2), 'en-US'];
98-
const matchKey = searchLocales.find(l => locales[l]);
99-
const matchValue = locales[matchKey];
100-
defaults.locale = matchKey;
101-
defaults.firstDayOfWeek = matchValue.dow || 1;
102-
defaults.dayNames = getDayNames(matchKey, 'long');
103-
defaults.dayNamesShort = getDayNames(matchKey, 'short');
104-
defaults.dayNamesShorter = defaults.dayNamesShort.map(s => s.substring(0, 2));
105-
defaults.dayNamesNarrow = getDayNames(matchKey, 'narrow');
106-
defaults.monthNames = getMonthNames(matchKey, 'long');
107-
defaults.monthNamesShort = getMonthNames(matchKey, 'short');
108-
defaults.masks = { L: matchValue.L };
97+
export default locale => {
98+
const detectedLocale = new Intl.DateTimeFormat().resolvedOptions().locale;
99+
const searchLocales = [
100+
locale,
101+
locale && locale.substring(0, 2),
102+
detectedLocale,
103+
];
104+
const resolvedLocale =
105+
searchLocales.find(l => locales[l]) || locale || detectedLocale;
106+
const localeExtra = {
107+
dow: 1,
108+
L: 'DD/MM/YYYY',
109+
...locales[resolvedLocale],
110+
};
111+
const dayNames = getDayNames(resolvedLocale, 'long');
112+
const dayNamesShort = getDayNames(resolvedLocale, 'short');
113+
const dayNamesShorter = dayNamesShort.map(s => s.substring(0, 2));
114+
const dayNamesNarrow = getDayNames(resolvedLocale, 'narrow');
115+
const monthNames = getMonthNames(resolvedLocale, 'long');
116+
const monthNamesShort = getMonthNames(resolvedLocale, 'short');
117+
return {
118+
locale: resolvedLocale,
119+
firstDayOfWeek: localeExtra.dow,
120+
masks: { L: localeExtra.L },
121+
dayNames,
122+
dayNamesShort,
123+
dayNamesShorter,
124+
dayNamesNarrow,
125+
monthNames,
126+
monthNamesShort,
127+
};
109128
};

0 commit comments

Comments
 (0)