Skip to content

Commit 23a120a

Browse files
committed
feat(shared/hooks): useChildrenWithRefs 可控制cloneElement 参数
1 parent 59ea017 commit 23a120a

File tree

3 files changed

+91
-72
lines changed

3 files changed

+91
-72
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,89 @@
11
import { fromEvent, switchMap, takeUntil, filter, merge, map, tap } from 'rxjs';
22
import { inRange } from '@tool-pack/basic';
33
import type { FN } from '@tool-pack/types';
4-
import type { RefObject } from 'react';
54

65
export function drag(
7-
ref: RefObject<HTMLDivElement>,
6+
refs: (HTMLElement | undefined)[],
87
ghostClassName: string,
98
onMove: (prevIndex: number, currIndex: number) => void,
109
onUp: (prevIndex: number, currIndex: number) => void,
1110
): undefined | FN {
12-
const el = ref.current;
13-
if (!el) return;
14-
15-
const mouseDown$ = fromEvent<MouseEvent>(el, 'mousedown');
1611
const mouseUp$ = fromEvent<MouseEvent>(window, 'mouseup');
1712
const dragOver$ = fromEvent<DragEvent>(window, 'dragover');
1813
const dragEnd$ = fromEvent<DragEvent>(window, 'dragend');
1914
const dragStart$ = fromEvent<DragEvent>(window, 'dragstart');
2015

21-
const queue$ = mouseDown$.pipe(
22-
map(findMouseDownChildIndex),
23-
filter((index): boolean => index >= 0),
24-
map(
25-
(
26-
index,
27-
): {
28-
readonly chosen: HTMLElement;
29-
readonly originIndex: number;
30-
index: number;
31-
} => {
32-
const chosen = getChildrenArray()[index] as HTMLElement;
33-
chosen.draggable = true;
34-
return { originIndex: index, chosen, index };
35-
},
36-
),
37-
switchMap((params) =>
38-
dragStart$.pipe(
39-
tap((e): void => {
40-
if (e.dataTransfer) e.dataTransfer.dropEffect = 'move';
41-
}),
42-
map(() => params),
16+
const listen = (el: HTMLElement) => {
17+
const mouseDown$ = fromEvent<MouseEvent>(el, 'mousedown');
18+
const queue$ = mouseDown$.pipe(
19+
map(findMouseDownChildIndex),
20+
filter((index): boolean => index >= 0),
21+
map(
22+
(
23+
index,
24+
): {
25+
readonly chosen: HTMLElement;
26+
readonly originIndex: number;
27+
index: number;
28+
} => {
29+
const chosen = refs[index] as HTMLElement;
30+
chosen.draggable = true;
31+
return { originIndex: index, chosen, index };
32+
},
33+
),
34+
switchMap((params) =>
35+
dragStart$.pipe(
36+
tap((e): void => {
37+
if (e.dataTransfer) e.dataTransfer.dropEffect = 'move';
38+
}),
39+
map(() => params),
40+
),
4341
),
44-
),
45-
switchMap((params) => {
46-
return dragOver$.pipe(
47-
tap((e: DragEvent): void => {
48-
params.chosen.classList.add(ghostClassName);
49-
if (e.dataTransfer) e.dataTransfer.dropEffect = 'move';
50-
const currIndex = findDragItemIndex(e);
51-
if (currIndex > -1 && currIndex !== params.index) {
52-
onMove(params.index, currIndex);
53-
params.index = currIndex;
54-
}
55-
if (mouseInElement(e, el)) e.preventDefault();
56-
}),
57-
takeUntil(
58-
merge(
59-
mouseUp$,
60-
dragEnd$.pipe(
61-
tap((e): void => {
62-
const currIndex = findDragItemIndex(e);
63-
if (currIndex > -1 && currIndex !== params.originIndex)
64-
onUp(params.index, currIndex);
42+
switchMap((params) => {
43+
return dragOver$.pipe(
44+
tap((e: DragEvent): void => {
45+
params.chosen.classList.add(ghostClassName);
46+
if (e.dataTransfer) e.dataTransfer.dropEffect = 'move';
47+
const currIndex = findDragItemIndex(e);
48+
if (currIndex > -1 && currIndex !== params.index) {
49+
onMove(params.index, currIndex);
50+
params.index = currIndex;
51+
}
52+
if (mouseInElement(e, el)) e.preventDefault();
53+
}),
54+
takeUntil(
55+
merge(
56+
mouseUp$,
57+
dragEnd$.pipe(
58+
tap((e): void => {
59+
const currIndex = findDragItemIndex(e);
60+
if (currIndex > -1 && currIndex !== params.originIndex)
61+
onUp(params.index, currIndex);
62+
}),
63+
),
64+
).pipe(
65+
tap((): void => {
66+
restoreChosenEl(params.chosen);
6567
}),
6668
),
67-
).pipe(
68-
tap((): void => {
69-
restoreChosenEl(params.chosen);
70-
}),
7169
),
72-
),
73-
);
74-
}),
75-
);
70+
);
71+
}),
72+
);
73+
const sub = queue$.subscribe();
74+
75+
return () => {
76+
sub.unsubscribe();
77+
};
78+
};
7679

77-
const sub = queue$.subscribe();
80+
const subs = refs.map<void | FN>((el): void | FN => {
81+
if (el) return listen(el);
82+
});
7883

7984
return () => {
80-
sub.unsubscribe();
85+
subs.forEach((s) => s?.());
86+
subs.length = 0;
8187
};
8288

8389
function restoreChosenEl(el: HTMLElement): void {
@@ -87,7 +93,7 @@ export function drag(
8793
function findMouseDownChildIndex(e: MouseEvent): number {
8894
const target = e.target as HTMLElement | null;
8995
if (!target) return -1;
90-
return getChildrenArray().findIndex((child) => child.contains(target));
96+
return refs.findIndex((child) => child && child.contains(target));
9197
}
9298
function mouseInElement(ev: MouseEvent, el: HTMLElement): boolean {
9399
const { clientX, clientY } = ev;
@@ -98,10 +104,6 @@ export function drag(
98104
);
99105
}
100106
function findDragItemIndex(e: DragEvent): number {
101-
return getChildrenArray().findIndex((child) => mouseInElement(e, child));
102-
}
103-
function getChildrenArray(): HTMLElement[] {
104-
if (!el) return [];
105-
return Array.from(el.children) as HTMLElement[];
107+
return refs.findIndex((child) => child && mouseInElement(e, child));
106108
}
107109
}

packages/shared/src/hooks/__tests__/useChildrenWithRefs.test.tsx

+15-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ describe('useChildrenWithRefs', () => {
3636
expect(ref.mock.calls[0][0]).toBe(container.firstChild);
3737
});
3838
test('app', () => {
39-
const obj: { ref: HTMLElement | null; refs: HTMLElement[] } = {
40-
ref: null,
41-
refs: [],
42-
};
39+
const obj: { refs: (HTMLElement | undefined)[]; ref: HTMLElement | null } =
40+
{
41+
ref: null,
42+
refs: [],
43+
};
4344
const App = () => {
4445
const ref = useRef<HTMLDivElement>(null);
4546
const [children, setChildren] = useState<ReactNode>(
@@ -90,4 +91,14 @@ describe('useChildrenWithRefs', () => {
9091
expect(obj.refs[0]).toBe(firstChild);
9192
expect(obj.ref).toBe(firstChild);
9293
});
94+
test('cloneEl', () => {
95+
const App = () => {
96+
const [children] = useChildrenWithRefs(<div>foo</div>, (element, props) =>
97+
cloneElement(element, { ...props, className: 'foo' }),
98+
);
99+
return children;
100+
};
101+
const { container } = render(<App />);
102+
expect(container.firstChild).toHaveClass('foo');
103+
});
93104
});

packages/shared/src/hooks/useChildrenWithRefs.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
JSXElementConstructor,
23
isValidElement,
34
cloneElement,
45
ReactElement,
@@ -12,12 +13,17 @@ import {
1213
*/
1314
export function useChildrenWithRefs(
1415
children?: ReactNode,
15-
): [children: ReactNode, refs: HTMLElement[]] {
16+
cloneEl?: (
17+
element: ReactElement<any, JSXElementConstructor<any> | string>,
18+
props: { ref: (el: HTMLElement) => void },
19+
) => ReactElement,
20+
): [children: ReactNode, refs: (HTMLElement | undefined)[]] {
1621
return useMemo(() => {
17-
const refs: HTMLElement[] = [];
22+
const refs: (HTMLElement | undefined)[] = [];
23+
const clone = cloneEl || cloneElement;
1824
const newChildren = Children.map(children, (child, index) => {
1925
if (!isValidElement(child)) return child;
20-
return cloneElement(child as ReactElement, {
26+
return clone(child, {
2127
ref: (el: HTMLElement) => {
2228
refs[index] = el;
2329
// ref 现在是直接放在 ReactElement 上的,props 虽然也有,但取的是 undefined

0 commit comments

Comments
 (0)