Skip to content

Commit

Permalink
refactor(Timepicker) implemented callback methods for confirm, cancel…
Browse files Browse the repository at this point in the history
…; configured confirmation buttons to render when auto submit is false materializecss#558
  • Loading branch information
gselderslaghs committed Jan 24, 2025
1 parent b03a26a commit 1585fad
Showing 1 changed file with 40 additions and 12 deletions.
52 changes: 40 additions & 12 deletions src/timepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ export interface TimepickerOptions extends BaseOptions {
* @default null
*/
onInputInteraction: (() => void) | null;
/**
* Callback function for done.
* @default null
*/
onDone: (() => void) | null;
/**
* Callback function for cancel.
* @default null
*/
onCancel: (() => void) | null;
}

const _defaults: TimepickerOptions = {
Expand All @@ -102,7 +112,9 @@ const _defaults: TimepickerOptions = {
vibrate: true, // vibrate the device when dragging clock hand
// Callbacks
onSelect: null,
onInputInteraction: null
onInputInteraction: null,
onDone: null,
onCancel: null,
};

type Point = {
Expand Down Expand Up @@ -383,19 +395,21 @@ export class Timepicker extends Component<TimepickerOptions> {
clearButton.addEventListener('click', this.clear);
this.footer.appendChild(clearButton);

const confirmationBtnsContainer = document.createElement('div');
confirmationBtnsContainer.classList.add('confirmation-btns');
this.footer.append(confirmationBtnsContainer);
if (!this.options.autoSubmit) {
const confirmationBtnsContainer = document.createElement('div');
confirmationBtnsContainer.classList.add('confirmation-btns');
this.footer.append(confirmationBtnsContainer);

const cancelButton = this._createButton(this.options.i18n.cancel, '');
cancelButton.classList.add('timepicker-close');
cancelButton.addEventListener('click', this.close);
confirmationBtnsContainer.appendChild(cancelButton);
const cancelButton = this._createButton(this.options.i18n.cancel, '');
cancelButton.classList.add('timepicker-close');
cancelButton.addEventListener('click', this.close);
confirmationBtnsContainer.appendChild(cancelButton);

const doneButton = this._createButton(this.options.i18n.done, '');
doneButton.classList.add('timepicker-close');
//doneButton.addEventListener('click', this._finishSelection);
confirmationBtnsContainer.appendChild(doneButton);
const doneButton = this._createButton(this.options.i18n.done, '');
doneButton.classList.add('timepicker-close');
doneButton.addEventListener('click', this.done);
confirmationBtnsContainer.appendChild(doneButton);
}
this._updateTimeFromInput();
this.showView('hours');
}
Expand Down Expand Up @@ -775,6 +789,20 @@ export class Timepicker extends Component<TimepickerOptions> {
}
};

confirm = () => {
this.done();
if (typeof this.options.onDone === 'function') {
setTimeout(() => {
this.options.onDone.call(this);
}, this.options.duration / 2);
}
}

cancel = () => {
this.clear();
if (typeof this.options.onDone === 'function') this.options.onCancel.call(this);
}

clear = () => {
this.done(true);
};
Expand Down

0 comments on commit 1585fad

Please sign in to comment.