Skip to content

Commit

Permalink
new implementation of PikadayInput has all tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
ef4 committed Jan 18, 2022
1 parent 6eb273e commit ceb8c42
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 13 deletions.
5 changes: 5 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"experimentalDecorators": true
}
}
1 change: 1 addition & 0 deletions src/components/pikaday-input2.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<input
{{pikaday
@options
value=this.value
onSelect=this.onSelect
setDefaultDate=true
Expand Down
10 changes: 4 additions & 6 deletions src/components/pikaday-input2.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ export default class extends Component {
}

get value() {
if (this.args.useUTC && this.args.value) {
let { value, useUTC } = this.args;
if (useUTC && value) {
let format = 'YYYY-MM-DD';
return moment(
moment.utc(this.args.value).format(format),
format
).toDate();
value = moment(moment.utc(value).format(format), format).toDate();
}
return this.args.value;
return value;
}

get yearRange() {
Expand Down
53 changes: 47 additions & 6 deletions src/modifiers/pikaday.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,56 @@ export default class PikadayModifier extends Modifier {
#pikaday;
#observer;

didInstall() {
get pikadayOptions() {
let opts = {
// Our element is Pikaday's field
field: this.element,

// All other named arguments go through to Pikaday
...this.args.named,

// We also optionally accept a bag of arguments as the first positional
// argument, because it's hard to do argument splatting in hbs. These are
// taking precedence over the named arguments.
...this.args.positional[0],
};

if (!opts.i18n) {
// Pikaday doesn't like it if you pass an empty value for i18n, whereas
// it's hard in HBS to only conditionally include a named argument, so we
// drop it here.
delete opts.i18n;
}
this.#pikaday = new Pikaday(opts);
this.#pikaday.setDate(this.args.named.value, true);
return opts;
}

didInstall() {
this.#pikaday = new Pikaday(this.pikadayOptions);
let { value } = this.args.named;
if (value) {
this.#pikaday.setDate(value, true);
}
this.#observer = new MutationObserver(this.sawMutation.bind(this));
this.#observer.observe(this.element, { attributes: true });
}

didUpdateArguments() {
this.#pikaday.setMinDate(this.args.named.minDate);
this.#pikaday.setMaxDate(this.args.named.maxDate);
this.#pikaday.setDate(this.args.named.value, true);
let { value, minDate, maxDate } = this.args.named;
let valueAltered = false;
this.#pikaday.setMinDate(copyDate(minDate));
if (minDate && value && value < minDate) {
value = minDate;
valueAltered = true;
}

this.#pikaday.setMaxDate(copyDate(maxDate));
if (maxDate && value && value > maxDate) {
value = maxDate;
valueAltered = true;
}

this.#pikaday.setDate(value, !valueAltered);
this.#pikaday.config(this.pikadayOptions);
}

willDestroy() {
Expand All @@ -38,3 +70,12 @@ export default class PikadayModifier extends Modifier {
}
}
}

// apparently Pikaday mutates Dates that you pass it for minDate and maxDate. <sigh>
function copyDate(date) {
if (date) {
return new Date(date.getTime());
} else {
return date;
}
}
3 changes: 2 additions & 1 deletion test-app/tests/integration/components/pikaday-input2-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const getDisabledDayCB = (weekendDay) => (e) => {
return attr ? attr.value == weekendDay : false;
};

module.skip('Integration | Component | pikaday-input2', function (hooks) {
module('Integration | Component | pikaday-input2', function (hooks) {
setupRenderingTest(hooks);

test('it is an input tag', async function (assert) {
Expand Down Expand Up @@ -666,6 +666,7 @@ module.skip('Integration | Component | pikaday-input2', function (hooks) {

this.set('minDate', today);
this.set('maxDate', tomorrow);
await settled();

assert.equal(
today.toISOString(),
Expand Down

0 comments on commit ceb8c42

Please sign in to comment.