Skip to content

Instantly share code, notes, and snippets.

@fsmanuel
Created November 27, 2016 13:20
Show Gist options
  • Save fsmanuel/6abe960d131a099a2ef3628ca301d53f to your computer and use it in GitHub Desktop.
Save fsmanuel/6abe960d131a099a2ef3628ca301d53f to your computer and use it in GitHub Desktop.
Ember: Patch input type='date'
import Ember from 'ember';
export function initialize() {
// TODO: remove me https://github.com/tildeio/htmlbars/issues/472
let proto = Ember.HTMLBars.DOMHelper.prototype;
proto.setPropertyStrict = function (element, name, value) {
if (value === undefined) {
value = null;
}
if (
value === null &&
(name === 'value' || name === 'type' || name === 'src')
) {
value = '';
}
if (element.type === 'date') {
let [year] = value.split('-');
if (year.length < 4) {
return;
}
}
element[name] = value;
};
Ember.$.valHooks.date = {
set(elem, value) {
let [year, month, day] = value.split('-');
if (year.length === 4) {
elem.value = value;
return value;
}
return `${('0000' + year).slice(-4)}-${month}-${day}`;
}
}
}
export default {
name: 'patch-input-type-date',
initialize
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment