Skip to content

Commit

Permalink
Add formatDay() to LocaleUtils
Browse files Browse the repository at this point in the history
This is required to add the aria-label attribute. See #132 for more details.
  • Loading branch information
gpbl committed Apr 8, 2016
1 parent f584889 commit 0f99bdb
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 2 deletions.
8 changes: 6 additions & 2 deletions docs/LocaleUtils.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# LocaleUtils

`LocaleUtils` is a set of functions used to localize the component (see: [Custom localization](LocalizationCustom.md)). You may want to implement your own `LocaleUtils`, or override some of its functions.
`LocaleUtils` is a set of functions used to localize the component (see: [Custom localization](LocalizationCustom.md)). You may want to implement your own `LocaleUtils`, or override some of its functions.

For example, this code renders the month's title as `M/YYYY` instead of the default:

```jsx
import DayPicker, { LocaleUtils } from "react-day-picker";

function formatMonthTitle(d, locale) {
return `${d.getMonth() + 1}/${d.getFullYear}`
return `${d.getMonth() + 1}/${d.getFullYear}`
}

<DayPicker localeUtils={{ ...LocaleUtils, formatMonthTitle }} />
Expand All @@ -17,6 +17,10 @@ function formatMonthTitle(d, locale) {

## Functions

### `formatDay(d: date, locale: string) -> string`

Format the string used as `aria-label` for the given day.

### `formatMonthTitle(d: date, locale: string) -> string`

Return the string used to format the month's title in the day picker for the given date `d`.
Expand Down
1 change: 1 addition & 0 deletions docs/LocalizationCustom.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const FIRST_DAY = {
}

const localeUtils = {
formatDay: (d, locale="en") => `${weekdaysLong[locale][d.getDay()]}, ${d.getDate()} ${months[locale][d.getMonth()]} ${d.getFullYear()}`,
formatMonthTitle: (d, locale) => `${MONTHS[locale][d.getMonth()]} ${d.getFullYear()}`,
formatWeekdayShort: (i, locale) => WEEKDAYS_SHORT[locale][i],
formatWeekdayLong: (i, locale) => WEEKDAYS_LONG[locale][i],
Expand Down
2 changes: 2 additions & 0 deletions examples/src/examples/LocalizedCustom.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const firstDayOfWeek = {
};

const localeUtils = {
formatDay: (d, locale="en") =>
`${weekdaysLong[locale][d.getDay()]}, ${d.getDate()} ${months[locale][d.getMonth()]} ${d.getFullYear()}`,
formatWeekdayShort: (index, locale="en") => weekdaysShort[locale][index],
formatWeekdayLong: (index, locale="en") => weekdaysLong[locale][index],
getFirstDayOfWeek: (locale) => firstDayOfWeek[locale],
Expand Down
5 changes: 5 additions & 0 deletions src/LocaleUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const WEEKDAYS_SHORT = ["Su", "Mo", "Tu",
const MONTHS = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];

export function formatDay(day) {
return day.toDateString();
}

export function formatMonthTitle(d) {
return `${MONTHS[d.getMonth()]} ${d.getFullYear()}`;
}
Expand All @@ -28,6 +32,7 @@ export function getMonths() {
}

export default {
formatDay,
formatMonthTitle,
formatWeekdayShort,
formatWeekdayLong,
Expand Down
5 changes: 5 additions & 0 deletions src/addons/MomentLocaleUtils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import moment from "moment";

export function formatDay(day, locale="en") {
return moment(day).locale(locale).format("ddd ll");
}

export function formatMonthTitle(date, locale="en") {
return moment(date).locale(locale).format("MMMM YYYY");
}
Expand Down Expand Up @@ -27,6 +31,7 @@ export function getMonths(locale="en") {
}

export default {
formatDay,
formatMonthTitle,
formatWeekdayShort,
formatWeekdayLong,
Expand Down

0 comments on commit 0f99bdb

Please sign in to comment.