-
-
Notifications
You must be signed in to change notification settings - Fork 651
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
412 additions
and
64 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
21 changes: 0 additions & 21 deletions
21
apps/docs/stories/react/Sortable/MultipleContainers/docs/MultipleLists.mdx
This file was deleted.
Oops, something went wrong.
File renamed without changes.
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
42 changes: 42 additions & 0 deletions
42
apps/docs/stories/react/Sortable/MultipleLists/docs/MultipleLists.mdx
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,42 @@ | ||
import {Code, Preview} from '../../../../components'; | ||
|
||
import {MultipleLists} from '../MultipleLists.tsx'; | ||
import {Example} from './examples/QuickStart'; | ||
import ExampleSource from './examples/QuickStart?raw'; | ||
import ColumnSource from './examples/Column?raw'; | ||
import ItemSource from './examples/Item?raw'; | ||
|
||
# Multiple lists | ||
|
||
Reorder sortable elements across multiple lists. | ||
|
||
<Preview hero> | ||
<div | ||
style={{display: 'flex', justifyContent: 'center', '--min-width': '270px'}} | ||
> | ||
<MultipleLists | ||
defaultItems={{ | ||
A: ['A1', 'A2'], | ||
B: ['B1', 'B2'], | ||
C: [], | ||
}} | ||
/> | ||
</div> | ||
</Preview> | ||
|
||
## Installation | ||
|
||
Before getting started, make sure to install the `@dnd-kit/react` package: | ||
|
||
```bash | ||
npm install @dnd-kit/react | ||
``` | ||
|
||
## Getting started | ||
|
||
<Preview | ||
tabs={['App.js', 'Item.js', 'Column.js']} | ||
code={[ExampleSource, ItemSource, ColumnSource]} | ||
> | ||
<Example /> | ||
</Preview> |
28 changes: 28 additions & 0 deletions
28
apps/docs/stories/react/Sortable/MultipleLists/docs/examples/Column.jsx
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,28 @@ | ||
import React from 'react'; | ||
import {useDroppable} from '@dnd-kit/react'; | ||
import {CollisionPriority} from '@dnd-kit/abstract'; | ||
|
||
const styles = { | ||
display: 'flex', | ||
flexDirection: 'column', | ||
gap: 10, | ||
padding: 20, | ||
minWidth: 200, | ||
backgroundColor: 'rgba(0,0,0,0.1)', | ||
borderRadius: 10, | ||
}; | ||
|
||
export function Column({children, id}) { | ||
const {ref} = useDroppable({ | ||
id, | ||
type: 'column', | ||
accept: ['item'], | ||
collisionPriority: CollisionPriority.Low, | ||
}); | ||
|
||
return ( | ||
<div style={styles} ref={ref}> | ||
{children} | ||
</div> | ||
); | ||
} |
12 changes: 12 additions & 0 deletions
12
apps/docs/stories/react/Sortable/MultipleLists/docs/examples/Item.jsx
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,12 @@ | ||
import React from 'react'; | ||
import {useSortable} from '@dnd-kit/react/sortable'; | ||
|
||
export function Item({id, index}) { | ||
const {ref} = useSortable({id, index, type: 'item', accept: ['item']}); | ||
|
||
return ( | ||
<button ref={ref}> | ||
{id} | ||
</button> | ||
); | ||
} |
38 changes: 38 additions & 0 deletions
38
apps/docs/stories/react/Sortable/MultipleLists/docs/examples/QuickStart.jsx
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,38 @@ | ||
import React, {useState} from 'react'; | ||
import {DragDropProvider} from '@dnd-kit/react'; | ||
import {move} from '@dnd-kit/state-management'; | ||
|
||
import {Column} from './Column'; | ||
import {Item} from './Item'; | ||
|
||
const styles = {display: 'inline-flex', flexDirection: 'row', gap: 20}; | ||
|
||
export function Example({style = styles}) { | ||
const [items, setItems] = useState({ | ||
A: ['A0', 'A1', 'A2'], | ||
B: ['B0', 'B1'], | ||
C: [], | ||
}); | ||
|
||
return ( | ||
<DragDropProvider | ||
onDragOver={(event) => { | ||
const {source, target} = event.operation; | ||
|
||
if (source && target) { | ||
setItems((items) => move(items, source, target)); | ||
} | ||
}} | ||
> | ||
<div style={style}> | ||
{Object.entries(items).map(([column, items]) => ( | ||
<Column key={column} id={column}> | ||
{items.map((id, index) => ( | ||
<Item key={id} id={id} index={index} /> | ||
))} | ||
</Column> | ||
))} | ||
</div> | ||
</DragDropProvider> | ||
); | ||
} |
83 changes: 83 additions & 0 deletions
83
apps/docs/stories/react/Sortable/Virtualized/ReactTinyVirtualListExample.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,83 @@ | ||
import React, {useRef, useState} from 'react'; | ||
import type {UniqueIdentifier} from '@dnd-kit/abstract'; | ||
import {DragDropProvider} from '@dnd-kit/react'; | ||
import {useSortable} from '@dnd-kit/react/sortable'; | ||
import {defaultPreset} from '@dnd-kit/dom'; | ||
import {Debug} from '@dnd-kit/dom/plugins/debug'; | ||
import {move} from '@dnd-kit/state-management'; | ||
import VirtualList from 'react-tiny-virtual-list'; | ||
|
||
import {Item, Handle} from '../../components'; | ||
import {createRange, cloneDeep} from '../../../utilities'; | ||
|
||
interface Props { | ||
debug?: boolean; | ||
} | ||
|
||
export function ReactTinyVirtualListExample({debug}: Props) { | ||
const [items, setItems] = useState<UniqueIdentifier[]>(createRange(1000)); | ||
const snapshot = useRef(cloneDeep(items)); | ||
|
||
return ( | ||
<DragDropProvider | ||
plugins={debug ? [Debug, ...defaultPreset.plugins] : undefined} | ||
onDragStart={() => { | ||
snapshot.current = cloneDeep(items); | ||
}} | ||
onDragOver={(event) => { | ||
const {source, target} = event.operation; | ||
|
||
if (!source || !target) { | ||
return; | ||
} | ||
|
||
setItems((items) => move(items, source, target)); | ||
}} | ||
onDragEnd={(event) => { | ||
if (event.canceled) { | ||
setItems(snapshot.current); | ||
} | ||
}} | ||
> | ||
<VirtualList | ||
width={window.innerWidth - 100} | ||
height={window.innerHeight - 100} | ||
itemCount={items.length} | ||
itemSize={82} | ||
renderItem={({index, style}) => { | ||
const id = items[index]; | ||
|
||
return <Sortable key={id} id={id} index={index} style={style} />; | ||
}} | ||
/> | ||
</DragDropProvider> | ||
); | ||
} | ||
|
||
interface SortableProps { | ||
id: UniqueIdentifier; | ||
index: number; | ||
style: React.CSSProperties; | ||
} | ||
|
||
function Sortable({id, index, style}: SortableProps) { | ||
const {isDragSource, ref, handleRef} = useSortable({ | ||
id, | ||
index, | ||
}); | ||
|
||
return ( | ||
<div | ||
ref={ref} | ||
style={{...style, display: 'flex', justifyContent: 'center', padding: 10}} | ||
> | ||
<Item | ||
actions={<Handle ref={handleRef} />} | ||
data-index={index} | ||
shadow={isDragSource} | ||
> | ||
{id} | ||
</Item> | ||
</div> | ||
); | ||
} |
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
101 changes: 101 additions & 0 deletions
101
apps/docs/stories/react/Sortable/Virtualized/ReactWindowExample.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,101 @@ | ||
import React, {useRef, useEffect, useState} from 'react'; | ||
import type {UniqueIdentifier} from '@dnd-kit/abstract'; | ||
import {DragDropProvider} from '@dnd-kit/react'; | ||
import {useSortable} from '@dnd-kit/react/sortable'; | ||
import {defaultPreset} from '@dnd-kit/dom'; | ||
import {Debug} from '@dnd-kit/dom/plugins/debug'; | ||
import {move} from '@dnd-kit/state-management'; | ||
import {FixedSizeList as List} from 'react-window'; | ||
|
||
import {Item, Handle} from '../../components'; | ||
import {createRange, cloneDeep} from '../../../utilities'; | ||
|
||
interface Props { | ||
debug?: boolean; | ||
} | ||
|
||
export function ReactWindowExample({debug}: Props) { | ||
const [items, setItems] = useState<UniqueIdentifier[]>(createRange(1000)); | ||
const snapshot = useRef(cloneDeep(items)); | ||
|
||
return ( | ||
<DragDropProvider | ||
plugins={debug ? [Debug, ...defaultPreset.plugins] : undefined} | ||
onDragStart={() => { | ||
snapshot.current = cloneDeep(items); | ||
}} | ||
onDragOver={(event) => { | ||
const {source, target} = event.operation; | ||
|
||
if (!source || !target) { | ||
return; | ||
} | ||
|
||
setItems((items) => move(items, source, target)); | ||
}} | ||
onDragEnd={(event) => { | ||
if (event.canceled) { | ||
setItems(snapshot.current); | ||
} | ||
}} | ||
> | ||
<List | ||
width={window.innerWidth - 100} | ||
height={window.innerHeight - 100} | ||
itemCount={items.length} | ||
itemSize={82} | ||
itemData={items} | ||
itemKey={(index) => items[index]} | ||
style={{margin: '0 auto'}} | ||
> | ||
{Row} | ||
</List> | ||
</DragDropProvider> | ||
); | ||
} | ||
|
||
function Row({ | ||
data, | ||
index, | ||
style, | ||
}: { | ||
data: UniqueIdentifier[]; | ||
index: number; | ||
style: React.CSSProperties; | ||
}) { | ||
return <Sortable id={data[index]} index={index} style={style} />; | ||
} | ||
|
||
interface SortableProps { | ||
id: UniqueIdentifier; | ||
index: number; | ||
style: React.CSSProperties; | ||
} | ||
|
||
function Sortable({id, index, style}: SortableProps) { | ||
const {isDragSource, ref, handleRef} = useSortable({ | ||
id, | ||
index, | ||
}); | ||
|
||
useEffect(() => { | ||
return () => { | ||
console.log('unmount', id); | ||
}; | ||
}, [id]); | ||
|
||
return ( | ||
<div | ||
ref={ref} | ||
style={{...style, display: 'flex', justifyContent: 'center', padding: 10}} | ||
> | ||
<Item | ||
actions={<Handle ref={handleRef} />} | ||
data-index={index} | ||
shadow={isDragSource} | ||
> | ||
{id} | ||
</Item> | ||
</div> | ||
); | ||
} |
17 changes: 13 additions & 4 deletions
17
apps/docs/stories/react/Sortable/Virtualized/Virtualized.stories.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 |
---|---|---|
@@ -1,18 +1,27 @@ | ||
import type {Meta, StoryObj} from '@storybook/react'; | ||
|
||
import {ReactWindowExample} from './ReactWindowExample'; | ||
import {ReactVirtualExample} from './ReactVirtualExample'; | ||
import {ReactTinyVirtualListExample} from './ReactTinyVirtualListExample'; | ||
|
||
const meta: Meta<typeof ReactVirtualExample> = { | ||
title: 'React/Sortable/Virtualized', | ||
component: ReactVirtualExample, | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof ReactVirtualExample>; | ||
|
||
export const ReactWindow: Story = { | ||
name: 'react-window', | ||
render: ReactWindowExample, | ||
}; | ||
|
||
export const ReactTinyVirtualList: Story = { | ||
name: 'react-tiny-virtual-list', | ||
render: ReactTinyVirtualListExample, | ||
}; | ||
|
||
export const ReactVirtual: Story = { | ||
name: 'react-virtual', | ||
args: { | ||
debug: false, | ||
}, | ||
render: ReactVirtualExample, | ||
}; |
Oops, something went wrong.