Skip to content

Commit

Permalink
Fix touch handlers and make them passive
Browse files Browse the repository at this point in the history
  • Loading branch information
pradeepnschrodinger committed Nov 10, 2023
1 parent 28e518a commit 4d87da2
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 21 deletions.
1 change: 1 addition & 0 deletions examples/ColumnGroupsResizeReorderExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class ColumnGroupsExample extends React.Component {
rowsCount={dataList.getSize()}
width={1000}
height={500}
touchScrollEnabled={true}
{...this.props}
>
{this.state.columnGroupOrder.map(function (columnGroupKey, i) {
Expand Down
1 change: 1 addition & 0 deletions examples/ReorderExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class ReorderExample extends React.Component {
rowsCount={dataList.getSize()}
width={1000}
height={500}
touchScrollEnabled={true}
{...this.props}
>
{this.state.columnOrder.map(function (columnKey, i) {
Expand Down
1 change: 1 addition & 0 deletions src/FixedDataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ class FixedDataTable extends React.Component {
fixedRightColumns={fixedRightColumnGroups}
scrollableColumns={scrollableColumnGroups}
visible={true}
touchEnabled={touchScrollEnabled}
onColumnResizeEndCallback={onColumnResizeEndCallback}
onColumnReorderEndCallback={onColumnReorderEndCallback}
showScrollbarY={scrollEnabledY}
Expand Down
2 changes: 2 additions & 0 deletions src/FixedDataTableCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ class FixedDataTableCell extends React.Component {
isVisible,
columnKey,
isHeaderOrFooter,
touchEnabled,
...props
} = this.props;

Expand Down Expand Up @@ -205,6 +206,7 @@ class FixedDataTableCell extends React.Component {
);

let cellProps = {
touchEnabled,
isVisible,
isHeader: this.props.isHeader,
isGroupHeader: this.props.isGroupHeader,
Expand Down
1 change: 1 addition & 0 deletions src/FixedDataTableCellDefault.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class FixedDataTableCellDefault extends React.Component {
isGroupHeader,
maxWidth,
minWidth,
touchEnabled,
...props
} = this.props;

Expand Down
1 change: 1 addition & 0 deletions src/FixedDataTableCellDefaultDeprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class FixedDataTableCellDefault extends React.Component {
isGroupHeader,
maxWidth,
minWidth,
touchEnabled,
...props
} = this.props;

Expand Down
5 changes: 4 additions & 1 deletion src/api/apiData.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ const getApiDataSelector = () =>
(state) => state.maxScrollX,
(state) => columnWidths(state).availableScrollWidth,
(state) => state.isRTL,
(state) => state.touchScrollEnabled,
],
(
/*number*/ tableHeight,
/*number*/ groupHeaderHeight,
/*number*/ scrollX,
/*number*/ maxScrollX,
/*number*/ availableScrollWidth,
/*boolean*/ isRTL
/*boolean*/ isRTL,
/*boolean*/ touchEnabled
) => {
return {
tableHeight,
Expand All @@ -36,6 +38,7 @@ const getApiDataSelector = () =>
maxScrollX,
availableScrollWidth,
isRTL,
touchEnabled,
};
}
);
Expand Down
17 changes: 13 additions & 4 deletions src/plugins/ResizeReorder/ReorderCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,10 @@ class ReorderCell extends React.PureComponent {
onColumnReorderStart,
onColumnReorderEnd,
reorderStartEvent,
children,
...props
} = this.props;

const { children, left } = props;

let className = joinClasses(
cx({
'public/fixedDataTableCell/resizeReorderCellContainer': true,
Expand Down Expand Up @@ -167,17 +166,27 @@ class ReorderCell extends React.PureComponent {

return (
<div
ref={this.setReorderHandle}
className={cx({
'fixedDataTableCellLayout/columnReorderContainer': true,
'fixedDataTableCellLayout/columnReorderContainer/active': false,
})}
onMouseDown={this.onMouseDown}
onTouchStart={this.onTouchStart}
style={style}
/>
);
}

setReorderHandle = (element) => {
if (element) {
element.addEventListener('mousedown', this.onMouseDown, {
passive: false,
});
element.addEventListener('touchstart', this.onTouchStart, {
passive: false,
});
}
};

onTouchStart = (ev) => {
if (!this.props.touchEnabled) {
return;
Expand Down
72 changes: 64 additions & 8 deletions src/plugins/ResizeReorder/ResizerKnob.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ResizerKnob extends React.PureComponent {
* Ref to ResizerKnob
* @type {HTMLDivElement}
*/
curRef = null;
resizerKnobRef = null;

/**
*
Expand All @@ -62,8 +62,14 @@ class ResizerKnob extends React.PureComponent {

componentDidMount() {
this.setState({
top: this.curRef.getBoundingClientRect().top,
top: this.resizerKnobRef.getBoundingClientRect().top,
});

this.setupHandlers();
}

componentWillUnmount() {
this.cleanupHandlers();
}

render() {
Expand All @@ -82,18 +88,56 @@ class ResizerKnob extends React.PureComponent {
return (
<div
className={cx('fixedDataTableCellLayout/columnResizerContainer')}
ref={(element) => (this.curRef = element)}
ref={this.setResizerKnobRef}
style={resizerKnobStyle}
onMouseDown={this.onMouseDown}
onTouchStart={this.props.touchEnabled ? this.onMouseDown : null}
onTouchEnd={this.props.touchEnabled ? this.suppressEvent : null}
onTouchMove={this.props.touchEnabled ? this.suppressEvent : null}
>
{resizerLine}
</div>
);
}

setResizerKnobRef = (element) => {
this.resizerKnobRef = element;
};

setupHandlers() {
this.resizerKnobRef.addEventListener('mousedown', this.onMouseDown, {
passive: false,
});
this.resizerKnobRef.addEventListener('touchstart', this.onTouchStart, {
passive: false,
});
this.resizerKnobRef.addEventListener(
'touchmove',
this.suppressEventIfInTouchMode,
{ passive: false }
);
this.resizerKnobRef.addEventListener(
'touchend',
this.suppressEventIfInTouchMode,
{ passive: false }
);
}

cleanupHandlers() {
this.resizerKnobRef.removeEventListener('mousedown', this.onMouseDown, {
passive: false,
});
this.resizerKnobRef.removeEventListener('touchstart', this.onTouchStart, {
passive: false,
});
this.resizerKnobRef.removeEventListener(
'touchmove',
this.suppressEventIfInTouchMode,
{ passive: false }
);
this.resizerKnobRef.removeEventListener(
'touchend',
this.suppressEventIfInTouchMode,
{ passive: false }
);
}

/**
* Registers event listeners for mouse tracking
* @param {MouseEvent} event
Expand All @@ -108,6 +152,15 @@ class ResizerKnob extends React.PureComponent {
this.mouseMoveTracker.captureMouseMoves(event);
};

/**
* @param {TouchEvent} event The touch start event
*/
onTouchStart = (event) => {
if (this.props.touchEnabled) {
this.onMouseDown(event);
}
};

/**
* @param {MouseEvent} ev Mouse down event
*/
Expand Down Expand Up @@ -185,7 +238,10 @@ class ResizerKnob extends React.PureComponent {
/**
* @param {Object} event
*/
suppressEvent = (event) => {
suppressEventIfInTouchMode = (event) => {
if (!this.props.touchEnabled) {
return;
}
event.preventDefault();
event.stopPropagation();
};
Expand Down
13 changes: 8 additions & 5 deletions src/vendor_upstream/dom/DOMMouseMoveTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,20 @@ class DOMMouseMoveTracker {
this._eventTouchStartToken = EventListener.listen(
this._domNode,
'touchstart',
this._onMouseMove
this._onMouseMove,
{ passive: false }
);
this._eventTouchMoveToken = EventListener.listen(
this._domNode,
event.target,
'touchmove',
this._onMouseMove
this._onMouseMove,
{ passive: false }
);
this._eventTouchEndToken = EventListener.listen(
this._domNode,
event.target,
'touchend',
this._onMouseUp
this._onMouseUp,
{ passive: false }
);
}

Expand Down
7 changes: 4 additions & 3 deletions src/vendor_upstream/stubs/EventListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ const EventListener = {
* @param {DOMEventTarget} target DOM element to register listener on.
* @param {string} eventType Event type, e.g. 'click' or 'mouseover'.
* @param {function} callback Callback function.
* @param {object} options Extra options to customize the listener
* @return {object} Object with a `remove` method.
*/
listen(target, eventType, callback) {
listen(target, eventType, callback, options = {}) {
if (target.addEventListener) {
target.addEventListener(eventType, callback, false);
target.addEventListener(eventType, callback, options || false);
return {
remove() {
target.removeEventListener(eventType, callback, false);
target.removeEventListener(eventType, callback, options || false);
},
};
} else if (target.attachEvent) {
Expand Down

0 comments on commit 4d87da2

Please sign in to comment.