-
-
Notifications
You must be signed in to change notification settings - Fork 3
fix: honor offset in non-virtual scrollTo, export scroll config types #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
Open
aojunhao123
wants to merge
5
commits into
react-component:master
Choose a base branch
from
aojunhao123:fix/non-virtual-scroll-offset
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bdf86cd
fix: honor offset in non-virtual scrollTo, export scroll config types
aojunhao123 9e9b461
fix: apply sticky header offset for auto-aligned scrollTo
aojunhao123 7b6970c
update
aojunhao123 0303776
fix: tag row keys by type so item and group scroll targets stay distinct
aojunhao123 779a902
test: cover virtual scrollTo passthrough branches
aojunhao123 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 hidden or 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 @@ | ||
| --- | ||
| title: Scroll To | ||
| nav: | ||
| title: Demo | ||
| path: /demo | ||
| --- | ||
|
|
||
| <code src="../examples/scroll-to.tsx"></code> |
This file contains hidden or 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,185 @@ | ||
| import React, { useRef, useState } from 'react'; | ||
| import Listy, { | ||
| type ListyRef, | ||
| type ListyScrollToConfig, | ||
| type ScrollAlign, | ||
| } from '@rc-component/listy'; | ||
| import '../../assets/index.less'; | ||
|
|
||
| const GROUP_SIZE = 15; | ||
| const GROUP_COUNT = 6; | ||
|
|
||
| interface Row { | ||
| id: number; | ||
| name: string; | ||
| group: string; | ||
| } | ||
|
|
||
| const items: Row[] = Array.from( | ||
| { length: GROUP_SIZE * GROUP_COUNT }, | ||
| (_, index) => ({ | ||
| id: index, | ||
| name: `Item ${index}`, | ||
| group: `Group ${Math.floor(index / GROUP_SIZE)}`, | ||
| }), | ||
| ); | ||
|
|
||
| const GROUP_KEYS = Array.from({ length: GROUP_COUNT }, (_, i) => `Group ${i}`); | ||
|
|
||
| const controlRow: React.CSSProperties = { | ||
| display: 'flex', | ||
| flexWrap: 'wrap', | ||
| alignItems: 'center', | ||
| gap: 8, | ||
| }; | ||
|
|
||
| export default () => { | ||
| const listRef = useRef<ListyRef>(null); | ||
| const [virtual, setVirtual] = useState(true); | ||
| const [align, setAlign] = useState<ScrollAlign>('top'); | ||
| const [offset, setOffset] = useState(0); | ||
| const [itemKey, setItemKey] = useState(50); | ||
| const [groupKey, setGroupKey] = useState('Group 3'); | ||
| const [lastConfig, setLastConfig] = useState<ListyScrollToConfig>(); | ||
|
|
||
| // Single entry point so the demo can echo the exact config it passes in. | ||
| const run = (config: ListyScrollToConfig) => { | ||
| setLastConfig(config); | ||
| listRef.current?.scrollTo(config); | ||
| }; | ||
|
|
||
| return ( | ||
| <div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}> | ||
| <div style={controlRow}> | ||
| <label> | ||
| <input | ||
| type="checkbox" | ||
| checked={virtual} | ||
| onChange={(e) => setVirtual(e.target.checked)} | ||
| />{' '} | ||
| virtual | ||
| </label> | ||
|
|
||
| <label> | ||
| align{' '} | ||
| <select | ||
| value={align} | ||
| onChange={(e) => setAlign(e.target.value as ScrollAlign)} | ||
| > | ||
| <option value="top">top</option> | ||
| <option value="bottom">bottom</option> | ||
| <option value="auto">auto</option> | ||
| </select> | ||
| </label> | ||
|
|
||
| <label> | ||
| offset{' '} | ||
| <input | ||
| type="number" | ||
| value={offset} | ||
| style={{ width: 64 }} | ||
| onChange={(e) => setOffset(Number(e.target.value) || 0)} | ||
| /> | ||
| </label> | ||
| </div> | ||
|
|
||
| {/* number | { top } — absolute pixel scroll */} | ||
| <div style={controlRow}> | ||
| <button type="button" onClick={() => run(0)}> | ||
| scrollTo(0) | ||
| </button> | ||
| <button type="button" onClick={() => run(400)}> | ||
| scrollTo(400) | ||
| </button> | ||
| <button type="button" onClick={() => run({ top: 200 })}> | ||
| scrollTo({'{ top: 200 }'}) | ||
| </button> | ||
| </div> | ||
|
|
||
| {/* { key, align, offset } — scroll to an item */} | ||
| <div style={controlRow}> | ||
| <label> | ||
| key{' '} | ||
| <input | ||
| type="number" | ||
| value={itemKey} | ||
| style={{ width: 64 }} | ||
| onChange={(e) => setItemKey(Number(e.target.value) || 0)} | ||
| /> | ||
| </label> | ||
| <button | ||
| type="button" | ||
| onClick={() => run({ key: itemKey, align, offset })} | ||
| > | ||
| scrollTo item | ||
| </button> | ||
|
|
||
| {/* { groupKey, align, offset } — scroll to a group header */} | ||
| <label> | ||
| group{' '} | ||
| <select value={groupKey} onChange={(e) => setGroupKey(e.target.value)}> | ||
| {GROUP_KEYS.map((key) => ( | ||
| <option key={key} value={key}> | ||
| {key} | ||
| </option> | ||
| ))} | ||
| </select> | ||
| </label> | ||
| <button | ||
| type="button" | ||
| onClick={() => run({ groupKey, align, offset })} | ||
| > | ||
| scrollTo group | ||
| </button> | ||
| </div> | ||
|
|
||
| <div> | ||
| last config:{' '} | ||
| <code>{lastConfig === undefined ? '—' : JSON.stringify(lastConfig)}</code> | ||
| </div> | ||
|
|
||
| <Listy | ||
| // Remount on mode switch so both branches start from a clean scroll. | ||
| key={virtual ? 'virtual' : 'raw'} | ||
| ref={listRef} | ||
| virtual={virtual} | ||
| height={360} | ||
| itemHeight={40} | ||
| items={items} | ||
| rowKey="id" | ||
| sticky | ||
| itemRender={(item, index) => { | ||
| const height = 40 + (index % 3) * 16; | ||
| return ( | ||
| <div | ||
| style={{ | ||
| padding: '0 16px', | ||
| height, | ||
| lineHeight: `${height}px`, | ||
| borderBottom: '1px solid #f0f0f0', | ||
| background: '#fff', | ||
| }} | ||
| > | ||
| {item.name} | ||
| </div> | ||
| ); | ||
| }} | ||
| group={{ | ||
| key: (item) => item.group, | ||
| title: (key, groupItems) => ( | ||
| <div | ||
| style={{ | ||
| padding: '10px 16px', | ||
| fontWeight: 600, | ||
| background: '#e6f4ff', | ||
| borderBottom: '1px solid #91caff', | ||
| }} | ||
| > | ||
| {key} · {groupItems.length} items | ||
| </div> | ||
| ), | ||
| }} | ||
| /> | ||
| </div> | ||
| ); | ||
| }; |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.