Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
pass locale via props. use js toLocaleDateString()
Browse files Browse the repository at this point in the history
  • Loading branch information
misteinb committed Aug 14, 2018
1 parent 10ab83c commit 7b28f58
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## v3.0.3
### Fixed
- screen reader support for calendar
- take in props to override browser locale for calendar

## v3.0.2
### Fixed
Expand Down
10 changes: 5 additions & 5 deletions lib/components/DateTime/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export interface CalendarAttributes {
export interface CalendarProps extends React.Props<CalendarComponentType> {
/** Current selected date */
value?: Date | string;
/** i18n locale */
locale?: string;

/** Year to display (otherwise shows the year from value) */
year?: number;
Expand Down Expand Up @@ -84,7 +86,6 @@ export class Calendar extends React.Component<CalendarProps, Partial<CalendarSta


constructor(props: CalendarProps) {
const locale = navigator['userLanguage'] || (navigator.language || 'en-us');
super(props);

if (typeof (this.props.value) === 'string') {
Expand All @@ -108,9 +109,9 @@ export class Calendar extends React.Component<CalendarProps, Partial<CalendarSta
detached: false
};

this.monthNames = getLocalMonths(locale);
this.monthNames = getLocalMonths(this.props.locale);

this.dayNames = getLocalWeekdays(locale);
this.dayNames = getLocalWeekdays(this.props.locale);

this.onPrevMonth = this.onPrevMonth.bind(this);
this.onNextMonth = this.onNextMonth.bind(this);
Expand Down Expand Up @@ -280,7 +281,6 @@ export class Calendar extends React.Component<CalendarProps, Partial<CalendarSta

const curYear = this.state.currentDate.year;
const curMonth = this.state.currentDate.month;
const locale = navigator['userLanguage'] || (navigator.language || 'en-us');

const weekdays = this.dayNames.map(day => {
return (
Expand Down Expand Up @@ -325,7 +325,7 @@ export class Calendar extends React.Component<CalendarProps, Partial<CalendarSta
const date = col.date;
const colMonth = col.month;
const key = `${colMonth}-${date}`;
const ariaLabel = `${date} ${this.monthNames[colMonth]} ${curYear}`;
const ariaLabel = new Date(`${curYear}-${colMonth + 1}-${date}`).toLocaleDateString(this.props.locale, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' });

/** Grayed out day from another month */
if (colMonth !== curMonth) {
Expand Down
3 changes: 3 additions & 0 deletions lib/components/DateTime/DateField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export interface DateFieldProps extends React.Props<DateFieldType> {
* Default: true
*/
localTimezone?: boolean;
/** i18n locale */
locale?: string;
/**
* Show Calendar below date picker input
*/
Expand Down Expand Up @@ -114,6 +116,7 @@ export const DateField: React.StatelessComponent<DateFieldProps> = (props: DateF
<DatePicker
name={props.name}
initialValue={props.initialValue}
locale={props.locale}
localTimezone={props.localTimezone}
tabIndex={props.tabIndex}
showAbove={props.showAbove}
Expand Down
4 changes: 4 additions & 0 deletions lib/components/DateTime/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export interface DatePickerProps extends React.Props<DatePickerType> {
* Default: true
*/
localTimezone?: boolean;

/** i18n locale */
locale?: string;
/**
* Show Calendar below date picker input
*/
Expand Down Expand Up @@ -446,6 +449,7 @@ export class DatePicker extends React.Component<DatePickerProps, Partial<DatePic
className={css('date-picker-calendar')}
year={parsed.year || null}
month={parsed.month - 1}
locale={this.props.locale}
attr={this.props.attr.calendar}
/>
<div className={css('date-picker-dropdown-triangle')}></div>
Expand Down
3 changes: 3 additions & 0 deletions lib/components/DateTime/DateTimeField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export interface DateTimeFieldProps extends React.Props<DateTimeFieldType> {
* Default: true
*/
localTimezone?: boolean;
/** i18n locale */
locale?: string;
/**
* Show Calendar below date picker input
*/
Expand Down Expand Up @@ -370,6 +372,7 @@ export class DateTimeField extends React.Component<DateTimeFieldProps, Partial<D
tabIndex={this.props.tabIndex}
error={!!this.props.error}
disabled={this.props.disabled}
locale={this.props.locale}
localTimezone={this.props.localTimezone}
showAbove={this.props.showAbove}
format={this.props.format}
Expand Down

0 comments on commit 7b28f58

Please sign in to comment.