List
๊ฐ ListItem
์ Link
๊ตฌ์ฑ ์์๋ก ๋ง๋ค๊ณ ์ถ์ต๋๋ค.
Drawer
๊ตฌ์ฑ ์์๋ก ๊ตฌ์ฑ๋ List
๊ตฌ์ฑ ์์๊ฐ ์์ต๋๋ค.
ํด๋น ๋ชฉ๋ก์ 4 ๊ฐ์ ListItem
๋ก ๊ตฌ์ฑ๋ฉ๋๋ค.
๋ด๊ฐ ๋ฌ์ฑํ๊ณ ์ถ์ ๊ฒ์ ๊ฐ ListItem
์ React ๋ผ์ฐํฐ Link
์
๋๋ค.
์๋์ onClick ๋ฉ์๋๋ฅผ ์์ ํ๊ณ ํด๋ฆญ ํ ListItem์ Id๋ฅผ ์ป์ ๋ค์ ID๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์์ผ๋ก ์ฐฝ์ ์์น๋ฅผ โโ๋ณ๊ฒฝํ๋ ค๊ณ ํ์ต๋๋ค.
๊ทธ๋ฌ๋ ๋๋์ด ์ ๊ทผ๋ฒ์ด ๋ฐ์ ๊ฐ๋
๋ณด๋ค ๋ jqueryish๋ผ๊ณ ์๊ฐํฉ๋๋ค.
๋ํ ์ถ๊ฐํ๋ ค๊ณ Link
์ component
์ ์ํ ListItem
,ํ์ง๋ง ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค. (๋๋ ๋น์ ์ด์์ ์ฌ์ฉํ ๋ณธ ์ ์ด component
์ ์ํ ๋ฌธ์).
์ด ์๊ตฌ ์ฌํญ์ ์ถฉ์กฑํ๋ ๊ถ์ฅ ๋ฐฉ๋ฒ์ ๋ฌด์์
๋๊น?
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { withStyles } from 'material-ui/styles';
import Drawer from 'material-ui/Drawer';
import List, { ListItem, ListItemIcon, ListItemText } from 'material-ui/List';
import Face from 'material-ui-icons/Face';
import Person from 'material-ui-icons/Person';
import Assignment from 'material-ui-icons/Assignment';
import Link from 'react-router-dom/Link';
import { FormattedMessage } from 'react-intl';
const styles = {
list: {
width: 250,
flex: 'initial',
},
};
class UndockedDrawer extends Component {
constructor() {
super();
this.onClick = this.onClick.bind(this);
}
onClick(e, data) {
console.log(data);
this.props.onRequestClose();
}
render() {
const classes = this.props.classes;
const sidebarListItems = (
<div>
<ListItem button >
<ListItemIcon>
<Person />
</ListItemIcon>
<ListItemText primary={<FormattedMessage id="user" />} />
</ListItem>
<ListItem button>
<ListItemIcon>
<Assignment />
</ListItemIcon>
<ListItemText primary={<FormattedMessage id="consultant" />} />
</ListItem>
<ListItem button>
<ListItemIcon>
<Face />
</ListItemIcon>
<ListItemText primary={<FormattedMessage id="student" />} />
</ListItem>
</div>
);
const sidebarList = (
<div>
<List className={classes.list} >
{sidebarListItems}
</List>
</div>
);
return (
<div>
<Drawer
anchor="right"
open={this.props.open}
onRequestClose={this.props.onRequestClose}
onClick={this.onClick}
>
{sidebarList}
</Drawer>
</div>
);
}
}
UndockedDrawer.propTypes = {
classes: PropTypes.shape({}).isRequired,
open: PropTypes.bool.isRequired,
onRequestClose: PropTypes.func.isRequired,
};
export default withStyles(styles)(UndockedDrawer);
๋ค์๊ณผ ๊ฐ์ด Link
ListItem
์ (๋ฅผ) ๋ํ ํ ์ ์์ต๋๋ค.
<Link to="users" style={{ textDecoration: 'none' }}>
<ListItem button >
<ListItemIcon>
<Person />
</ListItemIcon>
<ListItemText primary={<FormattedMessage id="user" />} />
</ListItem>
</Link>
์ด ์ ๊ทผ ๋ฐฉ์์ ์ฌ์ฉํ๋ฉด onClick ๋ฉ์๋๋ ์คํ๋ฉ๋๋ค.
์ ์์ด๋ ๊ฐ์ ์ฌํญ์ด ์์ต๋๊น?
๋๋ ๋ค์๊ณผ ๊ฐ์ด Link์ ListItem์ ๋ํ ํ ์ ์์์ ์์์ต๋๋ค.
์ด ์๋ฃจ์ ์ ์๋ฏธ ์ ์ ํจํ์ง ์์ต๋๋ค. ๋ฌธ์์์ ๋ฌธ์ ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ์ดํด๋ณด์ญ์์ค.
๋น์ ์ ๋ํ ์ด๊ฒ์ ์๋ ํ ์ ์์ต๋๋ค
์ด๋ https://material-ui.com/components/buttons/#third -party-routing-library ๋ฌธ์์์ ๋ค๋ฃน๋๋ค.
import React from 'react';
import { MemoryRouter as Router } from 'react-router';
import { Link, LinkProps } from 'react-router-dom';
import ListItem from '@material-ui/core/ListItem';
import { Omit } from '@material-ui/types';
// The usage of React.forwardRef will no longer be required for react-router-dom v6.
// see https://github.com/ReactTraining/react-router/issues/6056
const AdapterLink = React.forwardRef<HTMLAnchorElement, LinkProps>((props, ref) => (
<Link innerRef={ref as any} {...props} />
));
const CollisionLink = React.forwardRef<HTMLAnchorElement, Omit<LinkProps, 'innerRef' | 'to'>>(
(props, ref) => <Link innerRef={ref as any} to="/getting-started/installation/" {...props} />,
);
export default function ButtonRouter() {
return (
<Router>
<ListItem color="primary" component={AdapterLink} to="/">
Simple case
</ListItem>
<ListItem component={CollisionLink}>Avoids props collision</ListItem>
</Router>
);
}
๋ฌธ์์์ ๋ณผ ์ ์์ต๋๋ค.
https://material-ui-next.com/demos/lists/
https://material-ui-next.com/demos/drawers/
๋ผ์ฐํฐ ๋ฐ์ ์ฌ์ฉํ ๋ ์ผ๋ฐ์ ์ธ ์ผ์ด์ง๋ง ๊ทธ๊ฒ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ถ๋ช
ํ์ง ์๋ค NavLink
A์ ListItem
.
component
์ํ์ด์๋ Button
?
๋ฒํผ ์ํ๊ณผ ๊ตฌ์ฑ ์์ ์ํ์ด ๋์ผํ JSX ํ๊ทธ์ ํจ๊ป ํผํฉ๋์ด ์์ต๋๊น?
์ด๋ฐ ์ข
๋ฅ์ ๋ฏน์ค ์ธ์ ๋ฌด์์
๋๊น?
์ ๊ทธ๋ ๊ฒ ๋ณต์กํ๊ฐ์?
@mehrdaad ์ ์๋ฃจ์
์ ์ฌ์ฉํ ๋ ๋งํฌ๋ ์ผ๋ฐ MUI ๋ชฉ๋ก์ฒ๋ผ ๋ณด์ด์ง๋ง @oliviertassinari๊ฐ ์ ์ํ๋๋ก component={Link}
๋ฅผ ์ฌ์ฉํ๋ฉด ์คํ์ผ์ด ์๋ง์ด๋ฉ๋๋ค.
(์ด๋ฏธ์ง์์ ๋ง์ฐ์ค๋ "Build"์์ ์์ง๋ง ๋ ๋ฒ์งธ ํญ๋ชฉ์๋ ๋ฐ์ค์ด ํ์๋ฉ๋๋ค.)
@ilyador ์ด UI ๋งํฌ ์คํ์ผ ๋ฌธ์ ๊ฐ ํด๊ฒฐ๋์์ต๋๋ค.
@ zhangwei900808 ์๋์, ์ฌ์ ํ ์๋ฏธ ์ ์๋ชป๋์์ต๋๋ค. <ul><a ..>...
์์ฑ๋ฉ๋๋ค.
https://codesandbox.io/s/lpwq74p30m
์์ ์ ๊ณต๋ ๋ต๋ณ์ ์ฌ์ฉํ์ญ์์ค.
@oliviertassinari ๋ ์ฌ์ด ๋ฐฉ๋ฒ์ด
const styles = {
li: {
'&:hover': {
backgroundColor: 'transparent',
},
},
};
// ...
<MenuItem disableGutters className={classes.li}>
<Button component={Link} to="/logout" ...
์ด์ค ํธ๋ฒ ํจ๊ณผ๋ฅผ ๋ฐฉ์งํ๋ ค๋ฉด MenuItem
๋ฐ Button
(์ฌ์ ํ ์ฝ๊ฐ์ ๊ฑฐํฐ / ํจ๋ฉ์ด ์์, ๋ ๋ค๋ฅธ ๋ฌธ์ )
๋ฒ๊ฑฐ๋กญ๊ธฐ ๋๋ฌธ์
๋ฐ๋ชจ : https://codesandbox.io/s/nk834yk5pl (์ฌ์ฉํ์ง ์๋ component={Link} to="/..."
ํ์ง๋ง ๋์ผํฉ๋๋ค)
@caub ์ถ๊ฐ Button ๊ตฌ์ฑ ์์๋ฅผ ๋์ ํ๋ ์ด์ ๋ ๋ฌด์์ ๋๊น? MenuItem์์ ๊ตฌ์ฑ ์์ ์์ฑ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
@oliviertassinari ์๋ํ๋ฉด ul > a
์ ์ ํจํ์ง ์๊ธฐ ๋๋ฌธ์
๋๋ค. html5 https://codesandbox.io/s/lpwq74p30m
@caub nav > a
์ (๋) ์ ํจํฉ๋๋ค.
์ข์, ๊ฐ์ฌํฉ๋๋ค. https://codesandbox.io/s/lpwq74p30m
๋ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐ ํ ํด๊ฒฐ์ฑ ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
<List>
{menus.map((menu: any, index: any) => {
return (
<ListItem
key={index}
{...{ to: menu.link }}
component={Link}
button={true}
>
<ListItemIcon>{menu.icon}</ListItemIcon>
<ListItemText primary={menu.text} />
</ListItem>
);
})}
</List>
๋ฐ๋ผ์ ListItem์ ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ์ญ์์ค.
** {... {to : menu.link}} **
component = {Link}
button = {true}
>
import {Link} from 'react-router-dom' <ListItem component={Link} to="/" button> // or <ListItem component={props => <Link to="/" {...props} />} button>
ํธ์ ๋ด์ ๊ฐ๊ธฐ
์ด๊ฒ์ ๋์๊ฒ ์๋ฒฝํ์ต๋๋ค. ๊ฐ์ฌ!
MUI v4 ๋ง์ด๊ทธ๋ ์ด์ (4.3.1์ ์์) ์ดํ ๊ฐ์ฅ ๋ง์ด ๋ํ ํ ๋ต๋ณ์ "ํจ์ ๊ตฌ์ฑ ์์์ ์ฐธ์กฐ๋ฅผ ์ ๊ณต ํ ์ ์์ต๋๋ค.์ด ์ฐธ์กฐ์ ์ก์ธ์คํ๋ ค๋ ์๋๊ฐ ์คํจํฉ๋๋ค. React.forwardRef ()๋ฅผ ์ฌ์ฉํ๋ ค๊ณ ํ์ต๋๊น?"๋ก ์ธํด ์คํจํ์ต๋๋ค.
NavLink
๊ตฌ์ฑ ์์๋ฅผ ๋ค์๊ณผ ๊ฐ์ ๋ํผ๋ก ๊ต์ฒดํ์ต๋๋ค.
import React, { Component } from "react"
import { NavLink } from "react-router-dom"
/**
* React Router Nav Link wrapper to forward the ref to fix "Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?"
*
* From https://material-ui.com/guides/composition/#caveat-with-refs
*/
class NavLinkMui extends Component {
render() {
const { forwardedRef, ...props } = this.props
return <NavLink {...props} ref={forwardedRef} />
}
}
export default NavLinkMui
์ฉ๋ฒ:
<ListItem
button
component={NavLinkMui}
to='/'
>
<ListItemIcon>
<SlideshowIcon />
</ListItemIcon>
</ListItem>
@HugoGresse ์, forward ref API๋ฅผ ์ฌ์ฉํด์ผํฉ๋๋ค. ๋ฌธ์์์ ์์ ๋ฅผ ์ฐพ์ ์ ์์ด์ผํฉ๋๋ค. https://github.com/mui-org/material-ui/issues/7956#issuecomment -326908167์ ์ต์ ๋ต๋ณ์ผ๋ก ์ ๋ฐ์ดํธํ์ต๋๋ค. ๊ด์ฐฎ์์ง ์๋ ค์ฃผ์ธ์.
@oliviertassinari ๊ทธ๋ฌ ์ต๋๋ค.
Button ํ
์คํธ ์์์ด ํ
๋ง ํ๋ ํธ ์์์ ๊ณ ๋ คํ์ง ์๋ forwardRef ์๋ฃจ์
๋๋ Component 1์ ์ฌ์ ํ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ํ
์คํธ ์์์ palette.type: 'dark'
์ ์ผ์นํ๋๋ก ํฐ์์ด์ด์ผํ์ง๋ง ํฐ์์
๋๋ค.
component
์ํ ์์ด๋ ์ฌ์ ํ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๊ธฐ ๋๋ฌธ์ MUI์ ๋ณ๊ฒฝ ์ฌํญ๊ณผ ์ฐ๊ฒฐ๋์์ ์ ์์ต๋๋ค.
์ฌ๊ธฐ์์ ์ฝ๋ ์ฌ๋๋ค์ ์ํด ์ถ๊ฐ ์ํ์ ListItemText์ ์ ๋ฌํ ์ ์์ต๋๋ค.
<ListItemText
primaryTypographyProps={{
color: 'textPrimary'
}}
primary="Dashboard"
/>
BTW, ๊ทํ์ ์๋ ์ด์ TS์ ์์ต๋๋ค.
๋ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐ ํ ํด๊ฒฐ์ฑ ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
<List> {menus.map((menu: any, index: any) => { return ( <ListItem key={index} {...{ to: menu.link }} component={Link} button={true} > <ListItemIcon>{menu.icon}</ListItemIcon> <ListItemText primary={menu.text} /> </ListItem> ); })} </List>
๋ฐ๋ผ์ ListItem์ ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ์ญ์์ค.
key = {index}
** {... {to : menu.link}} **
component = {Link}
button = {true}
>
button={true}
๋จ์ํ ์๋์ UI ๋์์ ๋งํฌ๊ฐ์๋ ๊ฒ๊ณผ ๋์ผํ๊ฒ ๋ง๋ญ๋๋ค.
Typescript์์ ์ด๊ฒ์ ์๋ํ๋ ๋ค๋ฅธ ์ฌ๋๋ค์ ์ํด :
ListItem ๊ตฌ์ฑ ์์ ์์ฑ์ ์ฌ์ฉํ์ฌ
import { NavLink } from "react-router-dom";
<List>
<ListItem button={true} {...{ component: NavLink, to: "/Somewhere" }}>
<ListItemIcon>
<ShoppingCartIcon />
</ListItemIcon>
<ListItemText primary="Orders" />
</ListItem>
</List>
๋๋ ์๋ฅผ ๋ค์ด ListItem์์ onClick ํจ์๋ฅผ ์ฌ์ฉํ๋ ค๋ ๊ฒฝ์ฐ :
<List>
<ListItem button={true} {...{ onClick: () => alert("foo") }}>
<ListItemIcon>
<DashboardIcon />
</ListItemIcon>
<ListItemText primary="Dashboard" />
</ListItem>
</List>
```
์ด๊ฒ์ ๋๋ฅผ ์ํด ์ผํ์ต๋๋ค.
const NavLinkMui = React.forwardRef((props, ref) => (
<NavLink {...props} activeClassName="Mui-selected" ref={ref} />
))
<ListItem button component={NavLinkMui} to={to}>{text}</ListItem>
MUI v4 ๋ง์ด๊ทธ๋ ์ด์ (4.3.1์ ์์) ์ดํ ๊ฐ์ฅ ๋ง์ด ๋ํ ํ ๋ต๋ณ์ "ํจ์ ๊ตฌ์ฑ ์์์ ์ฐธ์กฐ๋ฅผ ์ ๊ณต ํ ์ ์์ต๋๋ค.์ด ์ฐธ์กฐ์ ์ก์ธ์คํ๋ ค๋ ์๋๊ฐ ์คํจํฉ๋๋ค. React.forwardRef ()๋ฅผ ์ฌ์ฉํ๋ ค๊ณ ํ์ต๋๊น?"๋ก ์ธํด ์คํจํ์ต๋๋ค.
NavLink
๊ตฌ์ฑ ์์๋ฅผ ๋ค์๊ณผ ๊ฐ์ ๋ํผ๋ก ๊ต์ฒดํ์ต๋๋ค.import React, { Component } from "react" import { NavLink } from "react-router-dom" /** * React Router Nav Link wrapper to forward the ref to fix "Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?" * * From https://material-ui.com/guides/composition/#caveat-with-refs */ class NavLinkMui extends Component { render() { const { forwardedRef, ...props } = this.props return <NavLink {...props} ref={forwardedRef} /> } } export default NavLinkMui
์ฉ๋ฒ:
<ListItem button component={NavLinkMui} to='/' > <ListItemIcon> <SlideshowIcon /> </ListItemIcon> </ListItem>
๊ฐ์ฌํฉ๋๋ค @HugoGresse ์ด๊ฒ์ ์๋ฒฝํ๊ฒ ์ ์๋ํฉ๋๋ค :-)
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ด๋ https://material-ui.com/components/buttons/#third -party-routing-library ๋ฌธ์์์ ๋ค๋ฃน๋๋ค.