Skip to content

Commit

Permalink
feat(datepicker): add request update for min and max date and adding … (
Browse files Browse the repository at this point in the history
#986)

…storybook
  • Loading branch information
dilandoogan authored Jan 9, 2025
1 parent 4be4e06 commit c0bd9eb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
18 changes: 18 additions & 0 deletions src/components/datepicker/bl-datepicker.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,24 @@ You can set input width of datepicker as you wish.
</Story>
</Canvas>


### Min - Max Date

You can set min date or max date for the datepicker.

<Canvas>
<Story name="Set min and max date"
args={{
type: 'single',
label: 'Set min and max date',
placeholder: 'Set min and max date',
minDate: `${new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 2)}`,
maxDate: `${new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() + 2)}`
}}>
{Template.bind({})}
</Story>
</Canvas>

## Reference

<ArgsTable of="bl-datepicker" />
10 changes: 0 additions & 10 deletions src/components/datepicker/bl-datepicker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,16 +284,6 @@ describe("BlDatepicker", () => {
expect(element.value).to.be.undefined;
});

it("should warn when 'value' is not an array for multiple/range selection", async () => {
element = await fixture<BlDatePicker>(html`
<bl-datepicker type="multiple" locale="en"></bl-datepicker>`);
element.value = new Date();

element.firstUpdated();

expect(consoleWarnSpy.calledOnce).to.be.true;
});

it("should not warn when value is an array for multiple/range selection", () => {
element.type = CALENDAR_TYPES.MULTIPLE;
element.value = [new Date(), new Date()];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ describe("DatepickerCalendarMixin", () => {
});

it("should set and get minDate correctly", () => {
const minDate = new Date("2024-01-01");
const minDate = new Date(2024,1,1);

element.minDate = minDate;
expect(element.minDate).to.equal(minDate);
expect(element.minDate.getTime()).to.equal(minDate.getTime());
});

it("should log a warning if minDate is greater than maxDate", () => {
Expand All @@ -56,10 +56,10 @@ describe("DatepickerCalendarMixin", () => {
});

it("should set and get maxDate correctly", () => {
const maxDate = new Date("2024-12-31");
const maxDate = new Date(2024,12,31);

element.maxDate = maxDate;
expect(element.maxDate).to.equal(maxDate);
expect(element.maxDate.getTime()).to.equal(maxDate.getTime());
});

it("should log a warning if maxDate is smaller than minDate", () => {
Expand Down
14 changes: 12 additions & 2 deletions src/mixins/datepicker-calendar-mixin/datepicker-calendar-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,15 @@ export default class DatepickerCalendarMixin extends LitElement {

@property({ type: Date, attribute: "max-date", reflect: true })
set maxDate(maxDate: Date) {
if (isNaN(new Date(maxDate).getTime())) {
console.warn("Invalid maxDate value.");
return;
}
if (this._minDate && this._minDate >= maxDate) {
console.warn("maxDate cannot be smaller than minDate.");
} else {
this._maxDate = maxDate;
this._maxDate = new Date(maxDate);
this.requestUpdate("maxDate", maxDate);
}
}

Expand All @@ -82,10 +87,15 @@ export default class DatepickerCalendarMixin extends LitElement {

@property({ type: Date, attribute: "min-date", reflect: true })
set minDate(minDate: Date) {
if (isNaN(new Date(minDate).getTime())) {
console.warn("Invalid minDate value.");
return;
}
if (this._maxDate && this._maxDate <= minDate) {
console.warn("minDate cannot be greater than maxDate.");
} else {
this._minDate = minDate;
this._minDate = new Date(minDate);
this.requestUpdate("minDate", minDate);
}
}

Expand Down

0 comments on commit c0bd9eb

Please sign in to comment.