Skip to content
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

feature/touchscreen-friendly #608

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
3 changes: 2 additions & 1 deletion src/components/Calendar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ class Calendar extends PureComponent {
<div
className={classnames(this.styles.calendarWrapper, className)}
onMouseUp={() => this.setState({ drag: { status: false, range: {} } })}
onTouchEnd={() => this.setState({ drag: { status: false, range: {} } })}
onMouseLeave={() => {
this.setState({ drag: { status: false, range: {} } });
}}>
Expand Down Expand Up @@ -493,7 +494,7 @@ class Calendar extends PureComponent {
isVertical ? this.styles.monthsVertical : this.styles.monthsHorizontal
)}>
{new Array(this.props.months).fill(null).map((_, i) => {
let monthStep = addMonths(this.state.focusedDate, i);;
let monthStep = addMonths(this.state.focusedDate, i);
if (this.props.calendarFocus === 'backwards') {
monthStep = subMonths(this.state.focusedDate, this.props.months - 1 - i);
}
Expand Down
63 changes: 60 additions & 3 deletions src/components/DayCell/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class DayCell extends Component {
this.state = {
hover: false,
active: false,
touchOverDay: props.day.toISOString(),
};
}

Expand All @@ -21,6 +22,59 @@ class DayCell extends Component {
else onMouseUp(day);
}
};

handleTouchEvent = event => {
const { day, disabled, onMouseDown, onMouseUp, onMouseEnter, onPreviewChange } = this.props;
const stateChanges = {};
if (disabled) {
onPreviewChange();
return;
}

const findButtonElem = elems => {
return elems.find(elem => elem.nodeName === 'BUTTON');
};

const isNewDay = targetElements => {
const buttonElem = findButtonElem(targetElements);
if (targetElements && buttonElem && buttonElem.classList.contains('rdrDay')) {
const newDay = buttonElem.getAttribute('data-day');
if (newDay && newDay !== this.state.touchOverDay) {
return newDay;
}
}
return null;
};

switch (event.type) {
case 'touchmove':
{
const targetElements = document.elementsFromPoint(
event.touches[0].clientX,
event.touches[0].clientY
);
if (targetElements.length === 0) return;
const newDay = isNewDay(targetElements);
if (newDay) {
onMouseEnter(new Date(newDay));
onPreviewChange(new Date(newDay));
this.setState({ touchOverDay: newDay });
}
}
break;
case 'touchend':
{
const endDate = new Date(this.state.touchOverDay);
onMouseUp(endDate);
this.setState({ touchOverDay: null });
}
break;
case 'touchstart':
stateChanges.active = true;
onMouseDown(day);
break;
}
};
handleMouseEvent = event => {
const { day, disabled, onPreviewChange, onMouseEnter, onMouseDown, onMouseUp } = this.props;
const stateChanges = {};
Expand Down Expand Up @@ -155,8 +209,12 @@ class DayCell extends Component {
return (
<button
type="button"
data-day={this.props.day.toISOString()}
onMouseEnter={this.handleMouseEvent}
onMouseLeave={this.handleMouseEvent}
onTouchStart={this.handleTouchEvent}
onTouchMove={this.handleTouchEvent}
onTouchEnd={this.handleTouchEvent}
onFocus={this.handleMouseEvent}
onMouseDown={this.handleMouseEvent}
onMouseUp={this.handleMouseEvent}
Expand All @@ -170,10 +228,9 @@ class DayCell extends Component {
{this.renderSelectionPlaceholders()}
{this.renderPreviewPlaceholder()}
<span className={this.props.styles.dayNumber}>
{
dayContentRenderer?.(this.props.day) ||
{dayContentRenderer?.(this.props.day) || (
<span>{format(this.props.day, this.props.dayDisplayFormat)}</span>
}
)}
</span>
</button>
);
Expand Down