-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from js-tool-pack/draggable-group
DraggableGroup
- Loading branch information
Showing
13 changed files
with
404 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
packages/components/src/draggable/demo/draggable-group.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
50
packages/components/src/draggable/demo/draggable-group.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** | ||
* title: draggable | ||
* title: 禁止拖拽选项 | ||
* description: html 元素设置 draggable 为 false 时不可拖动。 | ||
*/ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** | ||
* title: tag | ||
* title: 根标签 | ||
* description: 默认根元素为 div,当 tag 为 null 时 Draggable 组件不提供根元素。 | ||
*/ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** | ||
* title: transition | ||
* title: 动画 | ||
* description: 开启 transition 动画。 | ||
*/ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.