Skip to content

Commit b8e03e6

Browse files
committed
fix: rerender fix
1 parent d529e9e commit b8e03e6

File tree

4 files changed

+52
-53
lines changed

4 files changed

+52
-53
lines changed

src/components/Calendar/components/DateSelectorComposer/components/DateSelected/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default function DateSelected(date) {
1111
Component.call(this, { html, events });
1212

1313
this.$dateSelected = this.selected.get('date-selected');
14-
this.$dateSelected.innerText = date;
14+
this.setDate(date);
1515

1616
this.emitClickevent = () => {
1717
this.emit('item:click');
@@ -23,5 +23,9 @@ export default function DateSelected(date) {
2323
DateSelected.prototype = Object.assign(
2424
DateSelected.prototype,
2525
Component.prototype,
26-
{},
26+
{
27+
setDate(date) {
28+
this.$dateSelected.innerText = date;
29+
},
30+
},
2731
);

src/components/Calendar/components/DateSelectorComposer/components/MonthSelector/index.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Component } from 'pet-dex-utilities';
22
import { makeSwipable } from '../../../../../../utils/swiper';
33
import SelectorItem from '../SelectorItem';
4-
import { listenBreakpoint } from '../../../../../../utils/breakpoints/breakpoints';
54
import { MONTHS } from '../../../../utils/months';
65
import './index.scss';
76

@@ -28,7 +27,7 @@ export default function MonthSelector(dateArray) {
2827

2928
this.itemCount = this.dateArray.length;
3029
this.columnWidth = 150;
31-
this.nodePadding = 5;
30+
this.nodePadding = 6;
3231
this.scrollLeft = this.$monthSelector.scrollLeft;
3332

3433
const swiper = makeSwipable(this.$monthSelector);
@@ -49,7 +48,7 @@ export default function MonthSelector(dateArray) {
4948
this.viewportWidth = this.$monthSelector.offsetWidth;
5049

5150
const renderWindow = () => {
52-
this.totalContentWidth = this.itemCount * this.columnWidth;
51+
this.totalContentWidth = (this.itemCount - 1) * this.columnWidth + 176;
5352

5453
this.startNode =
5554
Math.floor(this.scrollLeft / this.columnWidth) - this.nodePadding;
@@ -84,7 +83,7 @@ export default function MonthSelector(dateArray) {
8483
emitMonthChangeEvent(item),
8584
);
8685

87-
if (index === 6) {
86+
if (index === 7) {
8887
selectorItem.active();
8988
}
9089
});
@@ -132,10 +131,6 @@ export default function MonthSelector(dateArray) {
132131

133132
renderWindow();
134133
scrollToMiddle();
135-
136-
listenBreakpoint('from667', (matches) => {
137-
if (matches) scrollToMiddle();
138-
});
139134
}, 0);
140135
}
141136

src/components/Calendar/components/DateSelectorComposer/components/YearSelector/index.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Component } from 'pet-dex-utilities';
22
import { makeSwipable } from '../../../../../../utils/swiper';
33
import SelectorItem from '../SelectorItem';
4-
import { listenBreakpoint } from '../../../../../../utils/breakpoints/breakpoints';
54
import './index.scss';
65

76
const events = ['selector:click', 'year:change'];
@@ -26,7 +25,7 @@ export default function YearSelector(dateArray) {
2625
this.$listContent = this.selected.get('list-content');
2726

2827
this.itemCount = this.dateArray.length;
29-
this.columnWidth = 75;
28+
this.columnWidth = 70;
3029
this.nodePadding = 5;
3130
this.scrollLeft = this.$dateSelector.scrollLeft;
3231

@@ -47,7 +46,7 @@ export default function YearSelector(dateArray) {
4746
this.viewportWidth = this.$dateSelector.offsetWidth;
4847

4948
const renderWindow = () => {
50-
this.totalContentWidth = this.itemCount * this.columnWidth;
49+
this.totalContentWidth = (this.itemCount - 1) * this.columnWidth + 96;
5150

5251
this.startNode =
5352
Math.floor(this.scrollLeft / this.columnWidth) - this.nodePadding;
@@ -122,17 +121,12 @@ export default function YearSelector(dateArray) {
122121
});
123122

124123
const scrollToMiddle = () => {
125-
this.scrollLeft =
126-
this.totalContentWidth / 2 - this.viewportWidth / 2 + 12;
124+
this.scrollLeft = this.totalContentWidth / 2 - this.viewportWidth / 2;
127125
this.$dateSelector.scrollLeft = this.scrollLeft;
128126
};
129127

130128
renderWindow();
131129
scrollToMiddle();
132-
133-
listenBreakpoint('from667', (matches) => {
134-
if (matches) scrollToMiddle();
135-
});
136130
}, 0);
137131
}
138132

src/components/Calendar/components/DateSelectorComposer/index.js

+40-34
Original file line numberDiff line numberDiff line change
@@ -31,53 +31,57 @@ export default function DateSelectorComposer(month, year) {
3131
this.$yearSelector = this.selected.get('year-selector');
3232
this.modalControl = new ModalController(this);
3333

34-
this.mountSelectors = () => {
35-
if (this.yearSelector) this.yearSelector.unmount();
36-
if (this.yearSelected) this.yearSelected.unmount();
34+
const mountMobileSelectors = () => {
3735
if (this.monthSelector) this.monthSelector.unmount();
38-
if (this.monthSelected) this.monthSelected.unmount();
36+
if (this.yearSelector) this.yearSelector.unmount();
3937

40-
this.yearArray = yearArrayGenerator(this.year);
4138
this.monthArray = monthArrayGenerator(this.month);
39+
this.yearArray = yearArrayGenerator(this.year);
4240

43-
this.yearSelector = new YearSelector(this.yearArray, 75);
44-
this.yearSelector.listen('year:change', (newYear) =>
45-
this.emit('year:change', newYear),
41+
this.monthSelector = new MonthSelector(this.monthArray);
42+
this.monthSelector.mount(this.$monthSelector);
43+
this.monthSelector.listen('month:change', (newMonth) =>
44+
this.setMonth(newMonth),
4645
);
4746

48-
this.yearSelected = new DateSelected(this.year);
49-
this.yearSelected.listen('item:click', () =>
50-
this.modalControl.Open(this.yearArray),
51-
);
47+
this.yearSelector = new YearSelector(this.yearArray);
48+
this.yearSelector.mount(this.$yearSelector);
49+
this.yearSelector.listen('year:change', (newYear) => this.setYear(newYear));
50+
};
5251

53-
this.monthSelector = new MonthSelector(this.monthArray, 120);
54-
this.monthSelector.listen('month:change', (newMonth) =>
55-
this.emit('month:change', newMonth),
56-
);
52+
const mountDesktopSelectors = () => {
53+
if (this.monthSelected) this.monthSelected.unmount();
54+
if (this.yearSelected) this.yearSelected.unmount();
55+
56+
this.monthArray = monthArrayGenerator(this.month);
57+
this.yearArray = yearArrayGenerator(this.year);
5758

5859
this.monthSelected = new DateSelected(MONTHS[this.month]);
60+
this.monthSelected.mount(this.$monthSelector);
5961
this.monthSelected.listen('item:click', () =>
6062
this.modalControl.Open(this.monthArray),
6163
);
6264

63-
listenBreakpoint('from667', (matches) => {
64-
if (matches) {
65-
this.monthSelector.unmount();
66-
this.yearSelector.unmount();
67-
68-
this.monthSelected.mount(this.$monthSelector);
69-
this.yearSelected.mount(this.$yearSelector);
70-
} else {
71-
this.yearSelected.unmount();
72-
this.monthSelected.unmount();
73-
74-
this.yearSelector.mount(this.$yearSelector);
75-
this.monthSelector.mount(this.$monthSelector);
76-
}
77-
});
65+
this.yearSelected = new DateSelected(this.year);
66+
this.yearSelected.mount(this.$yearSelector);
67+
this.yearSelected.listen('item:click', () =>
68+
this.modalControl.Open(this.yearArray),
69+
);
7870
};
7971

80-
this.mountSelectors();
72+
listenBreakpoint('from667', (matches) => {
73+
if (matches) {
74+
if (this.monthSelector) this.monthSelector.unmount();
75+
if (this.yearSelector) this.yearSelector.unmount();
76+
77+
mountDesktopSelectors();
78+
} else {
79+
if (this.monthSelected) this.monthSelected.unmount();
80+
if (this.yearSelected) this.yearSelected.unmount();
81+
82+
mountMobileSelectors();
83+
}
84+
});
8185

8286
window.addEventListener('click', (event) =>
8387
this.modalControl.CloseOnClickOutside(event),
@@ -89,14 +93,16 @@ DateSelectorComposer.prototype = Object.assign(
8993
Component.prototype,
9094
{
9195
setMonth(month) {
96+
if (this.monthSelected) this.monthSelected.setDate(MONTHS[month]);
97+
9298
this.month = month;
93-
this.mountSelectors();
9499
this.emit('month:change', month);
95100
},
96101

97102
setYear(year) {
103+
if (this.yearSelected) this.yearSelected.setDate(year);
104+
98105
this.year = year;
99-
this.mountSelectors();
100106
this.emit('year:change', year);
101107
},
102108
},

0 commit comments

Comments
 (0)