-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Add Chip TypeScript demo for Chip array (#15050)
* [docs] Add Chips Array TS Demo * do not initialize icon as null
- Loading branch information
Showing
2 changed files
with
69 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
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,68 @@ | ||
import React from 'react'; | ||
import { makeStyles, Theme } from '@material-ui/core/styles'; | ||
import Chip from '@material-ui/core/Chip'; | ||
import Paper from '@material-ui/core/Paper'; | ||
import TagFacesIcon from '@material-ui/icons/TagFaces'; | ||
|
||
interface ChipData { | ||
key: number; | ||
label: string; | ||
} | ||
|
||
const useStyles = makeStyles((theme: Theme) => ({ | ||
root: { | ||
display: 'flex', | ||
justifyContent: 'center', | ||
flexWrap: 'wrap', | ||
padding: theme.spacing(0.5), | ||
}, | ||
chip: { | ||
margin: theme.spacing(0.5), | ||
}, | ||
})); | ||
|
||
function ChipsArray() { | ||
const classes = useStyles(); | ||
const [chipData, setChipData] = React.useState<ChipData[]>([ | ||
{ key: 0, label: 'Angular' }, | ||
{ key: 1, label: 'jQuery' }, | ||
{ key: 2, label: 'Polymer' }, | ||
{ key: 3, label: 'React' }, | ||
{ key: 4, label: 'Vue.js' }, | ||
]); | ||
|
||
const handleDelete = (data: ChipData) => () => { | ||
if (data.label === 'React') { | ||
alert('Why would you want to delete React?! :)'); // eslint-disable-line no-alert | ||
return; | ||
} | ||
|
||
const chipToDelete = chipData.indexOf(data); | ||
chipData.splice(chipToDelete, 1); | ||
setChipData(chipData); | ||
}; | ||
|
||
return ( | ||
<Paper className={classes.root}> | ||
{chipData.map(data => { | ||
let icon; | ||
|
||
if (data.label === 'React') { | ||
icon = <TagFacesIcon />; | ||
} | ||
|
||
return ( | ||
<Chip | ||
key={data.key} | ||
icon={icon} | ||
label={data.label} | ||
onDelete={handleDelete(data)} | ||
className={classes.chip} | ||
/> | ||
); | ||
})} | ||
</Paper> | ||
); | ||
} | ||
|
||
export default ChipsArray; |