forked from atlassian/react-beautiful-dnd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding example for function components (atlassian#1167)
- Loading branch information
1 parent
bef5014
commit 779fe78
Showing
8 changed files
with
132 additions
and
14 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
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
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,9 @@ | ||
// @flow | ||
import React from 'react'; | ||
import { storiesOf } from '@storybook/react'; | ||
import QuoteApp from './src/function-component/quote-app'; | ||
|
||
storiesOf('Function component usage', module).add( | ||
'using rbd with function components and hooks', | ||
() => <QuoteApp />, | ||
); |
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,90 @@ | ||
// @flow | ||
import React, { useState } from 'react'; | ||
import styled from '@emotion/styled'; | ||
import { DragDropContext, Droppable, Draggable } from '../../../src'; | ||
import type { | ||
DropResult, | ||
DraggableProvided, | ||
DroppableProvided, | ||
} from '../../../src'; | ||
import type { Quote as QuoteType } from '../types'; | ||
import { quotes as initial } from '../data'; | ||
import reorder from '../reorder'; | ||
import { grid } from '../constants'; | ||
|
||
type QuoteProps = {| | ||
quote: QuoteType, | ||
index: number, | ||
|}; | ||
|
||
const QuoteItem = styled.div` | ||
width: 200px; | ||
border: 1px solid grey; | ||
margin-bottom: ${grid}px; | ||
background-color: lightblue; | ||
padding: ${grid}px; | ||
`; | ||
|
||
function Quote({ quote, index }: QuoteProps) { | ||
return ( | ||
<Draggable draggableId={quote.id} index={index}> | ||
{(provided: DraggableProvided) => ( | ||
<QuoteItem | ||
ref={provided.innerRef} | ||
{...provided.draggableProps} | ||
{...provided.dragHandleProps} | ||
> | ||
{quote.content} | ||
</QuoteItem> | ||
)} | ||
</Draggable> | ||
); | ||
} | ||
|
||
type QuoteListProps = {| | ||
quotes: QuoteType[], | ||
|}; | ||
|
||
// Ensuring the whole list does not re-render when the droppable re-renders | ||
const QuoteList = React.memo(function QuoteList({ quotes }: QuoteListProps) { | ||
return quotes.map((quote: QuoteType, index: number) => ( | ||
<Quote quote={quote} index={index} key={quote.id} /> | ||
)); | ||
}); | ||
|
||
function QuoteApp() { | ||
const [quotes, setQuotes] = useState(initial); | ||
|
||
function onDragEnd(result: DropResult) { | ||
if (!result.destination) { | ||
return; | ||
} | ||
|
||
if (result.destination.index === result.source.index) { | ||
return; | ||
} | ||
|
||
const newQuotes = reorder( | ||
quotes, | ||
result.source.index, | ||
result.destination.index, | ||
); | ||
|
||
setQuotes(newQuotes); | ||
} | ||
|
||
return ( | ||
<DragDropContext onDragEnd={onDragEnd}> | ||
<Droppable droppableId="list"> | ||
{(provided: DroppableProvided) => ( | ||
<div ref={provided.innerRef} {...provided.droppableProps}> | ||
<QuoteList quotes={quotes} /> | ||
{provided.placeholder} | ||
</div> | ||
)} | ||
</Droppable> | ||
</DragDropContext> | ||
); | ||
} | ||
|
||
export default QuoteApp; |
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