Skip to content

Commit

Permalink
Translating component depending on moment locale. Added translations …
Browse files Browse the repository at this point in the history
…for buttons too.
  • Loading branch information
Luisetelo committed May 26, 2017
1 parent c82fc3a commit 6ec72fe
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 29 deletions.
33 changes: 19 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Material Datetime Picker
# Material Datetime Picker

A Material Design date/time picker modal, built for the web. Works well with Materialize, or standalone.

[https://ripjar.github.io/material-datetime-picker/](https://ripjar.github.io/material-datetime-picker/)

[![Status][status]](https://travis-ci.org/ripjar/material-datetime-picker)
[![Status][status]](https://travis-ci.org/ripjar/material-datetime-picker)
[![Package][npm]](https://www.npmjs.com/package/material-datetime-picker)

![Time][date] ![Time][time]
Expand All @@ -23,7 +23,7 @@ The picker depends on Google's Material Design icons (packaged with Materialize)
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
```

---
---

For best results also include Google's Material Font `Roboto`;

Expand All @@ -47,7 +47,7 @@ If you want to use this project as a standalone `<script>`, you can use `dist/m
### Manual (recommended)

The picker can be instantiated and interacted with manally;

```javascript
import MaterialDateTimePicker from 'material-datetime-picker';

Expand All @@ -57,15 +57,15 @@ const picker = new MaterialDateTimePicker()
.on('close', () => console.log('closed'));

document.querySelector('.c-datepicker-btn')
.on('click', () => picker.open());
.on('click', () => picker.open());
```

---

### As form input

The picker is decoupled from any single form element for simplicity. However, it should be simple to link the picker to a form input or button. For instance, given the input element `<input class="c-datepicker-input" />`, the following could be written;

```javascript
import MaterialDateTimePicker from 'material-datetime-picker';

Expand All @@ -75,28 +75,28 @@ const picker = new MaterialDatePicker()
input.value = val.format("DD/MM/YYYY");
});

input.addEventListener('focus', () => picker.open());
input.addEventListener('focus', () => picker.open());
```

## Options

All options are optional, including the `el`.

```javascript
{
// DOM Element to attach the datepicker. This element will receive
// events when the data changes. If an input element, will be
// DOM Element to attach the datepicker. This element will receive
// events when the data changes. If an input element, will be
// populated with formatted date and time chosen.
// `el` must be a DOM Element object. Selectpr strings or wrappers
// like a jQuery selection are not supported.
el: document.querySelector('.c-datepicker-btn'),
// if `el` is set, the format used to display the datetime in the input,
format: 'DD/MM/YY',
// if `el` is set, the format used to display the datetime in the input,
format: 'DD/MM/YY',
// the default value of the picker
default: moment(),
// the container to append the picker. If you change this, you need to make
// sure your element has a z-index > 0 so that it displays in front of the scrim.
container: document.body,
container: document.body,
// cosmetic classes that can be overriden
// mostly used for styling the calendar
styles: {
Expand All @@ -123,7 +123,12 @@ All options are optional, including the `el`.
clockNum: 'c-datepicker__clock__num'
},
// date range to allow (see rome validator factories)
dateValidator: null
dateValidator: null
// translations for buttons label
okLabel: 'OK',
cancelLabel: 'Cancel'
// format to show the day number.
dayFormat: 'Do',
}
```

Expand Down
24 changes: 13 additions & 11 deletions lib/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import '../scss/material-datetime-picker.scss';

const prefix = 'c-datepicker';
const defaults = () => ({
default: moment().startOf('hour'),
default: moment(),
// allow the user to override all the classes
// used for styling the calendar
styles: {
Expand Down Expand Up @@ -40,7 +40,10 @@ const defaults = () => ({
// the container to append the picker
container: document.body,
// allow any dates
dateValidator: undefined
dateValidator: undefined,
okLabel: 'OK',
cancelLabel: 'Cancel',
dayFormat: 'Do'
});

class DateTimePicker extends Events {
Expand Down Expand Up @@ -70,15 +73,17 @@ class DateTimePicker extends Events {
styles: this.options.styles,
time: false,
dateValidator: validator,
initialValue: this.value
initialValue: this.options.default,
weekdayFormat: this.options.default.localeData().weekdaysMin(),
weekStart: this.options.default.clone().weekday(0).day()
}).on('data', onData);
}

// called to open the picker
open() {
const scrimEl = scrimTemplate(this.options);
_appendTemplate(document.body, scrimEl);
_appendTemplate(this.options.container, popupTemplate());
_appendTemplate(this.options.container, popupTemplate(this.options.okLabel, this.options.cancelLabel));
this.pickerEl = this.options.container.querySelector(`.${prefix}`);
this.scrimEl = document.body.querySelector(`.${this.options.styles.scrim}`);
this.amToggleEl = this.$('.c-datepicker__clock--am');
Expand All @@ -89,10 +94,7 @@ class DateTimePicker extends Events {
// set/setDate/setTime need refactoring to have single concerns
// (set: set the value; setDate/setTime rename to renderDate/renderTime
// and deal with updating the view only).
// For now this allows us to set the default time using the same quantize
// rules as setting the date explicitly. Setting this.value meets setTime|Date's
// expectation that we have a value, and `0` guarantees that we will detect
this.value = moment(0);
this.value = this.options.default.startOf('hour');
this.setDate(this.options.default);
this.setTime(this.options.default);
}
Expand Down Expand Up @@ -311,8 +313,8 @@ class DateTimePicker extends Events {
) {
this.setDate(m);
evts.push('change:date');
}
}

if (m.hour() !== this.value.hour()
|| m.minutes() !== this.value.minutes()
) {
Expand Down Expand Up @@ -342,7 +344,7 @@ class DateTimePicker extends Events {
setDate(date) {
const m = moment(date);
const month = m.format('MMM');
const day = m.format('Do');
const day = m.format(this.options.dayFormat);
const dayOfWeek = m.format('dddd');
const year = m.format('YYYY');

Expand Down
6 changes: 3 additions & 3 deletions lib/template/datepicker.template.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default () => `
export default (okLabel, cancelLabel) => `
<div class="c-datepicker">
<a class="c-datepicker__toggle c-datepicker__toggle--right c-datepicker--show-time js-show-clock" title="show time picker">
</a>
Expand Down Expand Up @@ -69,8 +69,8 @@ export default () => `
</div>
</div>
<div class="modal-btns">
<a class="c-btn c-btn--flat js-cancel">Cancel</a>
<a class="c-btn c-btn--flat js-ok">OK</a>
<a class="c-btn c-btn--flat js-cancel">${cancelLabel}</a>
<a class="c-btn c-btn--flat js-ok">${okLabel}</a>
</div>
</div>
`;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
],
"license": "MIT",
"dependencies": {
"moment": "2.10.6",
"moment": "2.18.1",
"rome": "https://github.com/jwhitfieldseed/rome.git#19f5d3031a922c29c52b9038b2832a827e5e99d6"
},
"devDependencies": {
Expand Down

0 comments on commit 6ec72fe

Please sign in to comment.