From e0e6f6d7a1f51e88028c8a259a254ce030594008 Mon Sep 17 00:00:00 2001 From: Philipp Siekmann Date: Sat, 12 Nov 2022 15:46:37 +0100 Subject: [PATCH] feat: add "disabled" prop --- .changeset/long-windows-rescue.md | 5 +++ docs/index.html | 30 +++++++++++++++ src/components.d.ts | 2 + .../wc-datepicker/wc-datepicker.spec.tsx | 29 +++++++++++++++ .../wc-datepicker/wc-datepicker.tsx | 33 ++++++++++++++++- src/themes/dark.css | 37 ++++++++++++++++++- src/themes/light.css | 37 ++++++++++++++++++- 7 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 .changeset/long-windows-rescue.md diff --git a/.changeset/long-windows-rescue.md b/.changeset/long-windows-rescue.md new file mode 100644 index 0000000..61b3304 --- /dev/null +++ b/.changeset/long-windows-rescue.md @@ -0,0 +1,5 @@ +--- +'wc-datepicker': minor +--- + +Add "disabled" property diff --git a/docs/index.html b/docs/index.html index a0d4243..0a866c3 100644 --- a/docs/index.html +++ b/docs/index.html @@ -118,6 +118,20 @@

Demo

+ + + disabled
+ Disable the datepicker + + + + + first-day-of-weekOptions + + disabled + boolean + false + Disable the datepicker. + disableDate (date: Date) => boolean @@ -527,6 +547,10 @@

Styling

.wc-datepicker Outer element of the datepicker + + .wc-datepicker--disabled + Outer element of the disabled datepicker + .wc-datepicker__header @@ -759,6 +783,7 @@

License

const demoCodeContainer = document.getElementById('demo-code'); + const disabledInput = document.getElementById('disabled'); const firstDayOfWeekInput = document.getElementById('first-day-of-week'); const localeInput = document.getElementById('locale'); const rangeInput = document.getElementById('range'); @@ -798,6 +823,11 @@

License

valueInput.value = event.detail ? JSON.stringify(event.detail) : ''; }); + disabledInput.addEventListener('change', (event) => { + datepicker.setAttribute('disabled', String(event.target.checked)); + updateDemoCode(); + }); + firstDayOfWeekInput.addEventListener('change', (event) => { datepicker.setAttribute('first-day-of-week', event.target.value); updateDemoCode(); diff --git a/src/components.d.ts b/src/components.d.ts index 60bdbff..3693a1f 100644 --- a/src/components.d.ts +++ b/src/components.d.ts @@ -10,6 +10,7 @@ export namespace Components { interface WcDatepicker { "clearButtonContent"?: string; "disableDate"?: (date: Date) => boolean; + "disabled"?: boolean; "elementClassName"?: string; "firstDayOfWeek"?: number; "labels"?: WCDatepickerLabels; @@ -47,6 +48,7 @@ declare namespace LocalJSX { interface WcDatepicker { "clearButtonContent"?: string; "disableDate"?: (date: Date) => boolean; + "disabled"?: boolean; "elementClassName"?: string; "firstDayOfWeek"?: number; "labels"?: WCDatepickerLabels; diff --git a/src/components/wc-datepicker/wc-datepicker.spec.tsx b/src/components/wc-datepicker/wc-datepicker.spec.tsx index 0b9bb32..8615ffb 100644 --- a/src/components/wc-datepicker/wc-datepicker.spec.tsx +++ b/src/components/wc-datepicker/wc-datepicker.spec.tsx @@ -453,4 +453,33 @@ describe('wc-datepicker', () => { expect(spy.mock.calls[1][0].detail).toBe(undefined); }); + + it('can be disabled', async () => { + const page = await newSpecPage({ + components: [WCDatepicker], + html: ``, + language: 'en' + }); + + const spy = jest.fn(); + + page.root.startDate = new Date('2022-01-01'); + page.root.addEventListener('selectDate', spy); + page.root.addEventListener('changeMonth', spy); + + await page.waitForChanges(); + + page.root + .querySelector('.wc-datepicker__date') + .click(); + + triggerKeyDown(page, 'ArrowRight'); + triggerKeyDown(page, 'Space'); + + expect( + page.root.children[0].classList.contains('wc-datepicker--disabled') + ).toBeTruthy(); + + expect(spy).not.toHaveBeenCalled(); + }); }); diff --git a/src/components/wc-datepicker/wc-datepicker.tsx b/src/components/wc-datepicker/wc-datepicker.tsx index 09b22d8..2701e00 100644 --- a/src/components/wc-datepicker/wc-datepicker.tsx +++ b/src/components/wc-datepicker/wc-datepicker.tsx @@ -69,6 +69,7 @@ export class WCDatepicker { @Element() el: HTMLElement; @Prop() clearButtonContent?: string; + @Prop() disabled?: boolean = false; @Prop() disableDate?: (date: Date) => boolean = () => false; @Prop() elementClassName?: string = 'wc-datepicker'; @Prop() firstDayOfWeek?: number = 0; @@ -284,6 +285,10 @@ export class WCDatepicker { }; private onClick = (event: Event) => { + if (this.disabled) { + return; + } + const target = (event.target as HTMLElement).closest( '[data-date]' ); @@ -317,6 +322,10 @@ export class WCDatepicker { }; private onKeyDown = (event: KeyboardEvent) => { + if (this.disabled) { + return; + } + if (event.code === 'ArrowLeft') { event.preventDefault(); this.updateCurrentDate(getPreviousDay(this.currentDate), true); @@ -358,6 +367,10 @@ export class WCDatepicker { }; private onMouseEnter = (event: MouseEvent) => { + if (this.disabled) { + return; + } + const date = new Date( (event.target as HTMLElement).closest('td').dataset.date ); @@ -375,8 +388,12 @@ export class WCDatepicker { return (
@@ -387,6 +404,7 @@ export class WCDatepicker {