Skip to content

Commit 7c55a26

Browse files
committed
fix(filter): Date Filters using Flatpickr throw error w/invalid locale
- fixes #346 - when using a locale that Flatpickr doesn't support, like "ua", it should use "en" (English) as default locale. - it will display a console warning that this locale is not supported but not throw an error (unless user set a different locale, see next) - user can choose to override the locale by setting locale to use via `filterOptions: { locale: 'en' }`
1 parent 93229f6 commit 7c55a26

File tree

4 files changed

+74
-12
lines changed

4 files changed

+74
-12
lines changed

src/app/modules/angular-slickgrid/filters/__tests__/compoundDateFilter.spec.ts

+22
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,28 @@ describe('CompoundDateFilter', () => {
245245
expect(selectonOptionElms[0].textContent).toBe('janvier');
246246
});
247247

248+
it('should throw an error and use English locale when user tries to load an unsupported Flatpickr locale', () => {
249+
translate.use('zx');
250+
const consoleSpy = jest.spyOn(global.console, 'warn').mockReturnValue();
251+
252+
filterArguments.searchTerms = ['2000-01-01T05:00:00.000Z'];
253+
mockColumn.filter.operator = '<=';
254+
255+
filter.init(filterArguments);
256+
const filterInputElm = divContainer.querySelector<HTMLInputElement>('.search-filter.filter-finish .flatpickr input.flatpickr');
257+
const calendarElm = document.body.querySelector<HTMLDivElement>('.flatpickr-calendar');
258+
const selectonOptionElms = calendarElm.querySelectorAll<HTMLSelectElement>(' .flatpickr-monthDropdown-months option');
259+
260+
filter.show();
261+
262+
filterInputElm.focus();
263+
filterInputElm.dispatchEvent(new (window.window as any).KeyboardEvent('keyup', { keyCode: 97, bubbles: true, cancelable: true }));
264+
265+
expect(consoleSpy).toHaveBeenCalledWith(expect.toInclude('[Angular-Slickgrid - CompoundDate Filter] It seems that "zx" is not a locale supported by Flatpickr'));
266+
expect(selectonOptionElms.length).toBe(12);
267+
expect(selectonOptionElms[0].textContent).toBe('January');
268+
});
269+
248270
it('should trigger a callback with the clear filter set when calling the "clear" method', () => {
249271
filterArguments.searchTerms = ['2000-01-01T05:00:00.000Z'];
250272
const spyCallback = jest.spyOn(filterArguments, 'callback');

src/app/modules/angular-slickgrid/filters/__tests__/dateRangeFilter.spec.ts

+22
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,28 @@ describe('DateRangeFilter', () => {
235235
expect(selectonOptionElms[0].textContent).toBe('janvier');
236236
});
237237

238+
it('should throw an error and use English locale when user tries to load an unsupported Flatpickr locale', () => {
239+
translate.use('zx');
240+
const consoleSpy = jest.spyOn(global.console, 'warn').mockReturnValue();
241+
242+
filterArguments.searchTerms = ['2000-01-01T05:00:00.000Z', '2000-01-31T05:00:00.000Z'];
243+
mockColumn.filter.operator = 'RangeInclusive';
244+
245+
filter.init(filterArguments);
246+
const filterInputElm = divContainer.querySelector<HTMLInputElement>('input.flatpickr.search-filter.filter-finish');
247+
const calendarElm = document.body.querySelector<HTMLDivElement>('.flatpickr-calendar');
248+
const selectonOptionElms = calendarElm.querySelectorAll<HTMLSelectElement>(' .flatpickr-monthDropdown-months option');
249+
250+
filter.show();
251+
252+
filterInputElm.focus();
253+
filterInputElm.dispatchEvent(new (window.window as any).KeyboardEvent('keyup', { keyCode: 97, bubbles: true, cancelable: true }));
254+
255+
expect(consoleSpy).toHaveBeenCalledWith(expect.toInclude('[Angular-Slickgrid - DateRange Filter] It seems that "zx" is not a locale supported by Flatpickr'));
256+
expect(selectonOptionElms.length).toBe(12);
257+
expect(selectonOptionElms[0].textContent).toBe('January');
258+
});
259+
238260
it('should trigger a callback with the clear filter set when calling the "clear" method', () => {
239261
filterArguments.searchTerms = ['2000-01-01T05:00:00.000Z', '2000-01-31T05:00:00.000Z'];
240262
const spyCallback = jest.spyOn(filterArguments, 'callback');

src/app/modules/angular-slickgrid/filters/compoundDateFilter.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ export class CompoundDateFilter implements Filter {
167167
private buildDatePickerInput(searchTerm?: SearchTerm) {
168168
const inputFormat = mapFlatpickrDateFormatWithFieldType(this.columnDef.type || FieldType.dateIso);
169169
const outputFormat = mapFlatpickrDateFormatWithFieldType(this.columnDef.outputType || this.columnDef.type || FieldType.dateUtc);
170-
let currentLocale = this.translate && this.translate.currentLang || 'en';
170+
const userFilterOptions = (this.columnFilter && this.columnFilter.filterOptions || {}) as FlatpickrOption;
171+
172+
// get current locale, if user defined a custom locale just use or get it the Translate Service if it exist else just use English
173+
let currentLocale = (userFilterOptions && userFilterOptions.locale) || (this.translate && this.translate.currentLang) || 'en';
171174
if (currentLocale && currentLocale.length > 2) {
172175
currentLocale = currentLocale.substring(0, 2);
173176
}
@@ -205,7 +208,7 @@ export class CompoundDateFilter implements Filter {
205208
}
206209

207210
// merge options with optional user's custom options
208-
this._flatpickrOptions = { ...pickerOptions, ...(this.columnFilter.filterOptions as FlatpickrOption) };
211+
this._flatpickrOptions = { ...pickerOptions, ...userFilterOptions };
209212

210213
let placeholder = (this.gridOptions) ? (this.gridOptions.defaultFilterPlaceholder || '') : '';
211214
if (this.columnFilter && this.columnFilter.placeholder) {
@@ -295,10 +298,16 @@ export class CompoundDateFilter implements Filter {
295298
private loadFlatpickrLocale(language: string) {
296299
let locales = 'en';
297300

298-
if (language !== 'en') {
299-
// change locale if needed, Flatpickr reference: https://chmln.github.io/flatpickr/localization/
300-
const localeDefault: any = require(`flatpickr/dist/l10n/${language}.js`).default;
301-
locales = (localeDefault && localeDefault[language]) ? localeDefault[language] : 'en';
301+
try {
302+
if (language !== 'en') {
303+
// change locale if needed, Flatpickr reference: https://chmln.github.io/flatpickr/localization/
304+
const localeDefault: any = require(`flatpickr/dist/l10n/${language}.js`).default;
305+
locales = (localeDefault && localeDefault[language]) ? localeDefault[language] : 'en';
306+
}
307+
} catch (e) {
308+
console.warn(`[Angular-Slickgrid - CompoundDate Filter] It seems that "${language}" is not a locale supported by Flatpickr, we will use "en" instead. `
309+
+ `To avoid seeing this message, you can specifically set "filter: { filterOptions: { locale: 'en' } }" in your column definition.`);
310+
return 'en';
302311
}
303312
return locales;
304313
}

src/app/modules/angular-slickgrid/filters/dateRangeFilter.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,10 @@ export class DateRangeFilter implements Filter {
169169
const columnId = this.columnDef && this.columnDef.id;
170170
const inputFormat = mapFlatpickrDateFormatWithFieldType(this.columnDef.type || FieldType.dateIso);
171171
const outputFormat = mapFlatpickrDateFormatWithFieldType(this.columnDef.outputType || this.columnDef.type || FieldType.dateUtc);
172-
let currentLocale = this.translate && this.translate.currentLang || 'en';
172+
const userFilterOptions = (this.columnFilter && this.columnFilter.filterOptions || {}) as FlatpickrOption;
173+
174+
// get current locale, if user defined a custom locale just use or get it the Translate Service if it exist else just use English
175+
let currentLocale = (userFilterOptions && userFilterOptions.locale) || (this.translate && this.translate.currentLang) || 'en';
173176
if (currentLocale.length > 2) {
174177
currentLocale = currentLocale.substring(0, 2);
175178
}
@@ -220,7 +223,7 @@ export class DateRangeFilter implements Filter {
220223
}
221224

222225
// merge options with optional user's custom options
223-
this._flatpickrOptions = { ...pickerOptions, ...(this.columnFilter.filterOptions as FlatpickrOption) };
226+
this._flatpickrOptions = { ...pickerOptions, ...userFilterOptions };
224227

225228
let placeholder = (this.gridOptions) ? (this.gridOptions.defaultFilterPlaceholder || '') : '';
226229
if (this.columnFilter && this.columnFilter.placeholder) {
@@ -272,10 +275,16 @@ export class DateRangeFilter implements Filter {
272275
private loadFlatpickrLocale(language: string) {
273276
let locales = 'en';
274277

275-
if (language !== 'en') {
276-
// change locale if needed, Flatpickr reference: https://chmln.github.io/flatpickr/localization/
277-
const localeDefault: any = require(`flatpickr/dist/l10n/${language}.js`).default;
278-
locales = (localeDefault && localeDefault[language]) ? localeDefault[language] : 'en';
278+
try {
279+
if (language !== 'en') {
280+
// change locale if needed, Flatpickr reference: https://chmln.github.io/flatpickr/localization/
281+
const localeDefault: any = require(`flatpickr/dist/l10n/${language}.js`).default;
282+
locales = (localeDefault && localeDefault[language]) ? localeDefault[language] : 'en';
283+
}
284+
} catch (e) {
285+
console.warn(`[Angular-Slickgrid - DateRange Filter] It seems that "${language}" is not a locale supported by Flatpickr, we will use "en" instead. `
286+
+ `To avoid seeing this message, you can specifically set "filter: { filterOptions: { locale: 'en' } }" in your column definition.`);
287+
return 'en';
279288
}
280289
return locales;
281290
}

0 commit comments

Comments
 (0)