Skip to content

Commit

Permalink
Merge pull request #346 from uvarov-frontend/fix/342_set_method_for_i…
Browse files Browse the repository at this point in the history
…nput

Fix set method for input
  • Loading branch information
uvarov-frontend authored Dec 16, 2024
2 parents 5ffaeac + ded3768 commit 7f049cf
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 45 deletions.
70 changes: 34 additions & 36 deletions demo/pages/input/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,53 +28,51 @@ const configDiv: Options = {
}
self.hide();
},
popups: {
'2024-08-28': {
modifier: 'bg-red',
html: 'Meeting at 9:00 PM',
},
'2024-08-05': {
modifier: 'bg-red text-bold',
html: 'Meeting at 6:00 PM with a friend',
},
'2024-08-11': {
modifier: 'bg-red text-bold',
html: 'Diner at 8:00 PM with a friend',
},
'2024-08-19': {
modifier: 'bg-orange',
html: `<div>
<u><b>12:00 PM</b></u>
<p style="margin: 5px 0 0;">Airplane in Las Vegas</p>
</div>`,
},
'2024-08-04': {
modifier: 'bg-orange',
html: `<div>
<u><b>12:00 PM</b></u>
<p style="margin: 5px 0 0;">Lunch with John for initial meeting</p>
</div>`,
},
},
};

document.addEventListener('DOMContentLoaded', () => {
const calendarInput = new Calendar('#calendar-input', configInput);
calendarInput.init();

const calendarDiv = new Calendar('#calendar-div', configDiv);
calendarDiv.set({
popups: {
'2024-08-28': {
modifier: 'bg-red',
html: 'Meeting at 9:00 PM',
},
'2024-08-05': {
modifier: 'bg-red text-bold',
html: 'Meeting at 6:00 PM with a friend',
},
'2024-08-11': {
modifier: 'bg-red text-bold',
html: 'Diner at 8:00 PM with a friend',
},
'2024-08-19': {
modifier: 'bg-orange',
html: `<div>
<u><b>12:00 PM</b></u>
<p style="margin: 5px 0 0;">Airplane in Las Vegas</p>
</div>`,
},
'2024-08-04': {
modifier: 'bg-orange',
html: `<div>
<u><b>12:00 PM</b></u>
<p style="margin: 5px 0 0;">Lunch with John for initial meeting</p>
</div>`,
},
},
});
calendarDiv.init();

document.querySelector('#set-date')?.addEventListener('click', () => {
calendarInput.selectedDates = ['2023-04-07'];
calendarInput.selectedMonth = 3;
calendarInput.selectedYear = 2023;
calendarInput.update({
dates: true,
month: true,
year: true,
calendarInput.set({
selectedDates: ['2023-04-07'],
selectedMonth: 3,
selectedYear: 2023,
});

calendarInput.context.inputElement!.value = '2023-04-07';
});
});
4 changes: 2 additions & 2 deletions package/src/scripts/methods/reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import initAllVariables from '@scripts/utils/initVariables/initAllVariables';
import setContext from '@scripts/utils/setContext';
import type { Calendar, Reset } from '@src/index';

const reset = (self: Calendar, { year, month, dates, time, locale }: Reset) => {
const reset = (self: Calendar, { year, month, dates, time, locale }: Reset, recreate = true) => {
const previousSelected = {
year: self.selectedYear,
month: self.selectedMonth,
Expand Down Expand Up @@ -32,7 +32,7 @@ const reset = (self: Calendar, { year, month, dates, time, locale }: Reset) => {
}

initAllVariables(self);
create(self);
if (recreate) create(self);

self.selectedYear = previousSelected.year;
self.selectedMonth = previousSelected.month;
Expand Down
5 changes: 2 additions & 3 deletions package/src/scripts/methods/set.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import reset from '@scripts/methods/reset';
import update from '@scripts/methods/update';
import replaceProperties from '@scripts/utils/replaceProperties';
import type { Calendar, Options, Reset } from '@src/index';

const set = (self: Calendar, options: Options, resetOptions?: Partial<Reset>) => {
const defaultReset = { year: true, month: true, dates: true, time: true, locale: true };
replaceProperties(self, options);
reset(self, { ...defaultReset, ...resetOptions });
if (self.context.isInit) update(self, resetOptions);
};

export default set;
5 changes: 1 addition & 4 deletions package/src/scripts/methods/update.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import createToInput from '@scripts/creators/createToInput';
import reset from '@scripts/methods/reset';
import errorMessages from '@scripts/utils/getErrorMessages';
import type { Calendar, Reset } from '@src/index';

const update = (self: Calendar, resetOptions?: Partial<Reset>) => {
if (!self.context.isInit) throw new Error(errorMessages.notInit);
if (self.inputMode && !self.context.inputModeInit) createToInput(self, false);
const defaultReset = { year: true, month: true, dates: true, time: true, locale: true };

reset(self, { ...defaultReset, ...resetOptions });
reset(self, { ...defaultReset, ...resetOptions }, !(self.inputMode && !self.context.inputModeInit));
if (self.onUpdate) self.onUpdate(self);
};

Expand Down

0 comments on commit 7f049cf

Please sign in to comment.