Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 172
fix: nest scroll should block#288
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
9defbc71801c495cb9654805dae25fc2c91a00367c71fa5d00bad8e5File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| title: Nest | ||
| nav: | ||
| title: Demo | ||
| path: /demo | ||
| --- | ||
| <code src="../../examples/nest.tsx"></code> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import * as React from 'react'; | ||
| import List from '../src/List'; | ||
| import './basic.less'; | ||
| interface Item { | ||
| id: number; | ||
| } | ||
| const data: Item[] = []; | ||
| for (let i = 0; i < 100; i += 1) { | ||
| data.push({ | ||
| id: i, | ||
| }); | ||
| } | ||
| const MyItem: React.ForwardRefRenderFunction<any, Item> = ({ id }, ref) => ( | ||
| <div style={{ padding: 20, background: 'yellow' }} ref={ref}> | ||
| <List | ||
| data={data} | ||
| height={200} | ||
| itemHeight={20} | ||
| itemKey="id" | ||
| style={{ | ||
| border: '1px solid blue', | ||
| boxSizing: 'border-box', | ||
| background: 'white', | ||
| }} | ||
| // debug={`inner_${id}`} | ||
| > | ||
| {(item, index, props) => ( | ||
| <div {...(item as any)} {...props} style={{ height: 20, border: '1px solid cyan' }}> | ||
| {id}-{index} | ||
| </div> | ||
| )} | ||
| </List> | ||
| </div> | ||
| ); | ||
| const ForwardMyItem = React.forwardRef(MyItem); | ||
| const onScroll: React.UIEventHandler<HTMLElement> = (e) => { | ||
| // console.log('scroll:', e.currentTarget.scrollTop); | ||
| }; | ||
| const Demo = () => { | ||
| return ( | ||
| <React.StrictMode> | ||
| <List | ||
| id="list" | ||
| data={data} | ||
| height={800} | ||
| itemHeight={20} | ||
| itemKey="id" | ||
| style={{ | ||
| border: '1px solid red', | ||
| boxSizing: 'border-box', | ||
| }} | ||
| onScroll={onScroll} | ||
| // debug="outer" | ||
| > | ||
| {(item, _, props) => <ForwardMyItem {...item} {...props} />} | ||
| </List> | ||
| </React.StrictMode> | ||
| ); | ||
| }; | ||
| export default Demo; | ||
| /* eslint-enable */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import * as React from 'react'; | ||
| export const WheelLockContext = React.createContext<(lock: boolean) => void>(() => {}); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -7,7 +7,12 @@ const SMOOTH_PTG = 14 / 15; | ||
| export default function useMobileTouchMove( | ||
| inVirtual: boolean, | ||
| listRef: React.RefObject<HTMLDivElement>, | ||
| callback: (isHorizontal: boolean, offset: number, smoothOffset?: boolean) => boolean, | ||
| callback: ( | ||
| isHorizontal: boolean, | ||
| offset: number, | ||
| smoothOffset: boolean, | ||
| e?: TouchEvent, | ||
| ) => boolean, | ||
| ) { | ||
| const touchedRef = useRef(false); | ||
| const touchXRef = useRef(0); | ||
| @@ -34,22 +39,27 @@ export default function useMobileTouchMove( | ||
| touchYRef.current = currentY; | ||
| } | ||
| if (callback(isHorizontal, isHorizontal ? offsetX : offsetY)) { | ||
| const scrollHandled = callback(isHorizontal, isHorizontal ? offsetX : offsetY, false, e); | ||
| if (scrollHandled) { | ||
| e.preventDefault(); | ||
| } | ||
| // Smooth interval | ||
| clearInterval(intervalRef.current); | ||
| intervalRef.current = setInterval(() => { | ||
| if (isHorizontal) { | ||
| offsetX *= SMOOTH_PTG; | ||
| } else { | ||
| offsetY *= SMOOTH_PTG; | ||
| } | ||
| const offset = Math.floor(isHorizontal ? offsetX : offsetY); | ||
| if (!callback(isHorizontal, offset, true) || Math.abs(offset) <= 0.1) { | ||
| clearInterval(intervalRef.current); | ||
| } | ||
| }, 16); | ||
| if (scrollHandled) { | ||
| intervalRef.current = setInterval(() => { | ||
| if (isHorizontal) { | ||
| offsetX *= SMOOTH_PTG; | ||
| } else { | ||
| offsetY *= SMOOTH_PTG; | ||
| } | ||
| const offset = Math.floor(isHorizontal ? offsetX : offsetY); | ||
| if (!callback(isHorizontal, offset, true) || Math.abs(offset) <= 0.1) { | ||
zombieJ marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| clearInterval(intervalRef.current); | ||
| } | ||
| }, 16); | ||
| } | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,9 @@ | ||
| import '@testing-library/jest-dom'; | ||
| import { createEvent, fireEvent, render } from '@testing-library/react'; | ||
| import { act, createEvent, fireEvent, render } from '@testing-library/react'; | ||
| import { mount } from 'enzyme'; | ||
| import { _rs as onLibResize } from 'rc-resize-observer/lib/utils/observerUtil'; | ||
| import { resetWarned } from 'rc-util/lib/warning'; | ||
| import React from 'react'; | ||
| import { act } from 'react-dom/test-utils'; | ||
| import List from '../src'; | ||
| import { spyElementPrototypes } from './utils/domHook'; | ||
| @@ -51,11 +50,13 @@ describe('List.Scroll', () => { | ||
| }); | ||
| function genList(props, func = mount) { | ||
| let node = ( | ||
| <List component="ul" itemKey="id" {...props}> | ||
| {({ id }) => <li>{id}</li>} | ||
| </List> | ||
| ); | ||
| const mergedProps = { | ||
| component: 'ul', | ||
| itemKey: 'id', | ||
| children: ({ id }) => <li>{id}</li>, | ||
| ...props, | ||
| }; | ||
| let node = <List {...mergedProps} />; | ||
| if (props.ref) { | ||
| node = <div>{node}</div>; | ||
| @@ -494,4 +495,43 @@ describe('List.Scroll', () => { | ||
| expect(container.querySelector('.rc-virtual-list-scrollbar-thumb')).toBeVisible(); | ||
| }); | ||
| it('nest scroll', async () => { | ||
| const { container } = genList( | ||
| { | ||
| itemHeight: 20, | ||
| height: 100, | ||
| data: genData(100), | ||
| children: ({ id }) => | ||
| id === '0' ? ( | ||
| <li> | ||
| <List component="ul" itemKey="id" itemHeight={20} height={100} data={genData(100)}> | ||
| {({ id }) => <li>{id}</li>} | ||
| </List> | ||
| </li> | ||
| ) : ( | ||
| <li /> | ||
| ), | ||
| }, | ||
| render, | ||
| ); | ||
| fireEvent.wheel(container.querySelector('ul ul li'), { | ||
| deltaY: 10, | ||
| }); | ||
zombieJ marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| await act(async () => { | ||
| jest.advanceTimersByTime(1000000); | ||
| await Promise.resolve(); | ||
| }); | ||
zombieJ marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| expect(container.querySelectorAll('[data-dev-offset-top]')[0]).toHaveAttribute( | ||
| 'data-dev-offset-top', | ||
| '0', | ||
| ); | ||
| expect(container.querySelectorAll('[data-dev-offset-top]')[1]).toHaveAttribute( | ||
| 'data-dev-offset-top', | ||
| '10', | ||
| ); | ||
zombieJ marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| import React from 'react'; | ||
| import { act, fireEvent, render } from '@testing-library/react'; | ||
| import { mount } from 'enzyme'; | ||
| import { spyElementPrototypes } from './utils/domHook'; | ||
| import React from 'react'; | ||
| import List from '../src'; | ||
| import { spyElementPrototypes } from './utils/domHook'; | ||
| function genData(count) { | ||
| return new Array(count).fill(null).map((_, index) => ({ id: String(index) })); | ||
| @@ -123,11 +124,55 @@ describe('List.Touch', () => { | ||
| const touchEvent = new Event('touchstart'); | ||
| touchEvent.preventDefault = preventDefault; | ||
| wrapper | ||
| .find('.rc-virtual-list-scrollbar') | ||
| .instance() | ||
| .dispatchEvent(touchEvent); | ||
| wrapper.find('.rc-virtual-list-scrollbar').instance().dispatchEvent(touchEvent); | ||
zombieJ marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| expect(preventDefault).toHaveBeenCalled(); | ||
| }); | ||
| it('nest touch', async () => { | ||
| const { container } = render( | ||
| <List component="ul" itemHeight={20} height={100} data={genData(100)}> | ||
| {({ id }) => | ||
| id === '0' ? ( | ||
| <li> | ||
| <List component="ul" itemKey="id" itemHeight={20} height={100} data={genData(100)}> | ||
| {({ id }) => <li>{id}</li>} | ||
| </List> | ||
| </li> | ||
| ) : ( | ||
| <li /> | ||
| ) | ||
| } | ||
| </List>, | ||
| ); | ||
| const targetLi = container.querySelector('ul ul li'); | ||
| fireEvent.touchStart(targetLi, { | ||
| touches: [{ pageY: 0 }], | ||
| }); | ||
| fireEvent.touchMove(targetLi, { | ||
| touches: [{ pageY: -1 }], | ||
| }); | ||
| await act(async () => { | ||
| jest.advanceTimersByTime(1000000); | ||
| await Promise.resolve(); | ||
| }); | ||
| expect(container.querySelectorAll('[data-dev-offset-top]')[0]).toHaveAttribute( | ||
| 'data-dev-offset-top', | ||
| '0', | ||
| ); | ||
| // inner not to be 0 | ||
| expect(container.querySelectorAll('[data-dev-offset-top]')[1]).toHaveAttribute( | ||
| 'data-dev-offset-top', | ||
| ); | ||
| expect(container.querySelectorAll('[data-dev-offset-top]')[1]).not.toHaveAttribute( | ||
| 'data-dev-offset-top', | ||
| '0', | ||
| ); | ||
| }); | ||
zombieJ marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.