Categories
Material UI

Material UI — Dialogs

Material UI is a Material Design library made for React.

It’s a set of React components that have Material Design styles.

In this article, we’ll look at how to add dialog boxes with Material UI.

Dialog

A dialog box is used to let users know about some information.

To add one, we can use the Dialog component.

For example, we can write:

import React from "react";
import Button from "@material-ui/core/Button";
import Dialog from "@material-ui/core/Dialog";
import DialogTitle from "@material-ui/core/DialogTitle";
import List from "@material-ui/core/List";
import ListItem from "@material-ui/core/ListItem";

export default function App() {
  const [open, setOpen] = React.useState(false);

  const handleClose = () => {
    setOpen(false);
  };

  return (
    <div>
      <Button variant="outlined" color="primary" onClick={() => setOpen(true)}>
        Open simple dialog
      </Button>
      <Dialog onClose={handleClose} open={open}>
        <DialogTitle id="simple-dialog-title">dialog</DialogTitle>
        <List>
          <ListItem autoFocus button onClick={handleClose}>
            close
          </ListItem>
        </List>
      </Dialog>
    </div>
  );
}

to add a dialog with some items in it.

We added a List with a ListItem which is a button.

We can click it to close the dialog box with the setOpen function with the false argument.

Then open prop controls whether it’s opened or not.

handleClose lets us close the dialog by setting open to false with setOpen .

variant set to outlined lets us add an outline.

Alerts

Alerts are used for display urgent messages.

To add one, we can use the components that we have before and add the DialogContent , DialogContentText , and DialogActions components to add the dialog content.

For example, we can write:

import React from "react";
import Button from "@material-ui/core/Button";
import Dialog from "@material-ui/core/Dialog";
import DialogTitle from "@material-ui/core/DialogTitle";
import DialogContent from "@material-ui/core/DialogContent";
import DialogContentText from "@material-ui/core/DialogContentText";
import DialogActions from "@material-ui/core/DialogActions";

export default function App() {
  const [open, setOpen] = React.useState(false);

  const handleClose = () => {
    setOpen(false);
  };

  return (
    <div>
      <Button variant="outlined" color="primary" onClick={() => setOpen(true)}>
        Open simple dialog
      </Button>
      <Dialog open={open} onClose={handleClose}>
        <DialogTitle>Confirm</DialogTitle>
        <DialogContent>
          <DialogContentText>Are you sure?</DialogContentText>
        </DialogContent>
        <DialogActions>
          <Button onClick={handleClose} color="primary">
            no
          </Button>
          <Button onClick={handleClose} color="primary" autoFocus>
            yes
          </Button>
        </DialogActions>
      </Dialog>
    </div>
  );
}

to add everything.

DialogTitle has the title.

DialogContent has the content.

DialogContentText is where we put the text for the dialog.

DialogActions has the buttons to let users do something.

Transitions

We can add transition effects to animate when dialogs open.

For example, we can write:

import React from "react";
import Button from "@material-ui/core/Button";
import Dialog from "@material-ui/core/Dialog";
import DialogTitle from "@material-ui/core/DialogTitle";
import DialogContent from "@material-ui/core/DialogContent";
import DialogContentText from "@material-ui/core/DialogContentText";
import DialogActions from "@material-ui/core/DialogActions";
import Slide from "@material-ui/core/Slide";

const Transition = React.forwardRef((props, ref) => {
  return <Slide direction="down" ref={ref} {...props} />;
});

export default function App() {
  const [open, setOpen] = React.useState(false);

  const handleClose = () => {
    setOpen(false);
  };

  return (
    <div>
      <Button variant="outlined" color="primary" onClick={() => setOpen(true)}>
        Open simple dialog
      </Button>
      <Dialog
        open={open}
        onClose={handleClose}
        TransitionComponent={Transition}
      >
        <DialogTitle>Confirm</DialogTitle>
        <DialogContent>
          <DialogContentText>Are you sure?</DialogContentText>
        </DialogContent>
        <DialogActions>
          <Button onClick={handleClose} color="primary">
            no
          </Button>
          <Button onClick={handleClose} color="primary" autoFocus>
            yes
          </Button>
        </DialogActions>
      </Dialog>
    </div>
  );
}

We created the Transition component which uses the Slide component to create a slide down effect.

direction sets the direction of the slide.

We’ve to pass the ref of the dialog into the Slide prop to make it slide.

Then we add the TransitionComponent prop to pass in the Transition component to it.

Form Dialogs

We can add forms inside dialog boxes.

For example, we can write:

import React from "react";
import Button from "@material-ui/core/Button";
import Dialog from "@material-ui/core/Dialog";
import DialogTitle from "@material-ui/core/DialogTitle";
import DialogContent from "@material-ui/core/DialogContent";
import DialogContentText from "@material-ui/core/DialogContentText";
import DialogActions from "@material-ui/core/DialogActions";
import TextField from "@material-ui/core/TextField";

export default function App() {
  const [open, setOpen] = React.useState(false);

  const handleClose = () => {
    setOpen(false);
  };

  return (
    <div>
      <Button variant="outlined" color="primary" onClick={() => setOpen(true)}>
        Open simple dialog
      </Button>
      <Dialog open={open} onClose={handleClose}>
        <DialogTitle>Confirm</DialogTitle>
        <DialogContent>
          <DialogContentText>Enter your name</DialogContentText>
          <TextField
            autoFocus
            margin="dense"
            id="name"
            label="name"
            type="text"
            fullWidth
          />
        </DialogContent>
        <DialogActions>
          <Button onClick={handleClose} color="primary">
            cancel
          </Button>
          <Button onClick={handleClose} color="primary" autoFocus>
            ok
          </Button>
        </DialogActions>
      </Dialog>
    </div>
  );
}

to add a TextField inside our dialog box.

We make it full width with the fullWidth prop to make it fit inside the dialog box.

Conclusion

We can add a dialog to display various things.

It can include buttons and forms.

Transition effects can also be included.

Categories
Material UI

Material UI — Dialog Customization

Material UI is a Material Design library made for React.

It’s a set of React components that have Material Design styles.

In this article, we’ll look at how to customize dialog boxes with Material UI.

Customized Dialogs

We can create our own dialog components by putting into our own components and passing in various styles to it.

For example, we can write:

import React from "react";
import { withStyles } from "@material-ui/core/styles";
import Button from "@material-ui/core/Button";
import Dialog from "@material-ui/core/Dialog";
import MuiDialogTitle from "@material-ui/core/DialogTitle";
import MuiDialogContent from "@material-ui/core/DialogContent";
import MuiDialogActions from "@material-ui/core/DialogActions";
import IconButton from "@material-ui/core/IconButton";
import CloseIcon from "@material-ui/icons/Close";
import Typography from "@material-ui/core/Typography";

const styles = theme => ({
  root: {
    margin: 0,
    padding: theme.spacing(3)
  },
  closeButton: {
    position: "absolute",
    right: theme.spacing(1),
    top: theme.spacing(2),
    color: theme.palette.grey[500]
  }
});

const DialogTitle = withStyles(styles)(props => {
  const { children, classes, onClose, ...other } = props;
  return (
    <MuiDialogTitle disableTypography className={classes.root} {...other}>
      <Typography variant="h6">{children}</Typography>
      {onClose ? (
        <IconButton className={classes.closeButton} onClick={onClose}>
          <CloseIcon />
        </IconButton>
      ) : null}
    </MuiDialogTitle>
  );
});

const DialogContent = withStyles(theme => ({
  root: {
    padding: theme.spacing(2)
  }
}))(MuiDialogContent);

const DialogActions = withStyles(theme => ({
  root: {
    margin: 0,
    padding: theme.spacing(1)
  }
}))(MuiDialogActions);

export default function App() {
  const [open, setOpen] = React.useState(false);

  const handleClickOpen = () => {
    setOpen(true);
  };
  const handleClose = () => {
    setOpen(false);
  };

  return (
    <div>
      <Button variant="outlined" color="primary" onClick={handleClickOpen}>
        Open dialog
      </Button>
      <Dialog onClose={handleClose} open={open}>
        <DialogTitle onClose={handleClose}>Modal</DialogTitle>
        <DialogContent dividers>
          <Typography gutterBottom>lorem ipsum.</Typography>
        </DialogContent>
        <DialogActions>
          <Button autoFocus onClick={handleClose} color="primary">
            ok
          </Button>
        </DialogActions>
      </Dialog>
    </div>
  );
}

We create a styles function that we pass into the withStyles higher-order components with various styles.

We moved the close button by setting some styles for the closeButton class.

To create the DialogTitle component, we passed a component that uses the classes and children from the props.

The classes are applied to icon buttons and dialog titles.

Also, we created the DialogContent and DialogActions in similar ways.

We changed the padding slightly to create both components.

Then we used them all in the App component.

Full-Screen Dialogs

We can make dialogs full screen by adding the fullscreen prop to Dialog .

For example, we can write:

import React from "react";
import Button from "@material-ui/core/Button";
import Dialog from "@material-ui/core/Dialog";
import DialogTitle from "@material-ui/core/DialogTitle";
import DialogContent from "@material-ui/core/DialogContent";
import DialogContentText from "@material-ui/core/DialogContentText";
import DialogActions from "@material-ui/core/DialogActions";

export default function App() {
  const [open, setOpen] = React.useState(false);
  const handleClose = () => {
    setOpen(false);
  };
  return (
    <div>
      <Button variant="outlined" color="primary" onClick={() => setOpen(true)}>
        Open dialog
      </Button>
      <Dialog fullScreen open={open} onClose={handleClose}>
        <DialogTitle>Title</DialogTitle>
        <DialogContent>
          <DialogContentText>lorem ipsum</DialogContentText>
        </DialogContent>
        <DialogActions>
          <Button onClick={handleClose} color="primary" autoFocus>
            ok
          </Button>
        </DialogActions>
      </Dialog>
    </div>
  );
}

to create a dialog that’s full screen with the fullScreen prop.

Optional Sizes

We can change the size of the dialog with the maxWidth prop to set the max width of the dialog.

fullWidth has the full width.

For example, we can write:

import React from "react";
import Button from "@material-ui/core/Button";
import Dialog from "@material-ui/core/Dialog";
import DialogTitle from "@material-ui/core/DialogTitle";
import DialogContent from "@material-ui/core/DialogContent";
import DialogContentText from "@material-ui/core/DialogContentText";
import DialogActions from "@material-ui/core/DialogActions";

export default function App() {
  const [open, setOpen] = React.useState(false);
  const handleClose = () => {
    setOpen(false);
  };
  return (
    <div>
      <Button variant="outlined" color="primary" onClick={() => setOpen(true)}>
        Open dialog
      </Button>
      <Dialog fullWidth maxWidth="sm" open={open} onClose={handleClose}>
        <DialogTitle>Title</DialogTitle>
        <DialogContent>
          <DialogContentText>lorem ipsum</DialogContentText>
        </DialogContent>
        <DialogActions>
          <Button onClick={handleClose} color="primary" autoFocus>
            ok
          </Button>
        </DialogActions>
      </Dialog>
    </div>
  );
}

to add the fullWidth and maxWidth props to the Dialog .

The fullWidth prop will display the dialog that stretches to the width of the screen if it’s narrower than the breakpoint given in maxWidth .

Otherwise, it’ll display with the max-width of the given breakpoint.

Conclusion

We can add dialogs with its own styles, full-screen dialogs, and also width a max-width.

Categories
Material UI

Material UI — Customize Tabs

Material UI is a Material Design library made for React.

It’s a set of React components that have Material Design styles.

In this article, we’ll look at how to customize tabs with Material UI.

Centered

We can make tabs centered with the centered prop.

For example, we can write:

import React from "react";
import AppBar from "@material-ui/core/AppBar";
import Tabs from "@material-ui/core/Tabs";
import Tab from "@material-ui/core/Tab";
import Box from "@material-ui/core/Box";

function TabPanel(props) {
  const { children, value, index, ...other } = props;

  return <div {...other}>{value === index && <Box p={3}>{children}</Box>}</div>;
}

export default function App() {
  const [value, setValue] = React.useState(0);

  const handleChange = (event, newValue) => {
    setValue(newValue);
  };

  return (
    <>
      <AppBar position="static">
        <Tabs value={value} onChange={handleChange} centered>
          <Tab label="Item One" />
          <Tab label="Item Two" />
        </Tabs>
      </AppBar>
      <TabPanel value={value} index={0}>
        Item One
      </TabPanel>
      <TabPanel value={value} index={1}>
        Item Two
      </TabPanel>
    </>
  );
}

Then the tabs will be centered.

Scrollable Tabs

We can make tabs scrollable with the scrollButtons set to auto and variant set to scrollable .

This way, the scroll buttons will be shown on mobile and hidden in the desktop.

For example, we can write:

import React from "react";
import AppBar from "@material-ui/core/AppBar";
import Tabs from "@material-ui/core/Tabs";
import Tab from "@material-ui/core/Tab";
import Box from "@material-ui/core/Box";

function TabPanel(props) {
  const { children, value, index, ...other } = props;

  return <div {...other}>{value === index && <Box p={3}>{children}</Box>}</div>;
}

export default function App() {
  const [value, setValue] = React.useState(0);

  const handleChange = (event, newValue) => {
    setValue(newValue);
  };

  return (
    <>
      <AppBar position="static">
        <Tabs
          value={value}
          onChange={handleChange}
          variant="scrollable"
          scrollButtons="auto"
        >
          {Array(10)
            .fill()
            .map((_, i) => (
              <Tab label={`Item ${i + 1}`} />
            ))}
        </Tabs>
      </AppBar>
      {Array(10)
        .fill()
        .map((_, i) => (
          <TabPanel value={value} index={i}>
            Item {i + 1}
          </TabPanel>
        ))}
    </>
  );
}

We created 10 tabs with the Array constructor and mapped them to tabs.

Similar, we did the same with the TabPanel s.

Now we should see an arrow button when the tabs overflow the page.

On mobile, we can scroll with swiping.

Prevent Scroll Buttons

We can turn off scroll buttons with the scrollButtons prop set to off .

For example, we can write:

import React from "react";
import AppBar from "@material-ui/core/AppBar";
import Tabs from "@material-ui/core/Tabs";
import Tab from "@material-ui/core/Tab";
import Box from "@material-ui/core/Box";

function TabPanel(props) {
  const { children, value, index, ...other } = props;

  return <div {...other}>{value === index && <Box p={3}>{children}</Box>}</div>;
}

export default function App() {
  const [value, setValue] = React.useState(0);

  const handleChange = (event, newValue) => {
    setValue(newValue);
  };

  return (
    <>
      <AppBar position="static">
        <Tabs
          value={value}
          onChange={handleChange}
          variant="scrollable"
          scrollButtons="off"
        >
          {Array(10)
            .fill()
            .map((_, i) => (
              <Tab label={`Item ${i + 1}`} />
            ))}
        </Tabs>
      </AppBar>
      {Array(10)
        .fill()
        .map((_, i) => (
          <TabPanel value={value} index={i}>
            Item {i + 1}
          </TabPanel>
        ))}
    </>
  );
}

And now we’ll never see the tab scroll buttons.

Customizing Tab Styles

We can customize the tab styles with the withStyles higher-order component.

For example, we can write:

import React from "react";
import AppBar from "@material-ui/core/AppBar";
import Tabs from "@material-ui/core/Tabs";
import Tab from "@material-ui/core/Tab";
import Box from "@material-ui/core/Box";
import { withStyles } from "@material-ui/core/styles";

const StyledTab = withStyles(theme => ({
  root: {
    textTransform: "none",
    color: "yellow",
    fontWeight: theme.typography.fontWeightRegular,
    fontSize: theme.typography.pxToRem(15),
    marginRight: theme.spacing(1)
  }
}))(props => <Tab disableRipple {...props} />);

function TabPanel(props) {
  const { children, value, index, ...other } = props;

  return <div {...other}>{value === index && <Box p={3}>{children}</Box>}</div>;
}

export default function App() {
  const [value, setValue] = React.useState(0);

  const handleChange = (event, newValue) => {
    setValue(newValue);
  };

  return (
    <>
      <AppBar position="static">
        <Tabs
          value={value}
          onChange={handleChange}
          variant="scrollable"
          scrollButtons="off"
        >
          {Array(5)
            .fill()
            .map((_, i) => (
              <StyledTab label={`Item ${i + 1}`} />
            ))}
        </Tabs>
      </AppBar>
      {Array(5)
        .fill()
        .map((_, i) => (
          <TabPanel value={value} index={i}>
            Item {i + 1}
          </TabPanel>
        ))}
    </>
  );
}

We call with the withStyles function with an object with some styles.

And we pass in a function that returns a Tab with props passed into it.

This way, we apply the styles in the object to the tab.

Then we can use the returned StyledTab in our App component.

Conclusion

We can customize tabs in various ways.

The styles, scroll ability, and more can be changed.

Categories
Material UI

Material UI — Breadcrumbs and Drawers

Material UI is a Material Design library made for React.

It’s a set of React components that have Material Design styles.

In this article, we’ll look at how to add breadcrumb styling and drawers to Material UI.

Customized Breadcrumbs

We can add styles to breadcrumb items by using the withStyles higher-order component.

For instance, we can write:

import React from "react";
import Breadcrumbs from "@materialui/core/Breadcrumbs";
import { emphasize, withStyles } from "@materialui/core/styles";
import Chip from "@materialui/core/Chip";
import HomeIcon from "@materialui/icons/Home";

const StyledBreadcrumb = withStyles(theme => ({
  root: {
    backgroundColor: theme.palette.grey[100],
    height: theme.spacing(6),
    "&:hover, &:focus": {
      backgroundColor: theme.palette.grey[300]
    },
    "&:active": {
      boxShadow: theme.shadows[1],
      backgroundColor: emphasize(theme.palette.grey[300], 0.12)
    }
  }
}))(Chip);

export default function App() {
  const handleClick = event => {
    event.preventDefault();
    console.info("clicked.");
  };
  return (
    <Breadcrumbs>
      <StyledBreadcrumb
        href="#"
        label="home"
        icon={<HomeIcon fontSize="small" />}
        onClick={handleClick}
      />
      <StyledBreadcrumb
        href="#"
        label="profile"
        onClick={handleClick}
      />
      <StyledBreadcrumb
        label="settings"
        onClick={handleClick}
        onDelete={handleClick}
      />
    </Breadcrumbs>
  );
}

We use the withStyles higher-order component to add some styles to a Chip component to add styles to a rounded box.

We ads the label onClick and onDelete prop as with any regular breadcrumb item.

Integration with react-router

We can incorporate React Router links to our breadcrumbs.

For example, we can write:

import React from "react";
import Breadcrumbs from "@materialui/core/Breadcrumbs";
import { Route, MemoryRouter } from "react-router";
import { Link as RouterLink } from "react-router-dom";
import Link from "@materialui/core/Link";

const LinkRouter = props => <Link {...props} component={RouterLink} />;

export default function App() {
  return (
    <div>
      <MemoryRouter initialEntries={["/"]} initialIndex={0}>
        <Route>
          {({ location }) => {
            return (
              <Breadcrumbs aria-label="breadcrumb">
                <LinkRouter color="inherit" to="/">
                  home
                </LinkRouter>
                <LinkRouter color="inherit" to="/profile">
                  profile
                </LinkRouter>
                <LinkRouter color="inherit" to="/settings">
                  settings
                </LinkRouter>
              </Breadcrumbs>
            );
          }}
        </Route>
      </MemoryRouter>
    </div>
  );
}

to create a LinkRouter component to return the Link component that we display in the breadcrumbs.

We pass in the to prop to our LinkRouter component to set the URL that the link goes to.

Drawer

A nav drawer lets us access destinations within our app.

It’s displayed on the side and has links to let us click on them.

For instance, we can write:

import React from "react";
import Drawer from "@materialui/core/Drawer";
import ListItemIcon from "@materialui/core/ListItemIcon";
import ListItem from "@materialui/core/ListItem";
import ListItemText from "@materialui/core/ListItemText";
import List from "@materialui/core/List";
import MailIcon from "@materialui/icons/Mail";

export default function App() {
  const [open, setOpen] = React.useState(true);
  const toggleDrawer = event => {
    setOpen(false);
  };
  return (
    <div>
      <Drawer anchor="left" open={open} onClose={toggleDrawer}>
        <List>
          <ListItem button key="home">
            <ListItemIcon>
              <MailIcon />
            </ListItemIcon>
            <ListItemText primary="home" />
          </ListItem>
          <ListItem button key="profile">
            <ListItemIcon>
              <MailIcon />
            </ListItemIcon>
            <ListItemText primary="profile" />
          </ListItem>
        </List>
      </Drawer>
    </div>
  );
}

We have the Drawer component with a anchor prop to make it display in the position that we want.

anchor can also be right , top or bottom .

open has the open state of the drawer.

onClose has a function that’s called when it’s closed.

We then render the drawer with the Drawer component.

Inside it, we have a List with some ListItem s.

ListItemIcon has the icon of each entry.

ListItemText has the item text.

Swipeable Drawer

We can add a SwipeableDrawer to make the drawer swipeable.

This works better on mobile devices.

For example, we can write:

import React from "react";
import ListItemIcon from "@materialui/core/ListItemIcon";
import ListItem from "@materialui/core/ListItem";
import ListItemText from "@materialui/core/ListItemText";
import List from "@materialui/core/List";
import MailIcon from "@materialui/icons/Mail";
import SwipeableDrawer from "@materialui/core/SwipeableDrawer";

export default function App() {
  const [open, setOpen] = React.useState(true);
  const toggleDrawer = event => {
    setOpen(false);
  };
  return (
    <div>
      <SwipeableDrawer anchor="left" open={open} onClose={toggleDrawer}>
        <List>
          <ListItem button key="home">
            <ListItemIcon>
              <MailIcon />
            </ListItemIcon>
            <ListItemText primary="home" />
          </ListItem>
          <ListItem button key="profile">
            <ListItemIcon>
              <MailIcon />
            </ListItemIcon>
            <ListItemText primary="profile" />
          </ListItem>
        </List>
      </SwipeableDrawer>
    </div>
  );
}

All we did is replace Drawer and SwipeableDrawer .

Conclusion

We can customize our breadcrumbs by changing its styles and content.

It also integrates with React Router.

We can add a drawer to add a sidebar.

Categories
Material UI

Material UI — Badges and Chips

Material UI is a Material Design library made for React.

It’s a set of React components that have Material Design styles.

In this article, we’ll look at how to customize badges and add chips with Material UI.

Maximum Value of Badges

We can set the maximum value of badges.

It has the max prop to let us do that.

For example, we can write:

import React from "react";
import Badge from "@material-ui/core/Badge";
import MailIcon from "@material-ui/icons/Mail";

export default function App() {
  return (
    <>
      <Badge badgeContent={1000} max={999} color="secondary">
        <MailIcon />
      </Badge>
    </>
  );
}

We added the Mailicon with a badge with the max prop.

It’s set to 999. So when badgeContent is bigger than that, it’ll display 999+.

Badge Overlap

We can change how badges overlap with icons.

For example, we can write:

import React from "react";
import clsx from "clsx";
import MailIcon from "@material-ui/icons/Mail";
import Badge from "@material-ui/core/Badge";

export default function App() {
  return (
    <div>
      <Badge color="secondary" overlap="circle" badgeContent=" " variant="dot">
        <MailIcon />
      </Badge>
    </div>
  );
}

We added the overlap ptop and set that to circle to make it work with circles.

Dot Badge

To make the badge display as a dot, we can use the variant set to dot .

For example, we can write:

import React from "react";
import clsx from "clsx";
import MailIcon from "@material-ui/icons/Mail";
import Badge from "@material-ui/core/Badge";

export default function App() {
  return (
    <div>
      <Badge color="secondary" variant="dot">
        <MailIcon />
      </Badge>
    </div>
  );
}

With the variant set to dot , we can see the dot above the mail icon.

Badge Alignment

To change the alignment of the badge, we can use the anchorOrigin prop to set the badge position.

For example, we can write:

import React from "react";
import clsx from "clsx";
import MailIcon from "@material-ui/icons/Mail";
import Badge from "@material-ui/core/Badge";

export default function App() {
  return (
    <div>
      <Badge
        color="secondary"
        variant="dot"
        anchorOrigin={{
          vertical: "top",
          horizontal: "left"
        }}
      >
        <MailIcon />
      </Badge>
    </div>
  );
}

to place the badge on the top left of the icon.

Chip

Chips are small elements that represent an input, attribute, or action.

We can add them by using the Chip component:

import React from "react";
import Chip from "@material-ui/core/Chip";

export default function App() {
  return (
    <div>
      <Chip label="chip" />
    </div>
  );
}

We add a chip with the Chip component.

The label text is displayed as the content.

Outlined Chips

We can add a chip with an outline with the variant set to outlined .

For example, we can write:

import React from "react";
import Chip from "@material-ui/core/Chip";

export default function App() {
  return (
    <div>
      <Chip label="chip" variant="outlined" />
    </div>
  );
}

Now we see an outline instead of a gray background.

Chip Array

We can make a group of chips by putting them in an array.

For instance, we can write:

import React from "react";
import Chip from "@material-ui/core/Chip";

export default function App() {
  const [chipData] = React.useState([
    { key: 0, label: "james" },
    { key: 1, label: "mary" },
    { key: 2, label: "alex" },
    { key: 3, label: "john" },
    { key: 4, label: "david" }
  ]);

  return (
    <div>
      {chipData.map(c => (
        <Chip label={c.label} key={c.key} />
      ))}
    </div>
  );
}

to put them in an array.

We can let users delete a chip with the onDelete prop:

import React from "react";
import Chip from "@material-ui/core/Chip";

export default function App() {
  const [chipData, setChipData] = React.useState([
    { key: 0, label: "james" },
    { key: 1, label: "mary" },
    { key: 2, label: "alex" },
    { key: 3, label: "john" },
    { key: 4, label: "david" }
  ]);

  const handleDelete = chipToDelete => () => {
    setChipData(chips => chips.filter(chip => chip.key !== chipToDelete.key));
  };

  return (
    <div>
      {chipData.map(c => (
        <Chip label={c.label} key={c.key} onDelete={handleDelete(c)} />
      ))}
    </div>
  );
}

We added the onDelete prop which takes the handleDelete function called with the chips entry we want to delete.

The function returns a function that sets the chip’s data to the new state without the entry we want to delete.

The ‘x’ icon will be shown to let us remove the chip.

Conclusion

We can add chips to display a small piece of data to the user.

They can be deleted.