-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from agentlab/issue-29-add-scrolling-horizonta…
…l-cards-list GH-29 Add horizontal scroll data control
- Loading branch information
Showing
11 changed files
with
715 additions
and
96 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from 'react'; | ||
|
||
export const Card = ({ itemId, onClick, children, style }: any) => { | ||
return ( | ||
<div | ||
onClick={() => onClick()} | ||
style={{ | ||
display: 'inline-block', | ||
margin: '0 10px', | ||
width: '260px', | ||
userSelect: 'none', | ||
...style, | ||
}} | ||
tabIndex={0} | ||
className='card'> | ||
{children} | ||
</div> | ||
); | ||
}; |
109 changes: 109 additions & 0 deletions
109
src/data-controls/HorizontalScroll/HorizontalScroll.tsx
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 |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { IViewKindElement } from '../../models/uischema'; | ||
import { Card } from './Card'; | ||
import { DispatchCell } from '../../DispatchCell'; | ||
import { ScrollMenu, VisibilityContext } from 'react-horizontal-scrolling-menu'; | ||
import { LeftArrow, RightArrow } from './arrows'; | ||
import useDrag from './useDrag'; | ||
|
||
import './styles.css'; | ||
import './hideScrollbar.css'; | ||
|
||
type scrollVisibilityApiType = React.ContextType<typeof VisibilityContext>; | ||
|
||
export const HorizontalScrollRenderer: React.FC<any> = (props) => { | ||
const { viewKind, viewKindElement, viewDescr, viewDescrElement, child, schema, getData, onSelect } = props; | ||
const [dataSource, setDataSource] = useState(child); | ||
const [selected, setSelected] = useState<string>(''); | ||
const template = viewKindElement?.options?.elementTemplate || null; | ||
|
||
const { dragStart, dragStop, dragMove, dragging } = useDrag(); | ||
const handleDrag = | ||
({ scrollContainer }: scrollVisibilityApiType) => | ||
(ev: React.MouseEvent) => | ||
dragMove(ev, (newPos) => { | ||
if (scrollContainer.current) { | ||
const currentScroll = scrollContainer.current.scrollLeft; | ||
scrollContainer.current.scrollLeft = currentScroll + newPos; | ||
} | ||
}); | ||
|
||
const handleItemClick = (itemId: string) => () => { | ||
if (dragging) { | ||
return false; | ||
} | ||
if (selected !== itemId) { | ||
setSelected(itemId); | ||
onSelect(itemId); | ||
} | ||
}; | ||
|
||
const createCell = (data: any, id: string | number) => | ||
template ? ( | ||
template.map((e: IViewKindElement, idx: number) => ( | ||
<DispatchCell | ||
id={String(id) + String(idx)} | ||
key={String(id) + String(idx)} | ||
viewKind={viewKind} | ||
viewKindElement={e} | ||
viewDescr={viewDescr} | ||
viewDescrElement={viewDescrElement} | ||
schema={schema} | ||
data={data} | ||
rowData={data} | ||
/> | ||
)) | ||
) : ( | ||
<span key={id}>{data['@id']}</span> | ||
); | ||
|
||
const onUpdate = ({ isLastItemVisible }: scrollVisibilityApiType) => { | ||
if (isLastItemVisible) { | ||
const newDataSource = dataSource.concat(getData()); | ||
console.log('push new items'); | ||
setDataSource(newDataSource); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
setDataSource(child); | ||
}, [child]); | ||
|
||
return ( | ||
<div onMouseLeave={dragStop}> | ||
<ScrollMenu | ||
LeftArrow={LeftArrow} | ||
RightArrow={RightArrow} | ||
onWheel={onWheel} | ||
onMouseDown={() => dragStart} | ||
onMouseUp={() => dragStop} | ||
onMouseMove={handleDrag} | ||
onUpdate={onUpdate}> | ||
{dataSource.map((data: any, idx: number) => ( | ||
<Card | ||
key={data['@id']} | ||
itemId={data['@id']} | ||
onClick={handleItemClick(data['@id'])} | ||
style={viewKindElement.options.style}> | ||
{createCell(data, idx)} | ||
</Card> | ||
))} | ||
</ScrollMenu> | ||
</div> | ||
); | ||
}; | ||
|
||
function onWheel(apiObj: scrollVisibilityApiType, ev: React.WheelEvent): void { | ||
const isThouchpad = Math.abs(ev.deltaX) !== 0 || Math.abs(ev.deltaY) < 15; | ||
|
||
if (isThouchpad) { | ||
ev.stopPropagation(); | ||
return; | ||
} | ||
|
||
if (ev.deltaY < 0) { | ||
apiObj.scrollNext(); | ||
} else if (ev.deltaY > 0) { | ||
apiObj.scrollPrev(); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React from 'react'; | ||
|
||
import { VisibilityContext } from 'react-horizontal-scrolling-menu'; | ||
import './styles.css'; | ||
|
||
function Arrow({ | ||
children, | ||
disabled, | ||
onClick, | ||
}: { | ||
children: React.ReactNode; | ||
disabled: boolean; | ||
onClick: VoidFunction; | ||
}) { | ||
return ( | ||
<button disabled={disabled} onClick={onClick} className='arrow'> | ||
{children} | ||
</button> | ||
); | ||
} | ||
|
||
export function LeftArrow() { | ||
const { isFirstItemVisible, scrollPrev, visibleItemsWithoutSeparators } = React.useContext(VisibilityContext); | ||
|
||
const [disabled, setDisabled] = React.useState(!visibleItemsWithoutSeparators.length && isFirstItemVisible); | ||
React.useEffect(() => { | ||
if (visibleItemsWithoutSeparators.length) { | ||
setDisabled(isFirstItemVisible); | ||
} | ||
}, [isFirstItemVisible, visibleItemsWithoutSeparators]); | ||
|
||
return ( | ||
<Arrow disabled={disabled} onClick={() => scrollPrev()}> | ||
{'<'} | ||
</Arrow> | ||
); | ||
} | ||
|
||
export function RightArrow() { | ||
const { isLastItemVisible, scrollNext, visibleItemsWithoutSeparators } = React.useContext(VisibilityContext); | ||
|
||
const [disabled, setDisabled] = React.useState(!visibleItemsWithoutSeparators.length && isLastItemVisible); | ||
React.useEffect(() => { | ||
if (visibleItemsWithoutSeparators.length) { | ||
setDisabled(isLastItemVisible); | ||
} | ||
}, [isLastItemVisible, visibleItemsWithoutSeparators]); | ||
|
||
return ( | ||
<Arrow disabled={disabled} onClick={() => scrollNext()}> | ||
{'>'} | ||
</Arrow> | ||
); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.react-horizontal-scrolling-menu--scroll-container::-webkit-scrollbar { | ||
display: none; | ||
} | ||
|
||
.react-horizontal-scrolling-menu--scroll-container { | ||
-ms-overflow-style: none; /* IE and Edge */ | ||
scrollbar-width: none; /* Firefox */ | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './HorizontalScroll'; |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
* { | ||
box-sizing: border-box; | ||
} | ||
|
||
.arrow { | ||
width: 50px; | ||
height: 50px; | ||
align-self: center; | ||
border-radius: 8px; | ||
margin: 0 3px 0 3px; | ||
background-color: white; | ||
box-shadow: 0 0 3px 1px rgba(0, 0, 0, 0.12); | ||
border: 1px solid rgb(222, 222, 222); | ||
cursor: pointer; | ||
text-align: center; | ||
vertical-align: middle; | ||
right: 1%; | ||
user-select: none; | ||
font-size: 1.2rem; | ||
color: rgb(136, 135, 135); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React from 'react'; | ||
|
||
export default function useDrag() { | ||
const [clicked, setClicked] = React.useState(false); | ||
const [dragging, setDragging] = React.useState(false); | ||
const [position, setPosition] = React.useState(0); | ||
const [diff, setDiff] = React.useState(0); | ||
|
||
const dragStart = React.useCallback((ev: React.MouseEvent) => { | ||
setPosition(ev.clientX); | ||
setDiff(0); | ||
setClicked(true); | ||
}, []); | ||
|
||
const dragStop = React.useCallback( | ||
() => | ||
window.requestAnimationFrame(() => { | ||
setDragging(false); | ||
setClicked(false); | ||
}), | ||
[], | ||
); | ||
|
||
const dragMove = React.useCallback( | ||
(ev: React.MouseEvent, cb: (newPos: number) => void) => { | ||
const newDiff = position - ev.clientX; | ||
|
||
const movedEnough = Math.abs(newDiff) > 5; | ||
|
||
if (clicked && movedEnough) { | ||
setDragging(true); | ||
} | ||
|
||
if (dragging && movedEnough) { | ||
setPosition(ev.clientX); | ||
setDiff(newDiff); | ||
cb(newDiff); | ||
} | ||
}, | ||
[clicked, dragging, position], | ||
); | ||
|
||
return { | ||
dragStart, | ||
dragStop, | ||
dragMove, | ||
diff, | ||
dragging, | ||
position, | ||
setDragging, | ||
setDiff, | ||
setPosition, | ||
}; | ||
} |
Oops, something went wrong.