Skip to content

Commit

Permalink
refactor: range on hover use provide
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxiong10 committed Feb 5, 2021
1 parent 4aee6dd commit cc0b605
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 44 deletions.
34 changes: 20 additions & 14 deletions __test__/calendar-range.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { mount } from '@vue/test-utils';
import { parse } from 'date-fns';
import CalendarRange from '../src/calendar/calendar-range';
import CalendarPanel from '../src/calendar/calendar-panel';

Expand All @@ -10,19 +11,25 @@ afterEach(() => {

describe('CalendarRange', () => {
it('feat: correct classes', () => {
const start = new Date(2019, 9, 30);
const end = new Date(2019, 10, 2);
wrapper = mount(CalendarRange, {
propsData: {
value: [new Date(2019, 9, 30), new Date(2019, 10, 2)],
value: [start, end],
},
});
const activeTds = wrapper.findAll('.mx-table-date .active');
const rangeTds = wrapper.findAll('.mx-table-date .in-range');
expect(activeTds.length).toBe(2);
expect(activeTds.at(0).text()).toBe('30');
expect(activeTds.at(1).text()).toBe('2');
expect(rangeTds.length).toBe(2);
expect(rangeTds.at(0).text()).toBe('31');
expect(rangeTds.at(1).text()).toBe('1');
const tds = wrapper.findAll('.mx-table-date td');

for (let i = 0; i < tds.length; i++) {
const td = tds.at(i);
const { title } = td.element;
const cell = parse(title, 'yyyy-MM-dd', new Date()).getTime();
if (cell > start.getTime() && cell < end.getTime()) {
expect(td.classes()).toContain('in-range');
} else {
expect(td.classes()).not.toContain('hover-in-range');
}
}
});

it('feat: click range', async () => {
Expand Down Expand Up @@ -87,13 +94,12 @@ describe('CalendarRange', () => {
defaultValue: new Date(2019, 9, 1),
},
});
expect(wrapper.vm.calendars).toEqual([new Date(2019, 9, 1), new Date(2019, 10, 1)]);
const tds = wrapper.findAll('.mx-table-date td');
await tds.at(2).trigger('click');
await tds.at(10).trigger('click');
await tds.at(60).trigger('mouseenter');

for (let i = 0; i < tds.length; i++) {
if (i > 2 && i < 60) {
if (i > 10 && i < 60) {
expect(tds.at(i).classes()).toContain('hover-in-range');
} else {
expect(tds.at(i).classes()).not.toContain('hover-in-range');
Expand All @@ -104,10 +110,10 @@ describe('CalendarRange', () => {

// hover to back
await tds.at(60).trigger('click');
await tds.at(2).trigger('mouseenter');
await tds.at(10).trigger('mouseenter');

for (let i = 0; i < tds.length; i++) {
if (i > 2 && i < 60) {
if (i > 10 && i < 60) {
expect(tds.at(i).classes()).toContain('hover-in-range');
} else {
expect(tds.at(i).classes()).not.toContain('hover-in-range');
Expand Down
4 changes: 0 additions & 4 deletions src/calendar/calendar-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,6 @@ export default {
handleSelectDate(date) {
this.emitDate(date, this.type === 'week' ? 'week' : 'date');
},
handleMouseEnter(cell) {
this.$emit('mouseenter', cell);
},
getMonthCellDate(month) {
return createDate(this.calendarYear, month);
},
Expand Down Expand Up @@ -262,7 +259,6 @@ export default {
onSelect={this.handleSelectDate}
onChangepanel={this.handelPanelChange}
onChangecalendar={this.handleCalendarChange}
onMouseenter={this.handleMouseEnter}
/>
);
},
Expand Down
50 changes: 25 additions & 25 deletions src/calendar/calendar-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { getValidDate, isValidDate, isValidRangeDate, startOfMonth } from '../ut
export default {
name: 'CalendarRange',
components: { CalendarPanel },
provide() {
return {
onDateMouseEnter: this.onDateMouseEnter,
onDateMouseLeave: this.onDateMouseLeave,
};
},
inject: {
prefixClass: {
default: 'mx',
Expand Down Expand Up @@ -66,10 +72,10 @@ export default {
this.innerValue = [date, new Date(NaN)];
}
},
handleCellMouseEnter(cell) {
onDateMouseEnter(cell) {
this.hoveredValue = cell;
},
handleRangeMouseLeave() {
onDateMouseLeave() {
this.hoveredValue = null;
},
emitDate(dates, type) {
Expand Down Expand Up @@ -107,27 +113,26 @@ export default {
},
getRangeClasses(cellDate, currentDates, classnames) {
const classes = [].concat(this.getClasses(cellDate, currentDates, classnames));
if (
currentDates.length === 2 &&
!/disabled|active|not-current-month/.test(classnames) &&
cellDate.getTime() > currentDates[0].getTime() &&
cellDate.getTime() < currentDates[1].getTime()
) {
classes.push('in-range');
} else if (
currentDates.length === 1 &&
this.hoveredValue &&
!/disabled|active/.test(classnames)
) {
let min = this.hoveredValue.getTime();
let max = currentDates[0].getTime();

if (/disabled|active/.test(classnames)) return classes;

const inRange = (data, range, fn = v => v.getTime()) => {
const value = fn(data);
let [min, max] = range.map(fn);
if (min > max) {
[min, max] = [max, min];
}
if (cellDate.getTime() > min && cellDate.getTime() < max) {
classes.push('hover-in-range');
}
return value > min && value < max;
};
if (currentDates.length === 2 && inRange(cellDate, currentDates)) {
return classes.concat('in-range');
}
if (
currentDates.length === 1 &&
this.hoveredValue &&
inRange(cellDate, [currentDates[0], this.hoveredValue])
) {
return classes.concat('hover-in-range');
}

return classes;
Expand All @@ -147,17 +152,12 @@ export default {
const on = {
select: this.handleSelect,
'update:calendar': index === 0 ? this.updateStartCalendar : this.updateEndCalendar,
mouseenter: this.handleCellMouseEnter,
};
return <calendar-panel {...{ props, on }}></calendar-panel>;
});

const { prefixClass } = this;

return (
<div class={`${prefixClass}-range-wrapper`} onMouseleave={this.handleRangeMouseLeave}>
{calendarRange}
</div>
);
return <div class={`${prefixClass}-range-wrapper`}>{calendarRange}</div>;
},
};
16 changes: 15 additions & 1 deletion src/calendar/table-date.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
:class="getCellClasses(cell)"
:title="getCellTitle(cell)"
@mouseenter="handleMouseEnter(cell)"
@mouseleave="handleMouseLeave(cell)"
>
<div>{{ cell.getDate() }}</div>
</td>
Expand Down Expand Up @@ -78,6 +79,12 @@ export default {
prefixClass: {
default: 'mx',
},
onDateMouseEnter: {
default: undefined,
},
onDateMouseLeave: {
default: undefined,
},
},
props: {
calendar: {
Expand Down Expand Up @@ -155,7 +162,14 @@ export default {
this.$emit('changepanel', panel);
},
handleMouseEnter(cell) {
this.$emit('mouseenter', cell);
if (typeof this.onDateMouseEnter === 'function') {
this.onDateMouseEnter(cell);
}
},
handleMouseLeave(cell) {
if (typeof this.onDateMouseLeave === 'function') {
this.onDateMouseLeave(cell);
}
},
handleCellClick(evt) {
let { target } = evt;
Expand Down
1 change: 1 addition & 0 deletions src/style/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@
}
.cell.not-current-month {
color: #ccc;
background: none; // cover the in-range style
}
}

Expand Down

0 comments on commit cc0b605

Please sign in to comment.