Skip to content

Commit

Permalink
Merge branch 'master' into feat/useTitle
Browse files Browse the repository at this point in the history
  • Loading branch information
brickspert authored Jul 6, 2020
2 parents a2c31a5 + 88b8a6d commit ca40243
Show file tree
Hide file tree
Showing 15 changed files with 267 additions and 102 deletions.
114 changes: 37 additions & 77 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import useEventTarget from './useEventTarget';
import useHistoryTravel from './useHistoryTravel';
import useDebounceEffect from './useDebounceEffect';
import useSetState from './useSetState';
import useWhyDidYouUpdate from './useWhyDidYouUpdate';

const useControlledValue: typeof useControllableValue = function (...args) {
console.warn(
Expand Down Expand Up @@ -101,4 +102,5 @@ export {
useHistoryTravel,
useFusionTable,
useSetState,
useWhyDidYouUpdate,
};
2 changes: 1 addition & 1 deletion packages/hooks/src/useCounter/demo/demo1.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default () => {

return (
<div>
<p>{current} [max: 10; min: 0;]</p>
<p>{current} [max: 10; min: 1;]</p>
<div>
<button
type="button"
Expand Down
12 changes: 6 additions & 6 deletions packages/hooks/src/useTextSelection/demo/demo1.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/**
* title: Set 'dom' target
* desc: Specifies the text selection to listen for an dom
* title: Default usage
* desc: Tracking content of user text selection
*
* title.zh-CN: 指定监听 'dom' 元素
* desc.zh-CN: 指定监听某个元素的文本选取。
* title.zh-CN: 默认用法
* desc.zh-CN: 实时获取页面上选择的文本
*/

import React from 'react';
import { useTextSelection } from 'ahooks';

export default () => {
const { text } = useTextSelection(() => document.querySelector('#target-dom'));
const { text } = useTextSelection();
return (
<div>
<p id="target-dom">Only listen to the text selection of this paragraph.</p>
<p>You can select text all page.</p>
<p>Result:{text}</p>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion packages/hooks/src/useTextSelection/demo/demo2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default () => {

return (
<div>
<p id="translate-dom">
<p id="translate-dom" style={{ padding: 20, border: '1px solid' }}>
Translation of this paragraph;Translation of this paragraph;Translation of this paragraph;
</p>
<Popover
Expand Down
10 changes: 5 additions & 5 deletions packages/hooks/src/useTextSelection/demo/demo3.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* title: Default usage
* desc: Tracking content, size, position of user text selection
* title: Listen specified area
* desc: useTextSelection can receive dom or ref, for listen specified area.
*
* title.zh-CN: 默认用法
* desc.zh-CN: 获取用户当前选择的文本内容、大小及其相对于视口的位置
* title.zh-CN: 监听特定区域文本选择
* desc.zh-CN: useTextSelection 可以接收 dom 或 ref,指定监听区域
*/

import React, { useRef } from 'react';
Expand All @@ -14,7 +14,7 @@ export default () => {
const selection = useTextSelection(ref);
return (
<div>
<div ref={ref}>
<div ref={ref} style={{ border: '1px solid', padding: 20 }}>
<p>Please swipe your mouse to select any text on this paragraph.</p>
</div>
<p>Result:{JSON.stringify(selection)}</p>
Expand Down
4 changes: 2 additions & 2 deletions packages/hooks/src/useTextSelection/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ Tracking content, size, position of user text selection

## Examples

### Set 'dom' target
### Default usage

<code src="./demo/demo1.tsx" />

### Use Ref
### Listen specified area

<code src="./demo/demo3.tsx" />

Expand Down
4 changes: 2 additions & 2 deletions packages/hooks/src/useTextSelection/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ legacy: /zh-CN/dom/use-text-selection

## 代码演示

### 使用 Dom
### 默认用法

<code src="./demo/demo1.tsx" />

### 使用 Ref
### 监听特定区域文本选择

<code src="./demo/demo3.tsx" />

Expand Down
6 changes: 3 additions & 3 deletions packages/hooks/src/useVirtualList/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('useVirtualList', () => {
};
wrapperProps: {
style: {
paddingTop: number;
marginTop: number;
height: number;
};
};
Expand Down Expand Up @@ -95,8 +95,8 @@ describe('useVirtualList', () => {
expect((hook.result.current.list[5] as { data: number }).data).toBe(25);
expect((hook.result.current.list[5] as { index: number }).index).toBe(25);

expect(hook.result.current.wrapperProps.style.paddingTop).toBe(20 * averageHeight);
expect(hook.result.current.wrapperProps.style.height).toBe(99998 * averageHeight + 30);
expect(hook.result.current.wrapperProps.style.marginTop).toBe(20 * averageHeight);
expect(hook.result.current.wrapperProps.style.height).toBe((99998 - 20) * averageHeight + 30);
});
});
});
18 changes: 13 additions & 5 deletions packages/hooks/src/useVirtualList/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface OptionType {
}

export default <T = any>(list: T[], options: OptionType) => {
const containerRef = useRef<HTMLElement>();
const containerRef = useRef<HTMLElement | null>();
const size = useSize(containerRef as MutableRefObject<HTMLElement>);
// 暂时禁止 cache
// const distanceCache = useRef<{ [key: number]: number }>({});
Expand Down Expand Up @@ -61,7 +61,10 @@ export default <T = any>(list: T[], options: OptionType) => {

const from = offset - overscan;
const to = offset + viewCapacity + overscan;
setState({ start: from < 0 ? 0 : from, end: to > list.length ? list.length : to });
setState({
start: from < 0 ? 0 : from,
end: to > list.length ? list.length : to,
});
}
};

Expand Down Expand Up @@ -102,6 +105,8 @@ export default <T = any>(list: T[], options: OptionType) => {
}
};

const offsetTop = useMemo(() => getDistanceTop(state.start), [state.start]);

return {
list: list.slice(state.start, state.end).map((ele, index) => ({
data: ele,
Expand All @@ -110,8 +115,7 @@ export default <T = any>(list: T[], options: OptionType) => {
scrollTo,
containerProps: {
ref: (ele: any) => {
// https://github.com/DefinitelyTyped/DefinitelyTyped/issues/31065
(containerRef as MutableRefObject<HTMLElement>).current = ele;
containerRef.current = ele;
},
onScroll: (e: any) => {
e.preventDefault();
Expand All @@ -120,7 +124,11 @@ export default <T = any>(list: T[], options: OptionType) => {
style: { overflowY: 'auto' as const },
},
wrapperProps: {
style: { width: '100%', height: totalHeight, paddingTop: getDistanceTop(state.start) },
style: {
width: '100%',
height: totalHeight - offsetTop,
marginTop: offsetTop,
},
},
};
};
Loading

0 comments on commit ca40243

Please sign in to comment.