Skip to content

Commit

Permalink
chore: clean up how persisting row click events are handled
Browse files Browse the repository at this point in the history
  • Loading branch information
oze4 committed Aug 1, 2021
1 parent 48a08aa commit d735ef8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
16 changes: 11 additions & 5 deletions __tests__/demo/demo-components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,12 +547,18 @@ export function EventTargetErrorOnRowClick(props) {
tableRef={tableRef}
columns={cols}
data={datas}
onSelectionChange={onRowSelectionChanged}
onRowClick={onRowClicked}
// onSelectionChange={onRowSelectionChanged}
// onRowClick={onRowClicked}
components={{
MTableBodyRow: (props, p2) => {
console.log({ props, p2 });
return <MTableBodyRow {...props} persistEvents={true} />;
Row: (props) => {
return (
<MTableBodyRow
{...props}
persistEvents={true}
onRowClick={onRowClicked}
onRowSelected={onRowSelectionChanged}
/>
);
}
}}
options={{
Expand Down
12 changes: 8 additions & 4 deletions src/components/m-table-body-row.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ export default function MTableBodyRow(props) {

const onRowClickListener = useDoubleClick(
onRowClick ? (e) => onClick(e, onRowClick) : undefined,
onDoubleRowClick ? (e) => onClick(e, onDoubleRowClick) : undefined,
persistEvents
onDoubleRowClick ? (e) => onClick(e, onDoubleRowClick) : undefined
);

const getRenderColumns = () => {
Expand Down Expand Up @@ -445,8 +444,13 @@ export default function MTableBodyRow(props) {
<TableRow
selected={hasAnyEditingRow}
{...rowProps}
onClick={onRowClickListener}
hover={!!onRowClick || !!onDoubleRowClick}
onClick={(event) => {
if (persistEvents) {
event.persist();
}
onRowClickListener(event);
}}
hover={onRowClick !== null || onDoubleRowClick !== null}
style={getStyle(props.index, props.level)}
>
{renderColumns}
Expand Down
5 changes: 1 addition & 4 deletions src/utils/hooks/useDoubleClick.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

function useDoubleClick(singleCallback, dbCallback, persistEvents) {
function useDoubleClick(singleCallback, dbCallback) {
const countRef = React.useRef(0);
/** Refs for the timer **/
const timerRef = React.useRef(null);
Expand All @@ -12,9 +12,6 @@ function useDoubleClick(singleCallback, dbCallback, persistEvents) {
inputSingleCallbackRef.current = singleCallback;
});
const onClick = React.useCallback((e) => {
if (persistEvents) {
e.persist();
}
const isDoubleClick = countRef.current + 1 === 2;
const timerIsPresent = timerRef.current;
if (timerIsPresent && isDoubleClick) {
Expand Down

0 comments on commit d735ef8

Please sign in to comment.