-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Animate component's mounting and unmounting (#24049)
- Loading branch information
Showing
4 changed files
with
172 additions
and
1 deletion.
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
79 changes: 79 additions & 0 deletions
79
docs/src/pages/components/transitions/TransitionGroupExample.js
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,79 @@ | ||
import * as React from 'react'; | ||
import Box from '@material-ui/core/Box'; | ||
import Button from '@material-ui/core/Button'; | ||
import Collapse from '@material-ui/core/Collapse'; | ||
import IconButton from '@material-ui/core/IconButton'; | ||
import List from '@material-ui/core/List'; | ||
import ListItem from '@material-ui/core/ListItem'; | ||
import ListItemSecondaryAction from '@material-ui/core/ListItemSecondaryAction'; | ||
import ListItemText from '@material-ui/core/ListItemText'; | ||
import DeleteIcon from '@material-ui/icons/Delete'; | ||
import { TransitionGroup } from 'react-transition-group'; | ||
|
||
const FRUITS = [ | ||
'🍏 Apple', | ||
'🍌 Banana', | ||
'🍍 Pineapple', | ||
'🥥 Coconut', | ||
'🍉 Watermelon', | ||
]; | ||
|
||
function renderItem({ item, handleRemoveFruit }) { | ||
return ( | ||
<ListItem> | ||
<ListItemText primary={item} /> | ||
<ListItemSecondaryAction> | ||
<IconButton | ||
edge="end" | ||
aria-label="delete" | ||
title="Delete" | ||
onClick={() => handleRemoveFruit(item)} | ||
> | ||
<DeleteIcon /> | ||
</IconButton> | ||
</ListItemSecondaryAction> | ||
</ListItem> | ||
); | ||
} | ||
|
||
export default function TransitionGroupExample() { | ||
const [fruitsInBasket, setFruitsInBasket] = React.useState(FRUITS.slice(0, 3)); | ||
|
||
const handleAddFruit = () => { | ||
const nextHiddenItem = FRUITS.find((i) => !fruitsInBasket.includes(i)); | ||
if (nextHiddenItem) { | ||
setFruitsInBasket((prev) => [nextHiddenItem, ...prev]); | ||
} | ||
}; | ||
|
||
const handleRemoveFruit = (item) => { | ||
setFruitsInBasket((prev) => [...prev.filter((i) => i !== item)]); | ||
}; | ||
|
||
const addFruitButton = ( | ||
<Button | ||
variant="contained" | ||
disabled={fruitsInBasket.length >= FRUITS.length} | ||
onClick={handleAddFruit} | ||
> | ||
Add fruit to basket | ||
</Button> | ||
); | ||
|
||
return ( | ||
<div> | ||
{addFruitButton} | ||
<Box sx={{ mt: 1 }}> | ||
<List> | ||
<TransitionGroup> | ||
{fruitsInBasket.map((item) => ( | ||
<Collapse key={item}> | ||
{renderItem({ item, handleRemoveFruit })} | ||
</Collapse> | ||
))} | ||
</TransitionGroup> | ||
</List> | ||
</Box> | ||
</div> | ||
); | ||
} |
84 changes: 84 additions & 0 deletions
84
docs/src/pages/components/transitions/TransitionGroupExample.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,84 @@ | ||
import * as React from 'react'; | ||
import Box from '@material-ui/core/Box'; | ||
import Button from '@material-ui/core/Button'; | ||
import Collapse from '@material-ui/core/Collapse'; | ||
import IconButton from '@material-ui/core/IconButton'; | ||
import List from '@material-ui/core/List'; | ||
import ListItem from '@material-ui/core/ListItem'; | ||
import ListItemSecondaryAction from '@material-ui/core/ListItemSecondaryAction'; | ||
import ListItemText from '@material-ui/core/ListItemText'; | ||
import DeleteIcon from '@material-ui/icons/Delete'; | ||
import { TransitionGroup } from 'react-transition-group'; | ||
|
||
const FRUITS = [ | ||
'🍏 Apple', | ||
'🍌 Banana', | ||
'🍍 Pineapple', | ||
'🥥 Coconut', | ||
'🍉 Watermelon', | ||
]; | ||
|
||
interface RenderItemOptions { | ||
item: string; | ||
handleRemoveFruit: (item: string) => void; | ||
} | ||
|
||
function renderItem({ item, handleRemoveFruit }: RenderItemOptions) { | ||
return ( | ||
<ListItem> | ||
<ListItemText primary={item} /> | ||
<ListItemSecondaryAction> | ||
<IconButton | ||
edge="end" | ||
aria-label="delete" | ||
title="Delete" | ||
onClick={() => handleRemoveFruit(item)} | ||
> | ||
<DeleteIcon /> | ||
</IconButton> | ||
</ListItemSecondaryAction> | ||
</ListItem> | ||
); | ||
} | ||
|
||
export default function TransitionGroupExample() { | ||
const [fruitsInBasket, setFruitsInBasket] = React.useState(FRUITS.slice(0, 3)); | ||
|
||
const handleAddFruit = () => { | ||
const nextHiddenItem = FRUITS.find((i) => !fruitsInBasket.includes(i)); | ||
if (nextHiddenItem) { | ||
setFruitsInBasket((prev) => [nextHiddenItem, ...prev]); | ||
} | ||
}; | ||
|
||
const handleRemoveFruit = (item: string) => { | ||
setFruitsInBasket((prev) => [...prev.filter((i) => i !== item)]); | ||
}; | ||
|
||
const addFruitButton = ( | ||
<Button | ||
variant="contained" | ||
disabled={fruitsInBasket.length >= FRUITS.length} | ||
onClick={handleAddFruit} | ||
> | ||
Add fruit to basket | ||
</Button> | ||
); | ||
|
||
return ( | ||
<div> | ||
{addFruitButton} | ||
<Box sx={{ mt: 1 }}> | ||
<List> | ||
<TransitionGroup> | ||
{fruitsInBasket.map((item) => ( | ||
<Collapse key={item}> | ||
{renderItem({ item, handleRemoveFruit })} | ||
</Collapse> | ||
))} | ||
</TransitionGroup> | ||
</List> | ||
</Box> | ||
</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