-
Notifications
You must be signed in to change notification settings - Fork 57
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
feat(Listbox): tap range select #1208
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9dab7f3
feat: wip select on multi tap
DanielS-Qlik 4ad669a
refactor: wip use select from listbox
DanielS-Qlik 35bbaf8
refactor: wip, select on onTouchEnd
DanielS-Qlik fa9058e
fix: remove select ref
DanielS-Qlik 7755ff6
fix: only range select if range is > 7
DanielS-Qlik 8581062
Merge branch 'master' into ild/tap-range-select
DanielS-Qlik 82992dc
test: add test
DanielS-Qlik ffaf757
fix: prevent gesture events in listbox
DanielS-Qlik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,47 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
import { useEffect, useCallback, useRef } from 'react'; | ||
import { selectValues, fillRange, getElemNumbersFromPages } from './listbox-selections'; | ||
import rowColClasses from '../../components/ListBoxRowColumn/helpers/classes'; | ||
|
||
const dataItemSelector = `.${rowColClasses.fieldRoot}`; | ||
const getKeyAsToggleSelected = (event) => !(event?.metaKey || event?.ctrlKey); | ||
|
||
export default function useSelectionsInteractions({ selectionState, selections, checkboxes = false, doc = document }) { | ||
export default function useSelectionsInteractions({ | ||
selectionState, | ||
selections, | ||
checkboxes = false, | ||
doc = document, | ||
loaderRef, | ||
}) { | ||
const currentSelect = useRef({ | ||
startElemNumber: undefined, | ||
elemNumbers: [], | ||
isRange: false, | ||
toggle: false, | ||
active: false, | ||
touchElemNumbers: [], | ||
touchRangeSmall: false, | ||
}); | ||
|
||
useEffect(() => { | ||
if (!loaderRef.current?._listRef?._outerRef) { | ||
return undefined; | ||
} | ||
const preventGestureStart = (e) => e.preventDefault(); | ||
const preventGestureChange = (e) => e.preventDefault(); | ||
const preventGestureEnd = (e) => e.preventDefault(); | ||
|
||
const listRef = loaderRef.current._listRef._outerRef; | ||
listRef.addEventListener('gesturestart', preventGestureStart); | ||
listRef.addEventListener('gesturechange', preventGestureChange); | ||
listRef.addEventListener('gestureend', preventGestureEnd); | ||
return () => { | ||
listRef.removeEventListener('gesturestart', preventGestureStart); | ||
listRef.removeEventListener('gesturechange', preventGestureChange); | ||
listRef.removeEventListener('gestureend', preventGestureEnd); | ||
}; | ||
}, [loaderRef.current?._listRef?._outerRef]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can maybe be a problem as |
||
|
||
// eslint-disable-next-line arrow-body-style | ||
const doSelect = () => { | ||
selectionState.setSelectableValuesUpdating(); | ||
|
@@ -23,14 +53,17 @@ export default function useSelectionsInteractions({ selectionState, selections, | |
}); | ||
}; | ||
|
||
const getRange = (start, end) => { | ||
const elemNumbersOrdered = getElemNumbersFromPages(selectionState.enginePages); | ||
return fillRange([start, end], elemNumbersOrdered); | ||
}; | ||
|
||
const addToRange = (elemNumber) => { | ||
const { startElemNumber } = currentSelect.current; | ||
if (startElemNumber === elemNumber) { | ||
return; | ||
} | ||
const rangeEnds = [currentSelect.current.startElemNumber, elemNumber]; | ||
const elemNumbersOrdered = getElemNumbersFromPages(selectionState.enginePages); | ||
const toMaybeAdd = fillRange(rangeEnds, elemNumbersOrdered); | ||
const toMaybeAdd = getRange(currentSelect.current.startElemNumber, elemNumber); | ||
selectionState.updateItems(toMaybeAdd, true, currentSelect.current.elemNumbers); | ||
}; | ||
|
||
|
@@ -138,6 +171,58 @@ export default function useSelectionsInteractions({ selectionState, selections, | |
addToRange(elemNumber); | ||
}, []); | ||
|
||
const onTouchStart = useCallback((event) => { | ||
// Handle range selection with two finger touch | ||
if ( | ||
currentSelect.current.active || | ||
currentSelect.current.isRange || | ||
selectionState.isSingleSelect || | ||
event.touches.length <= 1 | ||
) { | ||
return; | ||
} | ||
if (event.touches.length > 2) { | ||
doSelect(); | ||
return; | ||
} | ||
const startTouchElemNumber = Number(event.touches[0].target?.closest(dataItemSelector)?.getAttribute('data-n')); | ||
const endTouchElemNumber = Number(event.touches[1].target?.closest(dataItemSelector)?.getAttribute('data-n')); | ||
|
||
if (Number.isNaN(startTouchElemNumber) || Number.isNaN(startTouchElemNumber)) { | ||
doSelect(); | ||
return; | ||
} | ||
|
||
currentSelect.current.active = true; | ||
const range = getRange(startTouchElemNumber, endTouchElemNumber); | ||
if (range.length < 7) { | ||
currentSelect.current.touchRangeSmall = true; | ||
} | ||
|
||
currentSelect.current.elemNumbers = []; | ||
currentSelect.current.touchElemNumbers = [startTouchElemNumber, endTouchElemNumber]; | ||
}, []); | ||
|
||
const onTouchEnd = useCallback(() => { | ||
if (currentSelect.current.touchElemNumbers.length !== 2) { | ||
return; | ||
} | ||
if (currentSelect.current.touchRangeSmall) { | ||
currentSelect.current.touchRangeSmall = false; | ||
currentSelect.current.touchElemNumbers = []; | ||
currentSelect.current.active = false; | ||
return; | ||
} | ||
|
||
const [startTouchElemNumber, endTouchElemNumber] = currentSelect.current.touchElemNumbers; | ||
currentSelect.current.startElemNumber = startTouchElemNumber; | ||
addToRange(endTouchElemNumber); | ||
currentSelect.current.touchElemNumbers = []; | ||
currentSelect.current.active = false; | ||
currentSelect.current.toggle = true; | ||
doSelect(); | ||
}, []); | ||
|
||
useEffect(() => { | ||
doc.addEventListener('mouseup', onMouseUpDoc); | ||
return () => { | ||
|
@@ -169,7 +254,7 @@ export default function useSelectionsInteractions({ selectionState, selections, | |
if (checkboxes) { | ||
Object.assign(interactionEvents, { onChange }); | ||
} else { | ||
Object.assign(interactionEvents, { onMouseUp, onMouseDown, onMouseEnter }); | ||
Object.assign(interactionEvents, { onMouseUp, onMouseDown, onMouseEnter, onTouchStart, onTouchEnd }); | ||
} | ||
|
||
return { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is this for?