Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(b-calendar, b-form-datepicker): allow customization of in-component displayed date format (closes #4797) #4835

Merged
merged 34 commits into from Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
552476b
feat(b-calendar): made date format to be configurable
naletups Feb 26, 2020
3783f38
feat(b-form-datepicker): Added format option for display of formatted…
naletups Feb 26, 2020
3f5ad8d
docs(b-calendar): Updated prop list
naletups Feb 26, 2020
3064651
docs(b-form-datepicker): Updated prop list
naletups Feb 26, 2020
d43d867
Fixed prop default value
naletups Feb 26, 2020
eecfa61
fix(b-form-datepicker): prop default value
naletups Feb 26, 2020
ad3916b
Merge branch 'dev' into naletups-dateformat-string
tmorehouse Feb 26, 2020
7653f08
docs(b-calendar): change prop name and referenced default format
naletups Feb 26, 2020
7c30f34
docs(b-form-datepicker): change prop name and referenced default format
naletups Feb 26, 2020
7a0fd6c
fix(b-calendar): rename prop and enforce gregorian calendar
naletups Feb 26, 2020
bb93ab3
fix(b-form-datepicker): rename prop in component and in forwarding to…
naletups Feb 26, 2020
1ca5122
chore(b-calendar): file formating
naletups Feb 26, 2020
c5ffb70
docs(b-calendar): add styling subsection on new prop
naletups Feb 26, 2020
6c3f2a1
Fixed referenced filename
naletups Feb 26, 2020
d126a80
docs(b-form-datepicker): add styling subsection on new prop
naletups Feb 26, 2020
39a2f34
Removed fixed calendar type from prop dateFormatOptions
naletups Feb 26, 2020
b33a799
Removed fixed calendar type from prop dateFormatOptions
naletups Feb 26, 2020
c7adf94
Update package.json
tmorehouse Feb 26, 2020
a3279e1
Update package.json
tmorehouse Feb 26, 2020
9af4e94
Update README.md
tmorehouse Feb 26, 2020
933d479
Update README.md
tmorehouse Feb 26, 2020
2222e00
Update calendar.js
tmorehouse Feb 26, 2020
5bf14e2
Update README.md
tmorehouse Feb 26, 2020
f9de5e1
Update README.md
tmorehouse Feb 26, 2020
741fd17
Update README.md
tmorehouse Feb 26, 2020
5074c97
Update README.md
tmorehouse Feb 26, 2020
0d4bf03
Update README.md
tmorehouse Feb 26, 2020
d18bb5c
Update README.md
tmorehouse Feb 26, 2020
c234d66
Update README.md
tmorehouse Feb 26, 2020
98245eb
Update README.md
tmorehouse Feb 26, 2020
d3fbb3c
Merge branch 'dev' into naletups-dateformat-string
tmorehouse Feb 26, 2020
622b7f4
Update README.md
jacobmllr95 Feb 26, 2020
0061018
Minor tweaks
jacobmllr95 Feb 26, 2020
d65c796
Enhance docs
jacobmllr95 Feb 26, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 46 additions & 2 deletions src/components/calendar/README.md
Expand Up @@ -230,12 +230,56 @@ fit the width of the parent element. The `width` prop has no effect when `block`
Note it is _not recommended_ to set a width below `260px`, otherwise truncation and layout issues
with the component may occur.

### Date string format

<span class="badge badge-info small">v2.6.0+</span>

To change format options of the displayed date text inside the component, e.g. in the header, set
the `dateFormatOptions` prop to an object containing the requested format properties for the
`Intl.DateTimeFormat` object (see also [Internationalization](#internationalization)).

```html
<template>
<div>
<p>Custom date format:</p>
<b-calendar
:dateFormatOptions="{ year: 'numeric', month: 'short', day: '2-digit', weekday: 'short' }"
locale="en"
></b-calendar>
<p class="mt-3">Short date format:</p>
<b-calendar
:dateFormatOptions="{ year: 'numeric', month: 'numeric', day: 'numeric' }"
locale="en"
></b-calendar>
</div>
</template>

<!-- b-calendar-dateformat.vue -->
```

The following table summarizes the valid options for each format property:

| Property | Possible values |
| --------- | ------------------------------------------------------------ |
| `year` | `'numeric'`, or `'2-digit'` |
| `month` | `'numeric'`, `'2-digit'`, `'long'`, `'short'`, or `'narrow'` |
| `day` | `'numeric'`, or `'2-digit'` |
| `weekday` | `'long'`, `'short'`, or `'narrow'` |

Notes:

- leaving out certain options may affect the formatted text string, e.g. the `weekday`.
- the formatted value will vary according to the resolved locale. Some locales may not support the
`'narrow'` format and will fall back to `'short'` or `long'` (if `'short'` is not available)
- `year`, `month` and `day` will always be shown. If you need to leave out a value, set the property
to `undefined`, although this is highly discouraged for accessibility reasons

### Hiding the top selected date header

By default, the current selected date will be displayed at the top of the calendar component,
formatted in the locale's language.

You can hide this header via the `hide-header` prop. Note this only visually hides the selected
You can hide this header via the `hide-header` prop. Note this only _visually hides_ the selected
date, while keeping it available to screen reader users as an `aria-live` region.

### Border and padding
Expand Down Expand Up @@ -400,7 +444,7 @@ properties:
| `selectedFormatted` | The selected date formatted in the current locale. If no date is selected, this will be the value of the `label-no-date-selected` prop |
| `activeYMD` | The current date of the calendar day button that can receive focus as a string (`YYYY-MM-DD` format) |
| `activeDate` | The current date of the calendar day button that can receive focus as a `Date` object |
| `activeFormated` | The active date formatted in the current locale |
| `activeFormatted` | The active date formatted in the current locale |
| `disabled` | Will be `true` if active date is disabled, `false` otherwise |
| `locale` | The resolved locale (may not be the same as the requested locale) |
| `calendarLocale` | The resolved locale used by the calendar, optionally including the calendar type (i.e. 'gregory'). Usually this will be the same as `locale`, but may include the calendar type used, such as `fa-u-ca-gregory` when selecting the Persian locale (`'fa'`) |
Expand Down
25 changes: 22 additions & 3 deletions src/components/calendar/calendar.js
Expand Up @@ -224,6 +224,16 @@ export const BCalendar = Vue.extend({
labelHelp: {
type: String,
default: () => getComponentConfig(NAME, 'labelHelp')
},
dateFormatOptions: {
// `Intl.DateTimeFormat` object
type: Object,
default: () => ({
year: 'numeric',
month: 'long',
day: 'numeric',
weekday: 'long'
})
}
},
data() {
Expand Down Expand Up @@ -372,10 +382,19 @@ export const BCalendar = Vue.extend({
formatDateString() {
// Returns a date formatter function
return createDateFormatter(this.calendarLocale, {
// Ensure we have year, month, day shown for screen readers/ARIA
// If users really want to leave one of these out, they can
// pass `undefined` for the property value
year: 'numeric',
month: 'long',
day: 'numeric',
weekday: 'long',
month: '2-digit',
day: '2-digit',
// Merge in user supplied options
...this.dateFormatOptions,
// Ensure hours/minutes/seconds are not shown
hour: undefined,
minute: undefined,
second: undefined,
// Ensure calendar is gregorian
calendar: 'gregory'
})
},
Expand Down
5 changes: 5 additions & 0 deletions src/components/calendar/package.json
Expand Up @@ -135,6 +135,11 @@
{
"prop": "labelHelp",
"description": "Help text that appears at the bottom of the calendar grid"
},
{
"prop": "dateFormatOptions",
"version": "2.6.0",
"description": "Format object for displayed text string that is passed to `Intl.DateTimeFormat`"
}
],
"events": [
Expand Down
47 changes: 47 additions & 0 deletions src/components/form-datepicker/README.md
Expand Up @@ -301,6 +301,53 @@ and usage of these props.
Want a fancy popup with a dark background instead of a light background? Set the `dark` prop to
`true` to enable the dark background.

### Date string format

<span class="badge badge-info small">v2.6.0+</span>

To change format options of the displayed date text inside the component, e.g. in the header or
placeholder, set the `date-format-options` prop to an object containing the requested format
properties for the `Intl.DateTimeFormat` object (see also
[Internationalization](#internationalization)).

```html
<template>
<div>
<label for="datepicker-dateformat1">Custom date format</label>
<b-form-datepicker
id="datepicker-dateformat1"
:dateFormatOptions="{ year: 'numeric', month: 'short', day: '2-digit', weekday: 'short' }"
locale="en"
></b-form-datepicker>
<label for="datepicker-dateformat2">Short date format</label>
<b-form-datepicker
id="datepicker-dateformat2"
:dateFormatOptions="{ year: 'numeric', month: 'numeric', day: 'numeric' }"
locale="en"
></b-form-datepicker>
</div>
</template>

<!-- b-form-datepicker-dateformat.vue -->
```

The following table summarizes the valid options for each format property:

| Property | Possible values |
| --------- | ------------------------------------------------------------ |
| `year` | `'numeric'`, or `'2-digit'` |
| `month` | `'numeric'`, `'2-digit'`, `'long'`, `'short'`, or `'narrow'` |
| `day` | `'numeric'`, or `'2-digit'` |
| `weekday` | `'long'`, `'short'`, or `'narrow'` |

Notes:

- leaving out certain options may affect the formatted text string, e.g. the `weekday`.
- the formatted value will vary according to the resolved locale. Some locales may not support the
`'narrow'` format and will fall back to `'short'` or `long'` (if `'short'` is not available)
- `year`, `month` and `day` will always be shown. If you need to leave out a value, set the property
to `undefined`, although this is highly discouraged for accessibility reasons

## Internationalization

Internationalization of the date picker's calendar is provided via
Expand Down
15 changes: 13 additions & 2 deletions src/components/form-datepicker/form-datepicker.js
Expand Up @@ -208,6 +208,16 @@ const propsMixin = {
dark: {
type: Boolean,
default: false
},
dateFormatOptions: {
// `Intl.DateTimeFormat` object
type: Object,
default: () => ({
year: 'numeric',
month: 'long',
day: 'numeric',
weekday: 'long'
})
}
}
}
Expand Down Expand Up @@ -248,7 +258,7 @@ export const BFormDatepicker = /*#__PURE__*/ Vue.extend({
return this.activeYMD.slice(0, -3)
},
calendarProps() {
// We alis `this` to `self` for better minification
// We alias `this` to `self` for better minification
const self = this
// TODO: Make the ID's computed props
const idLabel = self.safeId('_value_')
Expand Down Expand Up @@ -280,7 +290,8 @@ export const BFormDatepicker = /*#__PURE__*/ Vue.extend({
labelNoDateSelected: self.labelNoDateSelected,
labelCalendar: self.labelCalendar,
labelNav: self.labelNav,
labelHelp: self.labelHelp
labelHelp: self.labelHelp,
dateFormatOptions: self.dateFormatOptions
}
},
computedResetValue() {
Expand Down
5 changes: 5 additions & 0 deletions src/components/form-datepicker/package.json
Expand Up @@ -208,6 +208,11 @@
{
"prop": "labelCloseButton",
"description": "Content for the optional `Close` button"
},
{
"prop": "dateFormatOptions",
"version": "2.6.0",
"description": "Format object for displayed text string that is passed to `Intl.DateTimeFormat`"
}
],
"events": [
Expand Down