Skip to content

Commit 917634a

Browse files
authored
Merge pull request #111 from js-tool-pack/draggable-group
DraggableGroup
2 parents 3535e56 + 5ee816f commit 917634a

13 files changed

+404
-83
lines changed

packages/components/src/draggable/Draggable.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { useDraggableChildren } from './hooks';
99
import type { ReactElement, FC } from 'react';
1010
import { getClasses } from '@pkg/shared';
1111

12-
export const cls = getClasses('draggable', ['ghost', 'item'], []);
12+
export const cls = getClasses('draggable', ['ghost', 'item'], ['hidden']);
1313
const defaultProps = {
1414
tag: 'div',
1515
list: [],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { createContext, ReactElement, useRef, FC } from 'react';
2+
import type { DraggableGroupProps } from './draggable.types';
3+
4+
const defaultProps = {} satisfies Partial<DraggableGroupProps>;
5+
6+
/**
7+
* 在 dragstart 里生产,在 dragenter 消费,在 drop | dragend 销毁
8+
*/
9+
export interface DraggableGroupContextProvider {
10+
readonly children: ReactElement;
11+
cancel?: (id: symbol) => void;
12+
enter?: (id: symbol) => void;
13+
drop?: (id: symbol) => void;
14+
readonly item: unknown;
15+
id: symbol;
16+
}
17+
export const draggableContext = createContext<{
18+
fromRef: { current: DraggableGroupContextProvider | null };
19+
toRef: { current: DraggableGroupContextProvider | null };
20+
type: Exclude<DraggableGroupProps['type'], undefined>;
21+
}>({
22+
fromRef: { current: null },
23+
toRef: { current: null },
24+
type: 'move',
25+
});
26+
27+
export const DraggableGroup: FC<DraggableGroupProps> = (props) => {
28+
const fromRef = useRef(null);
29+
const toRef = useRef(null);
30+
return (
31+
<draggableContext.Provider
32+
value={{
33+
type: props.type || 'move',
34+
fromRef,
35+
toRef,
36+
}}
37+
>
38+
{props.children}
39+
</draggableContext.Provider>
40+
);
41+
};
42+
43+
DraggableGroup.defaultProps = defaultProps;
44+
DraggableGroup.displayName = 'DraggableGroup';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.root {
2+
:global {
3+
.main {
4+
display: flex;
5+
margin-top: 1rem;
6+
}
7+
.t-draggable {
8+
flex: 1;
9+
+ .t-draggable {
10+
margin-left: 6px;
11+
}
12+
}
13+
.draggable-item {
14+
padding: 0 0.5rem;
15+
border: 1px solid #e6e6e6;
16+
background: #fff1d7;
17+
line-height: 32px;
18+
&.t-draggable__ghost {
19+
background: #f6a306;
20+
}
21+
}
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* title: 拖拽组
3+
* description: 拖拽组分为两种拖拽方式,'move' 为移动数据,'copy' 为复制数据,默认为 'move'。
4+
*/
5+
6+
import { DraggableGroup, Draggable, Select } from '@tool-pack/react-ui';
7+
import styles from './draggable-group.module.scss';
8+
import React, { useState } from 'react';
9+
10+
const options = [
11+
{ label: 'move', value: 'move' },
12+
{ label: 'copy', value: 'copy' },
13+
];
14+
15+
const App: React.FC = () => {
16+
const [type, setType] = useState<'copy' | 'move'>('move');
17+
const [state, setState] = React.useState<string[]>(['A', 'B', 'C', 'D']);
18+
const [state2, setState2] = React.useState<string[]>(['a', 'b', 'c', 'd']);
19+
return (
20+
<div className={styles['root']}>
21+
<Select
22+
attrs={{ style: { display: 'inline-flex' } }}
23+
onChange={setType}
24+
options={options}
25+
value={type}
26+
size="small"
27+
/>
28+
<div className="main">
29+
<DraggableGroup type={type}>
30+
<Draggable onChange={setState} list={state} key="left">
31+
{state.map((item) => (
32+
<div className="draggable-item" key={item}>
33+
{item}
34+
</div>
35+
))}
36+
</Draggable>
37+
<Draggable onChange={setState2} list={state2} key="right">
38+
{state2.map((item) => (
39+
<div className="draggable-item" key={item}>
40+
{item}
41+
</div>
42+
))}
43+
</Draggable>
44+
</DraggableGroup>
45+
</div>
46+
</div>
47+
);
48+
};
49+
50+
export default App;

packages/components/src/draggable/demo/draggable.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* title: draggable
2+
* title: 禁止拖拽选项
33
* description: html 元素设置 draggable 为 false 时不可拖动。
44
*/
55

packages/components/src/draggable/demo/tag.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* title: tag
2+
* title: 根标签
33
* description: 默认根元素为 div,当 tag 为 null 时 Draggable 组件不提供根元素。
44
*/
55

packages/components/src/draggable/demo/transition.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* title: transition
2+
* title: 动画
33
* description: 开启 transition 动画。
44
*/
55

packages/components/src/draggable/draggable.types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ export interface DraggableProps<T = unknown> extends PropsBase<HTMLDivElement> {
1010
}
1111
export type DraggableFC = <T>(props: DraggableProps<T>) => ReactElement;
1212
// export interface DraggableLocale {}
13+
14+
export interface DraggableGroupProps {
15+
children: ReactElement[];
16+
type?: 'move' | 'copy';
17+
}

0 commit comments

Comments
 (0)