Skip to content

Pickers modal display plugin #608

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

Draft
wants to merge 5 commits into
base: v2-dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 16 additions & 11 deletions sass/components/_datepicker.scss
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
/* Modal */
// @removed since v2.2.1
/*.datepicker-modal {
max-width: 325px;
// @removed since v2.2.1-dev regarding Material M3 standards
min-width: 300px;
max-height: none;
}*/
@use './mixins.module.scss' as *;

.datepicker-container {
display: flex;
flex-direction: column;
max-width: 325px;
padding: 0;
background-color: var(--md-sys-color-surface);
background-color: var(--md-sys-color-inverse-on-surface);
}

.datepicker-controls {
Expand All @@ -34,8 +27,8 @@
text-align: center;

&:focus {
border-bottom: none;
background-color: var(--md-sys-color-primary-container);
color: var(--md-sys-color-primary);
background-color: color-mix(in srgb, transparent, var(--md-sys-color-primary) 20%);
}

&::selection {
Expand Down Expand Up @@ -70,6 +63,12 @@
.month-next {
display: inline-flex;
align-items: center;

@include btn($height: 49px);

&:focus {
background-color: color-mix(in srgb, transparent, var(--md-sys-color-primary) 20%);
}
}

.month-prev > svg,
Expand Down Expand Up @@ -261,3 +260,9 @@
color: var(--md-sys-color-error);
}

/* Display modes */
.datepicker-modal {
max-width: calc(325px + var(--modal-padding)*2);
max-height: none;
background-color: var(--md-sys-color-inverse-on-surface);
}
3 changes: 3 additions & 0 deletions sass/components/_global.scss
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,6 @@ $spacing-values: ("0": 0, "1": 0.25rem, "2": 0.5rem, "3": 0.75rem, "4": 1rem, "5
visibility: hidden;
}

.confirmation-btns {
margin-left: auto;
}
8 changes: 6 additions & 2 deletions sass/components/_modal.scss
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,23 @@
flex-shrink: 0;
position: sticky;
top: 0;
background-color: var(--modal-background-color);
// disabled since background color inheritance from parent element
// background-color: var(--modal-background-color);
}
.modal-content {
padding: 0 var(--modal-padding);
}
.modal-footer {
display: flex;
border-radius: 0 0 var(--modal-border-radius) var(--modal-border-radius);
padding: var(--modal-padding);
text-align: right;
flex-shrink: 0;
position: sticky;
bottom: 0;
background-color: var(--modal-background-color);
// disabled since background color inheritance from parent element
// background-color: var(--modal-background-color);
justify-content: space-between;
}

.modal-close {
Expand Down
72 changes: 70 additions & 2 deletions sass/components/_timepicker.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
width: auto;
flex: 1 auto;
// background-color: var(--md-sys-color-surface);
padding: 2rem .67rem .67rem .67rem;
padding: 2rem .71rem .67rem .71rem;
font-weight: 300;
}

Expand Down Expand Up @@ -51,7 +51,7 @@

.timepicker-input-hours-wrapper,
.timepicker-input-minutes-wrapper {
width: 6.9rem;
width: 6.85rem;
height: 5.75rem;
}

Expand Down Expand Up @@ -231,6 +231,38 @@ input[type=text].timepicker-input-minutes {
padding: 0 20px;
}

/* Display modes */
.timepicker-modal {
max-width: 326px;
max-height: none;
background-color: var(--md-sys-color-inverse-on-surface);
overflow-x: hidden;

// Reset margins and paddings since they are defined by the modal instance
.timepicker-container {
margin-left: calc(-1 * var(--modal-padding));
margin-right: calc(-1 * var(--modal-padding));
}

.modal-header + .modal-content {
.timepicker-digital-display {
padding-top: 0;
}

.timepicker-text-container {
padding-top: 4px;
}
}

.timepicker-analog-display {
padding-bottom: 0;
}

.timepicker-plate {
margin-bottom: 0;
}
}

/* Media Queries */
@media #{$large-and-up} {
.timepicker-container {
Expand Down Expand Up @@ -265,6 +297,11 @@ input[type=text].timepicker-input-minutes {
display: flex;
flex-grow: 1;
max-width: unset;
/*width: 100%;
padding: 0 4%;
flex-wrap: wrap;
flex-direction: row;
display: flex;*/
}

.timepicker-container .am-btn,
Expand All @@ -291,4 +328,35 @@ input[type=text].timepicker-input-minutes {
.timepicker-plate {
margin-top: 1.6rem;
}

/* Display modes */
.timepicker-modal {
width: 65%;
max-width: 605px;

.modal-header + .modal-content {
.timepicker-digital-display,
.timepicker-analog-display {
padding-top: 0;
}

.timepicker-text-container {
margin-top: 2.4rem;
}

.timepicker-plate {
margin-top: 4px;
margin-bottom: 0;
}
}

.timepicker-digital-display,
.timepicker-analog-display {
padding-bottom: 0;
}

.timepicker-plate {
margin-bottom: 0;
}
}
}
121 changes: 71 additions & 50 deletions src/datepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Utils } from './utils';
import { FormSelect } from './select';
import { BaseOptions, Component, I18nOptions, InitElements, MElement } from './component';
import { DockedDisplayPlugin } from './plugin/dockedDisplayPlugin';
import { ModalDisplayPlugin } from './plugin/modalDisplayPlugin';

export interface DateI18nOptions extends I18nOptions {
previousMonth: string;
Expand Down Expand Up @@ -321,7 +322,7 @@ export class Datepicker extends Component<DatepickerOptions> {
calendars: [{ month: number; year: number }];
private _y: number;
private _m: number;
private displayPlugin: DockedDisplayPlugin;
private displayPlugin: DockedDisplayPlugin | ModalDisplayPlugin;
private footer: HTMLElement;
static _template: string;

Expand All @@ -348,41 +349,8 @@ export class Datepicker extends Component<DatepickerOptions> {
this._setupVariables();
this._insertHTMLIntoDOM();
this._setupEventHandlers();

if (!this.options.defaultDate) {
this.options.defaultDate = new Date(Date.parse(this.el.value));
}

const defDate = this.options.defaultDate;
if (Datepicker._isDate(defDate)) {
if (this.options.setDefaultDate) {
this.setDate(defDate, true);
this.setInputValue(this.el, defDate);
} else {
this.gotoDate(defDate);
}
} else {
this.gotoDate(new Date());
}
if (this.options.isDateRange) {
this.multiple = true;
const defEndDate = this.options.defaultEndDate;
if (Datepicker._isDate(defEndDate)) {
if (this.options.setDefaultEndDate) {
this.setDate(defEndDate, true, true);
this.setInputValue(this.endDateEl, defEndDate);
}
}
}
if (this.options.isMultipleSelection) {
this.multiple = true;
this.dates = [];
this.dateEls = [];
this.dateEls.push(el);
}
if (this.options.displayPlugin) {
if (this.options.displayPlugin === 'docked') this.displayPlugin = DockedDisplayPlugin.init(this.el, this.containerEl, this.options.displayPluginOptions);
}
if (this.options.displayPlugin) this._setupDisplayPlugin();
this._pickerSetup();
}

static get defaults() {
Expand Down Expand Up @@ -503,27 +471,13 @@ export class Datepicker extends Component<DatepickerOptions> {
}
}

/*if (this.options.showClearBtn) {
this.clearBtn.style.visibility = '';
this.clearBtn.innerText = this.options.i18n.clear;
}
this.doneBtn.innerText = this.options.i18n.done;
this.cancelBtn.innerText = this.options.i18n.cancel;*/
Utils.createButton(this.footer, this.options.i18n.clear, ['datepicker-clear'], this.options.showClearBtn, this._handleClearClick);

if (!this.options.autoSubmit) {
Utils.createConfirmationContainer(this.footer, this.options.i18n.done, this.options.i18n.cancel, this._confirm, this._cancel);
}

if (this.options.container) {
const optEl = this.options.container;
this.options.container =
optEl instanceof HTMLElement ? optEl : (document.querySelector(optEl) as HTMLElement);
this.options.container.append(this.containerEl);
} else {
//this.containerEl.before(this.el);
const appendTo = !this.endDateEl ? this.el : this.endDateEl;
if (!this.options.openByDefault) (this.containerEl as HTMLElement).setAttribute('style', 'display: none; visibility: hidden;');
appendTo.parentElement.after(this.containerEl);
}
}
Expand Down Expand Up @@ -700,6 +654,21 @@ export class Datepicker extends Component<DatepickerOptions> {
);
}

/**
* Display plugin setup.
*/
_setupDisplayPlugin() {
if (this.options.displayPlugin === 'docked') this.displayPlugin = DockedDisplayPlugin.init(this.el, this.containerEl, this.options.displayPluginOptions);
if (this.options.displayPlugin === 'modal') {
this.displayPlugin = ModalDisplayPlugin.init(this.el, this.containerEl, {
...this.options.displayPluginOptions,
...{ classList: ['datepicker-modal'] }
});
this.footer.remove();
this.footer = this.displayPlugin.footer;
}
}

/**
* Renders the date in the modal head section.
*/
Expand Down Expand Up @@ -1186,6 +1155,56 @@ export class Datepicker extends Component<DatepickerOptions> {
};
}

_pickerSetup() {
if (!this.options.defaultDate) {
this.options.defaultDate = new Date(Date.parse(this.el.value));
}

const defDate = this.options.defaultDate;
if (Datepicker._isDate(defDate)) {
if (this.options.setDefaultDate) {
this.setDate(defDate, true);
this.setInputValue(this.el, defDate);
} else {
this.gotoDate(defDate);
}
} else {
this.gotoDate(new Date());
}

if (this.options.isDateRange) {
this.multiple = true;
const defEndDate = this.options.defaultEndDate;
if (Datepicker._isDate(defEndDate)) {
if (this.options.setDefaultEndDate) {
this.setDate(defEndDate, true, true);
this.setInputValue(this.endDateEl, defEndDate);
}
}
}

if (this.options.isMultipleSelection) {
this.multiple = true;
this.dates = [];
this.dateEls = [];
this.dateEls.push(this.el);
}

if (this.options.showClearBtn) {
Utils.createButton(
this.footer,
this.options.i18n.clear,
['datepicker-clear'],
true,
this._handleClearClick
);
}

if (!this.options.autoSubmit) {
Utils.createConfirmationContainer(this.footer, this.options.i18n.done, this.options.i18n.cancel, this._confirm, this._cancel);
}
}

_removeEventHandlers() {
this.el.removeEventListener('click', this._handleInputClick);
this.el.removeEventListener('keydown', this._handleInputKeydown);
Expand Down Expand Up @@ -1352,10 +1371,12 @@ export class Datepicker extends Component<DatepickerOptions> {

_confirm = () => {
this._finishSelection();
if (this.displayPlugin) this.displayPlugin.hide();
if (typeof this.options.onConfirm === 'function') this.options.onConfirm.call(this);
}

_cancel = () => {
if (this.displayPlugin) this.displayPlugin.hide();
if (typeof this.options.onCancel === 'function') this.options.onCancel.call(this);
};

Expand Down
Loading