diff --git a/src/datepicker-input/datepicker-input.component.ts b/src/datepicker-input/datepicker-input.component.ts
index dd8e4039f7..149edb38f5 100644
--- a/src/datepicker-input/datepicker-input.component.ts
+++ b/src/datepicker-input/datepicker-input.component.ts
@@ -5,93 +5,80 @@ import {
EventEmitter,
ElementRef,
TemplateRef,
- ViewChild
+ ViewChild, HostBinding
} from "@angular/core";
import { NG_VALUE_ACCESSOR } from "@angular/forms";
@Component({
selector: "cds-date-picker-input, ibm-date-picker-input",
template: `
-
`,
@@ -125,10 +123,7 @@ export class DatePicker implements
AfterViewInit {
private static datePickerCount = 0;
- /**
- * Select calendar range mode
- */
- @Input() range = false;
+ @Input() datePickerType: "simple" | "single" | "range" = "simple";
/**
* Format of date
@@ -227,11 +222,11 @@ export class DatePicker implements
}
get flatpickrOptions() {
const plugins = [...this.plugins, carbonFlatpickrMonthSelectPlugin];
- if (this.range) {
+ if (this.datePickerType === "range") {
plugins.push(rangePlugin({ input: `#${this.id}-rangeInput`, position: "left" }));
}
return Object.assign({}, this._flatpickrOptions, this.flatpickrBaseOptions, {
- mode: this.range ? "range" : "single",
+ mode: this.datePickerType === "range" ? "range" : "single",
plugins,
dateFormat: this.dateFormat,
locale: languages.default?.default[this.language] || languages.default[this.language]
@@ -267,7 +262,7 @@ export class DatePicker implements
onClose: (date) => {
// This makes sure that the `flatpickrInstance selectedDates` are in sync with the values of
// the inputs when the calendar closes.
- if (this.range && this.flatpickrInstance) {
+ if (this.datePickerType === "range" && this.flatpickrInstance) {
if (this.flatpickrInstance.selectedDates.length !== 2) {
// we could `this.flatpickrInstance.clear()` but it insists on opening the second picker
// in some cases, so instead we do this
@@ -354,7 +349,7 @@ export class DatePicker implements
onFocus() {
// Updates the month manually when calendar mode is range because month
// will not update properly without manually updating them on focus.
- if (this.range) {
+ if (this.datePickerType === "range") {
if (this.rangeInput.input.nativeElement === document.activeElement && this.flatpickrInstance.selectedDates[1]) {
const currentMonth = this.flatpickrInstance.selectedDates[1].getMonth();
this.flatpickrInstance.changeMonth(currentMonth, false);
@@ -420,7 +415,7 @@ export class DatePicker implements
onValueChange(event: string) {
if (this.isFlatpickrLoaded()) {
const date = this.flatpickrInstance.parseDate(event, this.dateFormat);
- if (this.range) {
+ if (this.datePickerType === "range") {
this.setDateValues([date, this.flatpickrInstance.selectedDates[1]]);
} else {
this.setDateValues([date]);
@@ -444,7 +439,7 @@ export class DatePicker implements
* Handles opening the calendar "properly" when the calendar icon is clicked.
*/
openCalendar(datepickerInput: DatePickerInput) {
- if (this.range) {
+ if (this.datePickerType === "range") {
datepickerInput.input.nativeElement.click();
// If the first input's calendar icon is clicked when calendar is in range mode, then
@@ -652,7 +647,7 @@ export class DatePicker implements
// In range mode, if a date is selected from the first calendar that is from the previous month,
// the month will not be updated on the calendar until the calendar is re-opened.
// This will make sure the calendar is updated with the correct month.
- if (this.range && this.flatpickrInstance.selectedDates[0]) {
+ if (this.datePickerType === "range" && this.flatpickrInstance.selectedDates[0]) {
const currentMonth = this.flatpickrInstance.selectedDates[0].getMonth();
// `flatpickrInstance.changeMonth` removes the focus on the selected date element and will
diff --git a/src/datepicker/datepicker.stories.ts b/src/datepicker/datepicker.stories.ts
index 19f875a637..bdc8ad5686 100644
--- a/src/datepicker/datepicker.stories.ts
+++ b/src/datepicker/datepicker.stories.ts
@@ -41,7 +41,7 @@ export default {
const Template = (args) => ({
props: args,
template: `
- ({
[warn]="warn"
[warnText]="warnText"
(valueChange)="valueChange($event)">
-
+
`
});
export const Basic = Template.bind({});
@@ -65,6 +65,7 @@ const SingleTemplate = (args) => ({
[label]="label"
id="initial-value-datepicker"
[placeholder]="placeholder"
+ [datePickerType]="'single'"
[language]="language"
[size]="size"
[theme]="theme"
@@ -83,6 +84,7 @@ const SingleTemplate = (args) => ({
[placeholder]="placeholder"
[language]="language"
[size]="size"
+ [datePickerType]="'single'"
[theme]="theme"
[disabled]="disabled"
[invalid]="invalid"
@@ -96,8 +98,9 @@ const SingleTemplate = (args) => ({
});
export const Single = SingleTemplate.bind({});
Single.args = {
- dateFormat: "m/d/y",
- language: "en"
+ dateFormat: "m/d/Y",
+ language: "en",
+ value: "01/01/24"
};
Single.argTypes = {
language: {
@@ -114,7 +117,7 @@ const RangeTemplate = (args) => ({
[label]="label"
[rangeLabel]="label"
[size]="size"
- range="true"
+ [datePickerType]="'range'"
id="initial-value-range-datepicker"
[placeholder]="placeholder"
[language]="language"
@@ -135,8 +138,8 @@ const RangeTemplate = (args) => ({