Last active
February 2, 2022 01:22
-
-
Save runspired/42702f3a07b8047052e6c20c9991141f to your computer and use it in GitHub Desktop.
Date Select
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Component from '@glimmer/component'; | |
import { action } from "@ember/object"; | |
const ThirtyMonths = new Set([ | |
4, | |
6, | |
9, | |
10, | |
]); | |
export default class extends Component { | |
get showDefault() { | |
return this.args.showDefault !== undefined ? this.args.showDefault : true; | |
} | |
// `number` (1 -> 31) or `value` ("01" -> "31") | |
get valueType() { | |
return this.args.valueType || "value"; | |
} | |
get days() { | |
const { | |
showDisabled = false, | |
disabledDays = [], | |
month, | |
isLeapYear = false | |
} = this.args; | |
let dayCount = 31; | |
const days = []; | |
if (month) { | |
let monthNum = Number(month); | |
if (typeof month === "number") { | |
monthNum = month + 1; // assume month code for numbers which is zero indexed | |
} | |
if (monthNum === 2) { | |
// we don't allow leap year inputs by default | |
dayCount = isLeapYear ? 29 : 28; | |
} else if (ThirtyMonths.has(monthNum)) { | |
dayCount = 30; | |
} | |
} | |
for (let i = 1; i <= dayCount; i++) { | |
let enabled = !disabledDays.includes(i); | |
if (enabled || showDisabled) { | |
days.push({ | |
number: i, | |
value: i < 10 ? `0${i}` : `${i}`, | |
enabled | |
}); | |
} | |
} | |
return days; | |
} | |
@action | |
update(updateFn, event) { | |
const value = event.target.value; | |
updateFn(this.valueType === "number" ? Number(value) : value); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Component from '@glimmer/component'; | |
import { action } from "@ember/object"; | |
export default class extends Component { | |
get displayType() { | |
return this.args.displayType || "long"; | |
} | |
// `number` (1 -> 12) | |
// or `value` ("01" -> "12") | |
// or `code` (0 -> 11) | |
// or `long` (January) | |
// or `short` (Jan) | |
get valueType() { | |
return this.args.valueType || "value"; | |
} | |
get showDefault() { | |
return this.args.showDefault !== undefined ? this.args.showDefault : true; | |
} | |
get months() { | |
const { | |
showDisabled = false, | |
disabledMonths = [], | |
locale = "default", | |
} = this.args; | |
const date = new Date(); | |
const months = []; | |
for (let i = 0; i < 12; i++) { | |
let enabled = !disabledMonths.includes(i); | |
if (enabled || showDisabled) { | |
date.setMonth(i, 1); | |
const value = i + 1; | |
months.push({ | |
long: date.toLocaleString(locale, { month: 'long' }), | |
short: date.toLocaleString(locale, { month: 'short' }), | |
value: value < 10 ? `0${value}` : `${value}`, | |
number: i + 1, | |
code: i, | |
enabled | |
}); | |
} | |
} | |
return months; | |
} | |
@action | |
update(updateFn, event) { | |
const value = event.target.value; | |
const { valueType } = this; | |
updateFn( | |
(valueType === "number" || valueType === "code") ? | |
Number(value) : value | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Controller from '@ember/controller'; | |
import { tracked } from "@glimmer/tracking"; | |
export default class ApplicationController extends Controller { | |
appName = 'Ember Twiddle'; | |
@tracked | |
month = 1; | |
@tracked | |
day = 1; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { helper } from '@ember/component/helper'; | |
export default helper(function eq(params/*, hash*/) { | |
return params[0] === params[1]; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { helper } from '@ember/component/helper'; | |
export default helper(function not(params/*, hash*/) { | |
return !params[0]; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.17.1", | |
"EmberENV": { | |
"FEATURES": {}, | |
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false, | |
"_APPLICATION_TEMPLATE_WRAPPER": true, | |
"_JQUERY_INTEGRATION": true | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js", | |
"ember": "3.18.1", | |
"ember-template-compiler": "3.18.1", | |
"ember-testing": "3.18.1" | |
}, | |
"addons": { | |
"@glimmer/component": "1.0.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment