Skip to content

Commit 36331b4

Browse files
authored
Merge branch 'next' into tag-component
2 parents 69585ab + d8ed355 commit 36331b4

8 files changed

+92
-23
lines changed

docs/using-baklava-in-next.stories.mdx

+23
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,29 @@ In the page, import the component using `dynamic` with ssr off.
5252
const Button = dynamic(() => import("@/components/Button"), { ssr: false });
5353
```
5454
55+
Or you can export the component as dynamic to avoid type errors.
56+
57+
```jsx
58+
59+
"use client";
60+
61+
import dynamic from 'next/dynamic';
62+
import type { ComponentProps } from 'react';
63+
import { BlButton } from "@trendyol/baklava/dist/baklava-react";
64+
65+
type ButtonProps = ComponentProps<typeof BlButton>;
66+
67+
const Button = (props: ButtonProps) => (
68+
<BlButton {...props}>Click me!</BlButton>
69+
);
70+
71+
const DynamicButton = dynamic<ButtonProps>(() =>
72+
Promise.resolve(Button)
73+
);
74+
75+
export default DynamicButton;
76+
```
77+
5578
[Here is the demo repository](https://github.com/trendyol/baklava/tree/next/examples/next-app-router-ssr). You can also preview it live with [StackBlitz](https://stackblitz.com/github/trendyol/baklava/tree/next/examples/next-app-router-ssr).
5679
5780
## Using with SSR

src/components/calendar/bl-calendar.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ describe("bl-calendar", () => {
534534

535535
const setNextCalendarViewSpy = sinon.spy(element, "setNextCalendarView");
536536

537-
element.handleDate(new Date(2024, 2, 15));
537+
element.handleDate(new Date(element.today.getFullYear()+1,2,15));
538538

539539
expect(setNextCalendarViewSpy).to.have.been.called;
540540

src/components/calendar/bl-calendar.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,12 @@ export default class BlCalendar extends DatepickerCalendarMixin {
200200
}
201201

202202
checkIfSelectedDate(calendarDate: Date) {
203-
return this._selectedDates?.some(date => date?.getTime() === calendarDate.getTime());
203+
return this._selectedDates?.some(
204+
date =>
205+
date.getFullYear() === calendarDate.getFullYear() &&
206+
date.getMonth() === calendarDate.getMonth() &&
207+
date.getDate() === calendarDate.getDate()
208+
);
204209
}
205210

206211
checkIfDateIsToday(calendarDate: Date) {

src/components/datepicker/bl-datepicker.stories.mdx

+18
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,24 @@ You can set input width of datepicker as you wish.
134134
</Story>
135135
</Canvas>
136136

137+
138+
### Min - Max Date
139+
140+
You can set min date or max date for the datepicker.
141+
142+
<Canvas>
143+
<Story name="Set min and max date"
144+
args={{
145+
type: 'single',
146+
label: 'Set min and max date',
147+
placeholder: 'Set min and max date',
148+
minDate: `${new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 2)}`,
149+
maxDate: `${new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() + 2)}`
150+
}}>
151+
{Template.bind({})}
152+
</Story>
153+
</Canvas>
154+
137155
## Reference
138156

139157
<ArgsTable of="bl-datepicker" />

src/components/datepicker/bl-datepicker.test.ts

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { aTimeout, expect, fixture, html } from "@open-wc/testing";
1+
import { aTimeout, elementUpdated, expect, fixture, html } from "@open-wc/testing";
22
import { BlButton, BlDatePicker } from "../../baklava";
33
import { CALENDAR_TYPES } from "../calendar/bl-calendar.constant";
44
import sinon from "sinon";
@@ -95,11 +95,20 @@ describe("BlDatepicker", () => {
9595
element._selectedDates = [new Date(2023, 1, 1)];
9696
await element.updateComplete;
9797

98+
element.addEventListener("bl-datepicker-change", (event) => {
99+
const customEvent = event as CustomEvent;
100+
101+
expect(customEvent).to.exist;
102+
expect(customEvent.detail).to.deep.equal([]);
103+
104+
});
105+
98106
const clearButton = element.shadowRoot?.querySelector("bl-button") as BlButton;
99107

100108
clearButton?.click();
101109
await element.updateComplete;
102110

111+
103112
expect(element._selectedDates).to.deep.equal([]);
104113
expect(element._inputValue).to.equal("");
105114
});
@@ -275,16 +284,6 @@ describe("BlDatepicker", () => {
275284
expect(element.value).to.be.undefined;
276285
});
277286

278-
it("should warn when 'value' is not an array for multiple/range selection", async () => {
279-
element = await fixture<BlDatePicker>(html`
280-
<bl-datepicker type="multiple" locale="en"></bl-datepicker>`);
281-
element.value = new Date();
282-
283-
element.firstUpdated();
284-
285-
expect(consoleWarnSpy.calledOnce).to.be.true;
286-
});
287-
288287
it("should not warn when value is an array for multiple/range selection", () => {
289288
element.type = CALENDAR_TYPES.MULTIPLE;
290289
element.value = [new Date(), new Date()];
@@ -376,4 +375,13 @@ describe("BlDatepicker", () => {
376375
expect(focusSpy.called).to.be.true;
377376
});
378377

378+
it("should call setDatePickerInput when _selectedDates changes", async () => {
379+
const setDatePickerInputSpy = sinon.spy(element, "setDatePickerInput");
380+
381+
element.value = [new Date(2025,0,10)];
382+
await elementUpdated(element);
383+
384+
expect(setDatePickerInputSpy).to.have.been.calledOnceWith(element._selectedDates);
385+
});
386+
379387
});

src/components/datepicker/bl-datepicker.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CSSResultGroup, html, TemplateResult } from "lit";
1+
import { CSSResultGroup, html, PropertyValues, TemplateResult } from "lit";
22
import { customElement, property, query, state } from "lit/decorators.js";
33
import { BlCalendar, BlPopover } from "../../baklava";
44
import DatepickerCalendarMixin from "../../mixins/datepicker-calendar-mixin/datepicker-calendar-mixin";
@@ -147,10 +147,10 @@ export default class BlDatepicker extends DatepickerCalendarMixin {
147147
}
148148

149149
clearDatepicker() {
150-
this._calendarEl.handleClearSelectedDates();
151150
this._selectedDates = [];
152151
this._inputValue = "";
153152
this._floatingDateCount = 0;
153+
this._calendarEl.handleClearSelectedDates();
154154
}
155155

156156
openPopover() {
@@ -197,6 +197,12 @@ export default class BlDatepicker extends DatepickerCalendarMixin {
197197
}
198198
}
199199

200+
updated(changedProperties: PropertyValues) {
201+
if (changedProperties.has("_selectedDates")) {
202+
this.setDatePickerInput(this._selectedDates);
203+
}
204+
}
205+
200206
disconnectedCallback() {
201207
super.disconnectedCallback();
202208
this._calendarEl?.removeEventListener("mousedown", this._onCalendarMouseDown);
@@ -242,7 +248,7 @@ export default class BlDatepicker extends DatepickerCalendarMixin {
242248
variant="tertiary"
243249
kind="neutral"
244250
icon="close"
245-
@click=${() => this.clearDatepicker()}
251+
@click=${this.clearDatepicker}
246252
></bl-button>
247253
<div class="action-divider"></div>`
248254
: "";

src/mixins/datepicker-calendar-mixin/datepicker-calendar-mixin.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ describe("DatepickerCalendarMixin", () => {
4040
});
4141

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

4545
element.minDate = minDate;
46-
expect(element.minDate).to.equal(minDate);
46+
expect(element.minDate.getTime()).to.equal(minDate.getTime());
4747
});
4848

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

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

6161
element.maxDate = maxDate;
62-
expect(element.maxDate).to.equal(maxDate);
62+
expect(element.maxDate.getTime()).to.equal(maxDate.getTime());
6363
});
6464

6565
it("should log a warning if maxDate is smaller than minDate", () => {

src/mixins/datepicker-calendar-mixin/datepicker-calendar-mixin.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export default class DatepickerCalendarMixin extends LitElement {
3434

3535
@property({
3636
attribute: "disabled-dates",
37-
type: Array,
3837
reflect: true,
3938
})
4039
set disabledDates(disabledDates: Date[] | string) {
@@ -65,10 +64,15 @@ export default class DatepickerCalendarMixin extends LitElement {
6564

6665
@property({ type: Date, attribute: "max-date", reflect: true })
6766
set maxDate(maxDate: Date) {
67+
if (isNaN(new Date(maxDate).getTime())) {
68+
console.warn("Invalid maxDate value.");
69+
return;
70+
}
6871
if (this._minDate && this._minDate >= maxDate) {
6972
console.warn("maxDate cannot be smaller than minDate.");
7073
} else {
71-
this._maxDate = maxDate;
74+
this._maxDate = new Date(maxDate);
75+
this.requestUpdate("maxDate", maxDate);
7276
}
7377
}
7478

@@ -83,10 +87,15 @@ export default class DatepickerCalendarMixin extends LitElement {
8387

8488
@property({ type: Date, attribute: "min-date", reflect: true })
8589
set minDate(minDate: Date) {
90+
if (isNaN(new Date(minDate).getTime())) {
91+
console.warn("Invalid minDate value.");
92+
return;
93+
}
8694
if (this._maxDate && this._maxDate <= minDate) {
8795
console.warn("minDate cannot be greater than maxDate.");
8896
} else {
89-
this._minDate = minDate;
97+
this._minDate = new Date(minDate);
98+
this.requestUpdate("minDate", minDate);
9099
}
91100
}
92101

0 commit comments

Comments
 (0)