Skip to content

Commit

Permalink
Merge pull request #111 from js-tool-pack/draggable-group
Browse files Browse the repository at this point in the history
DraggableGroup
  • Loading branch information
mengxinssfd authored Jun 9, 2024
2 parents 3535e56 + 5ee816f commit 917634a
Show file tree
Hide file tree
Showing 13 changed files with 404 additions and 83 deletions.
2 changes: 1 addition & 1 deletion packages/components/src/draggable/Draggable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useDraggableChildren } from './hooks';
import type { ReactElement, FC } from 'react';
import { getClasses } from '@pkg/shared';

export const cls = getClasses('draggable', ['ghost', 'item'], []);
export const cls = getClasses('draggable', ['ghost', 'item'], ['hidden']);
const defaultProps = {
tag: 'div',
list: [],
Expand Down
44 changes: 44 additions & 0 deletions packages/components/src/draggable/DraggableGroup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { createContext, ReactElement, useRef, FC } from 'react';
import type { DraggableGroupProps } from './draggable.types';

const defaultProps = {} satisfies Partial<DraggableGroupProps>;

/**
* 在 dragstart 里生产,在 dragenter 消费,在 drop | dragend 销毁
*/
export interface DraggableGroupContextProvider {
readonly children: ReactElement;
cancel?: (id: symbol) => void;
enter?: (id: symbol) => void;
drop?: (id: symbol) => void;
readonly item: unknown;
id: symbol;
}
export const draggableContext = createContext<{
fromRef: { current: DraggableGroupContextProvider | null };
toRef: { current: DraggableGroupContextProvider | null };
type: Exclude<DraggableGroupProps['type'], undefined>;
}>({
fromRef: { current: null },
toRef: { current: null },
type: 'move',
});

export const DraggableGroup: FC<DraggableGroupProps> = (props) => {
const fromRef = useRef(null);
const toRef = useRef(null);
return (
<draggableContext.Provider
value={{
type: props.type || 'move',
fromRef,
toRef,
}}
>
{props.children}
</draggableContext.Provider>
);
};

DraggableGroup.defaultProps = defaultProps;
DraggableGroup.displayName = 'DraggableGroup';
23 changes: 23 additions & 0 deletions packages/components/src/draggable/demo/draggable-group.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.root {
:global {
.main {
display: flex;
margin-top: 1rem;
}
.t-draggable {
flex: 1;
+ .t-draggable {
margin-left: 6px;
}
}
.draggable-item {
padding: 0 0.5rem;
border: 1px solid #e6e6e6;
background: #fff1d7;
line-height: 32px;
&.t-draggable__ghost {
background: #f6a306;
}
}
}
}
50 changes: 50 additions & 0 deletions packages/components/src/draggable/demo/draggable-group.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* title: 拖拽组
* description: 拖拽组分为两种拖拽方式,'move' 为移动数据,'copy' 为复制数据,默认为 'move'。
*/

import { DraggableGroup, Draggable, Select } from '@tool-pack/react-ui';
import styles from './draggable-group.module.scss';
import React, { useState } from 'react';

const options = [
{ label: 'move', value: 'move' },
{ label: 'copy', value: 'copy' },
];

const App: React.FC = () => {
const [type, setType] = useState<'copy' | 'move'>('move');
const [state, setState] = React.useState<string[]>(['A', 'B', 'C', 'D']);
const [state2, setState2] = React.useState<string[]>(['a', 'b', 'c', 'd']);
return (
<div className={styles['root']}>
<Select
attrs={{ style: { display: 'inline-flex' } }}
onChange={setType}
options={options}
value={type}
size="small"
/>
<div className="main">
<DraggableGroup type={type}>
<Draggable onChange={setState} list={state} key="left">
{state.map((item) => (
<div className="draggable-item" key={item}>
{item}
</div>
))}
</Draggable>
<Draggable onChange={setState2} list={state2} key="right">
{state2.map((item) => (
<div className="draggable-item" key={item}>
{item}
</div>
))}
</Draggable>
</DraggableGroup>
</div>
</div>
);
};

export default App;
2 changes: 1 addition & 1 deletion packages/components/src/draggable/demo/draggable.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* title: draggable
* title: 禁止拖拽选项
* description: html 元素设置 draggable 为 false 时不可拖动。
*/

Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/draggable/demo/tag.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* title: tag
* title: 根标签
* description: 默认根元素为 div,当 tag 为 null 时 Draggable 组件不提供根元素。
*/

Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/draggable/demo/transition.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* title: transition
* title: 动画
* description: 开启 transition 动画。
*/

Expand Down
5 changes: 5 additions & 0 deletions packages/components/src/draggable/draggable.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ export interface DraggableProps<T = unknown> extends PropsBase<HTMLDivElement> {
}
export type DraggableFC = <T>(props: DraggableProps<T>) => ReactElement;
// export interface DraggableLocale {}

export interface DraggableGroupProps {
children: ReactElement[];
type?: 'move' | 'copy';
}
Loading

0 comments on commit 917634a

Please sign in to comment.