Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(datepicker): add week select support #806

Merged
merged 23 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
55e1a4f
add week select plugin
JoelGraham93 Apr 26, 2021
7ecfc7b
week select style progress
JoelGraham93 Apr 26, 2021
1e2f4ae
Merge branch 'master' into viv-515-datepicker-week
JoelGraham93 Apr 28, 2021
83e76ab
Merge branch 'master' into viv-515-datepicker-week
JoelGraham93 Apr 29, 2021
94109a6
highlight selected week day
JoelGraham93 Apr 29, 2021
8a54fcd
remove duplicate arg type
JoelGraham93 Apr 29, 2021
4d0cc1c
Merge branch 'master' into viv-515-datepicker-week
JoelGraham93 Apr 30, 2021
27598db
update week select test
JoelGraham93 Apr 30, 2021
c8a9039
improve story
JoelGraham93 Apr 30, 2021
7711fb8
update test
JoelGraham93 Apr 30, 2021
bf7abee
Merge branch 'master' into viv-515-datepicker-week
yinonov May 2, 2021
f0e0476
Merge branch 'master' into viv-515-datepicker-week
JoelGraham93 May 3, 2021
3124599
Merge branch 'master' into viv-515-datepicker-week
JoelGraham93 May 5, 2021
6320d1f
Merge branch 'master' into viv-515-datepicker-week
JoelGraham93 May 5, 2021
00646ac
Merge branch 'master' into viv-515-datepicker-week
JoelGraham93 May 7, 2021
02c9102
Merge branch 'master' into viv-515-datepicker-week
JoelGraham93 May 9, 2021
19fd43c
display next month days
JoelGraham93 May 10, 2021
aa2a8c1
make week select compatible with inline mode
JoelGraham93 May 10, 2021
2af334e
Merge branch 'master' into viv-515-datepicker-week
JoelGraham93 May 10, 2021
97fb073
Merge branch 'master' into viv-515-datepicker-week
JoelGraham93 May 11, 2021
3317a01
Merge branch 'master' into viv-515-datepicker-week
yinonov May 12, 2021
8aa5947
clear week hover styles on mouseleave
JoelGraham93 May 13, 2021
1562c6e
Merge branch 'master' into viv-515-datepicker-week
yinonov May 14, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 68 additions & 16 deletions components/datepicker/src/vwc-datepicker.scss
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,17 @@ vwc-menu {
}

&:nth-child(7n) {
&.flatpickr-day.inRange {
&.flatpickr-day.inRange,
&.flatpickr-day.week {
border-bottom-right-radius: 15px;
border-top-right-radius: 15px;
box-shadow: -5px 0 0 var(#{scheme-variables.$vvd-color-neutral-20});
}
}

&:nth-child(7n + 1) {
&.flatpickr-day.inRange {
&.flatpickr-day.inRange,
&.flatpickr-day.week {
border-bottom-left-radius: 15px;
border-top-left-radius: 15px;
box-shadow: 5px 0 0 var(#{scheme-variables.$vvd-color-neutral-20});
Expand All @@ -120,17 +122,13 @@ vwc-menu {
}

&.prevMonthDay,
&.nextMonthDay,
&.flatpickr-disabled {
color: var(#{scheme-variables.$vvd-color-neutral-50});
cursor: default;
pointer-events: none;
}

&.nextMonthDay {
height: 0;
visibility: hidden;
}

&:hover,
&.startRange,
&.endRange,
Expand All @@ -146,19 +144,12 @@ vwc-menu {
}
}

&.selected:not(.startRange):not(.endRange) {
&.selected:not(.startRange):not(.endRange):not(.week) {
&::before {
background-color: var(#{scheme-variables.$vvd-color-announcement});
}
}

// &.selected {
// &::before {
// animation-name: none;
// background-color: $black;
// }
// }

&.startRange + .inRange,
&.endRange {
&::after {
Expand All @@ -181,7 +172,8 @@ vwc-menu {
}

&.inRange,
&.inRange.today {
&.inRange.today,
&.week.selected {
border-color: var(#{scheme-variables.$vvd-color-neutral-20});
background-color: var(#{scheme-variables.$vvd-color-neutral-20});
box-shadow: -5px 0 0 var(#{scheme-variables.$vvd-color-neutral-20}),
Expand Down Expand Up @@ -263,6 +255,66 @@ vwc-menu {
}
}


//------------
// week select
//------------
:host([weekSelect]) {
.flatpickr-calendar.vvd-datepicker .flatpickr-day {
&.inRange {
font-weight: normal;

&:hover,
&.today:hover {
z-index: 0;
color: var(#{scheme-variables.$vvd-color-on-primary});

&::before {
background-color: var(#{scheme-variables.$vvd-color-primary});
}
}
}

&.selected {
&::before {
background-color: transparent;
}

&.week {
color: var(#{scheme-variables.$vvd-color-on-base});
font-weight: normal;

&:hover {
color: var(#{scheme-variables.$vvd-color-on-primary});
}
}

&.vvd-selected-week-day {
z-index: 0;
color: var(#{scheme-variables.$vvd-color-on-announcement});

&::before {
background-color: var(#{scheme-variables.$vvd-color-announcement});
}
}
}

&.today {
z-index: 0;
}
}

.inline {
.flatpickr-day {
&.inRange,
&.week.selected:not(:nth-child(7n)):not(:nth-child(7n + 1)) {
box-shadow: -10px 0 0 var(#{scheme-variables.$vvd-color-neutral-20}),
10px 0 0 var(#{scheme-variables.$vvd-color-neutral-20});
}
}
}
}

//--------------
// custom header
//--------------
Expand Down
66 changes: 62 additions & 4 deletions components/datepicker/src/vwc-datepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
customElement, property, html, CSSResult, query
} from 'lit-element';
import { LitFlatpickr } from 'lit-flatpickr';
import * as weekSelectPlugin from 'flatpickr/dist/plugins/weekSelect/weekSelect';
import { Options } from 'flatpickr/dist/types/options';
import { style as vwcDatepickerStyles } from './vwc-datepicker.css.js';
import { VWCButton } from '@vonage/vwc-button';
Expand Down Expand Up @@ -32,6 +33,9 @@ export class VWCDatepicker extends LitFlatpickr {
@property({ type: Boolean, reflect: true })
monthPicker = false;

@property({ type: Boolean, reflect: true })
weekSelect = false;

@property({ type: Boolean, reflect: true })
closeOnSelect = false;

Expand All @@ -42,6 +46,8 @@ export class VWCDatepicker extends LitFlatpickr {

private appendTo: HTMLElement | undefined;

private plugins: Array<any> = [];

constructor() {
super();
// override LitFlatpickr to work with [flatpickr change](https://github.com/flatpickr/flatpickr/blame/07cf1b1ba5ec71da511c295f622d60eed3bf3eb7/src/index.ts#L1522)
Expand All @@ -66,6 +72,10 @@ export class VWCDatepicker extends LitFlatpickr {
const menu = this.shadowRoot?.querySelector('vwc-menu');
if (menu) menu.open = false;
};

if (this.weekSelect) {
this.plugins.push(weekSelectPlugin.default());
}
}

// override lit-flatpicker
Expand Down Expand Up @@ -104,6 +114,8 @@ export class VWCDatepicker extends LitFlatpickr {
this.appendTo = this.datepickerWrapper;
this.initializeComponent();
this.renderCustomParts();
this.disableAdjacentMonthDays();
this.inlineDayClickHandlers();
}

private renderCustomParts(): void {
Expand Down Expand Up @@ -131,7 +143,17 @@ export class VWCDatepicker extends LitFlatpickr {
}
this._instance?.mobileInput?.setAttribute('slot', 'formInputElement');

this.disablePrevMonthDays();
// clear week select hover on mouseleave
if (this.weekSelect) {
const daysContainer = this._instance?.daysContainer;

daysContainer?.addEventListener('mouseleave', () => {
const days = daysContainer.querySelectorAll('.flatpickr-day');
days.forEach((day) => {
day.classList.remove('inRange');
});
});
}
}

private changeHandler(): void {
Expand All @@ -158,6 +180,8 @@ export class VWCDatepicker extends LitFlatpickr {
rangeEnd.classList.toggle('vvd-selected', !!endDate);
}
}

this.highlightSelectedWeekDay();
}

private renderHeader(): void {
Expand Down Expand Up @@ -225,6 +249,8 @@ export class VWCDatepicker extends LitFlatpickr {

this.updateHeaderMonth();
this.updateHeaderYear();
this.highlightSelectedWeekDay();
this.disableAdjacentMonthDays();
}

private updateHeaderMonth(): void {
Expand Down Expand Up @@ -340,6 +366,7 @@ export class VWCDatepicker extends LitFlatpickr {
this._instance?.changeMonth(selectedMonth - this._instance.currentMonth);
this.updateHeaderMonth();
this.updateHeaderYear();
this.highlightSelectedWeekDay();
}
}

Expand Down Expand Up @@ -391,13 +418,43 @@ export class VWCDatepicker extends LitFlatpickr {
}
}

private disablePrevMonthDays(): void {
const prevMonthDays = this._instance?.calendarContainer.querySelectorAll('.prevMonthDay');
prevMonthDays?.forEach((day) => {
private highlightSelectedWeekDay(): void {
if (this._instance && this.weekSelect) {
const className = 'vvd-selected-week-day';
const selectedWeekDay = this._instance.calendarContainer.querySelector(`.${className}`);
selectedWeekDay?.classList.remove(className);
this._instance.selectedDateElem?.classList.add(className);
}

// reset inline day handlers after lit-flatpickr update
if (this.inline && this.weekSelect) {
this.inlineDayClickHandlers();
}
}

private disableAdjacentMonthDays(): void {
const disabledMonthDays = this._instance?.calendarContainer.querySelectorAll('.prevMonthDay, .nextMonthDay');

disabledMonthDays?.forEach((day) => {
day.setAttribute('aria-disabled', 'true');
});
}

private inlineDayClickHandlers(): void {
// inline mode doesn't dispatch change event on day select so trigger changeHandler here
if (this.inline && this.weekSelect) {
const days = this._instance?.calendarContainer.querySelectorAll('.flatpickr-day') as NodeListOf<HTMLElement>;

days.forEach((day) => {
day.onclick = () => {
setTimeout(() => {
this.changeHandler();
}, 0);
};
});
}
}

// copied from lit-flatpickr
// add additional config options
getOptions(): Options {
Expand Down Expand Up @@ -443,6 +500,7 @@ export class VWCDatepicker extends LitFlatpickr {
wrap: this.wrap,
// additional config options
...(this.enable && { enable: this.enable }),
...(this.plugins.length && { plugins: this.plugins }),
appendTo: this.appendTo,
closeOnSelect: this.closeOnSelect
};
Expand Down
7 changes: 6 additions & 1 deletion components/datepicker/stories/arg-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export const argTypes = {
options: ['single', 'multiple', 'range']
}
},
weekSelect: {
control: {
type: 'inline-radio',
options: { 'true': '', 'false': undefined }
}
},
monthPicker: {
control: {
type: 'inline-radio',
Expand Down Expand Up @@ -97,6 +103,5 @@ export const argTypes = {
},
styles: { table: { disable: true } },
theme: { table: { disable: true } },
weekSelect: { table: { disable: true } },
clickOpens: { table: { disable: true } }
}
30 changes: 30 additions & 0 deletions components/datepicker/stories/datepicker-week-select.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import '@vonage/vwc-datepicker';
import { html } from 'lit-element';
import { spread } from '@open-wc/lit-helpers';

const Template = args => html`
<vwc-datepicker id="datepicker" ...=${spread(args)}></vwc-datepicker>

<script>
datepicker.onChange = () => {
const weekNumber = datepicker.getSelectedDates()[0]
? datepicker.getConfig().getWeek(datepicker.getSelectedDates()[0])
: null;

console.log(weekNumber);
};
</script>

<pre><code class="js language-js">
datepicker.onChange = () => {
const weekNumber = datepicker.getSelectedDates()[0]
? datepicker.getConfig().getWeek(datepicker.getSelectedDates()[0])
: null;

console.log(weekNumber);
};
</code></pre>
`;

export const WeekSelect = Template.bind({});
WeekSelect.args = { weekSelect: '', inline: '' };
4 changes: 4 additions & 0 deletions components/datepicker/stories/datepicker.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ const InlineTemplate = args => html`<vwc-datepicker ...=${spread(args)}></vwc-da
export const Inline = InlineTemplate.bind({});
Inline.args = { inline: '' };

export {
WeekSelect
} from './datepicker-week-select.stories.js';

export {
Dialog
} from './datepicker-dialog.stories.js';
Expand Down
19 changes: 19 additions & 0 deletions components/datepicker/test/datepicker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,23 @@ describe('datepicker', () => {
expect(customFooter).to.not.exist;
});
});

it('should highlight selected week day when set to weekSelect', async () => {
const [actualElement] = addElement(
textToDomToParent(`
<${COMPONENT_NAME} inline weekSelect></${COMPONENT_NAME}>
`)
);
await actualElement.onReady;
await waitNextTask();

actualElement.defaultDate = 'today';
await waitNextTask();

actualElement.highlightSelectedWeekDay();

const selectedWeekDay = actualElement._instance.selectedDateElem;
expect(selectedWeekDay).to.exist;
expect(selectedWeekDay.classList.contains('vvd-selected-week-day')).true;
});
});