-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Add a11y section to Tabs (#20965)
- Loading branch information
Showing
3 changed files
with
228 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
import AppBar from '@material-ui/core/AppBar'; | ||
import Tabs from '@material-ui/core/Tabs'; | ||
import Tab from '@material-ui/core/Tab'; | ||
import Typography from '@material-ui/core/Typography'; | ||
import Box from '@material-ui/core/Box'; | ||
|
||
function TabPanel(props) { | ||
const { children, value, index, ...other } = props; | ||
|
||
return ( | ||
<div | ||
role="tabpanel" | ||
hidden={value !== index} | ||
id={`simple-tabpanel-${index}`} | ||
aria-labelledby={`simple-tab-${index}`} | ||
{...other} | ||
> | ||
{value === index && ( | ||
<Box p={3}> | ||
<Typography>{children}</Typography> | ||
</Box> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
TabPanel.propTypes = { | ||
children: PropTypes.node, | ||
index: PropTypes.number.isRequired, | ||
value: PropTypes.number.isRequired, | ||
}; | ||
|
||
function DemoTabs(props) { | ||
const { labelId, onChange, selectionFollowsFocus, value } = props; | ||
|
||
return ( | ||
<AppBar position="static"> | ||
<Tabs | ||
aria-labelledby={labelId} | ||
onChange={onChange} | ||
selectionFollowsFocus={selectionFollowsFocus} | ||
value={value} | ||
> | ||
<Tab label="Item One" aria-controls="simple-tabpanel-0" id="simple-tab-0" /> | ||
<Tab label="Item Two" aria-controls="simple-tabpanel-1" id="simple-tab-1" /> | ||
<Tab label="Item Three" aria-controls="simple-tabpanel-2" id="simple-tab-2" /> | ||
</Tabs> | ||
</AppBar> | ||
); | ||
} | ||
|
||
DemoTabs.propTypes = { | ||
labelId: PropTypes.string.isRequired, | ||
onChange: PropTypes.func.isRequired, | ||
selectionFollowsFocus: PropTypes.bool, | ||
value: PropTypes.number.isRequired, | ||
}; | ||
|
||
const useStyles = makeStyles({ | ||
root: { | ||
flexGrow: 1, | ||
}, | ||
}); | ||
|
||
export default function AccessibleTabs() { | ||
const classes = useStyles(); | ||
|
||
const [value, setValue] = React.useState(0); | ||
const handleChange = (event, newValue) => { | ||
setValue(newValue); | ||
}; | ||
|
||
return ( | ||
<div className={classes.root}> | ||
<Typography id="demo-a11y-tabs-automatic-label"> | ||
Tabs where selection follows focus | ||
</Typography> | ||
<DemoTabs | ||
labelId="demo-a11y-tabs-automatic-label" | ||
selectionFollowsFocus | ||
onChange={handleChange} | ||
value={value} | ||
/> | ||
<Typography id="demo-a11y-tabs-manual-label"> | ||
Tabs where each tab needs to be selected manually | ||
</Typography> | ||
<DemoTabs labelId="demo-a11y-tabs-manual-label" onChange={handleChange} value={value} /> | ||
<TabPanel value={value} index={0}> | ||
Item One | ||
</TabPanel> | ||
<TabPanel value={value} index={1}> | ||
Item Two | ||
</TabPanel> | ||
<TabPanel value={value} index={2}> | ||
Item Three | ||
</TabPanel> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import React from 'react'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
import AppBar from '@material-ui/core/AppBar'; | ||
import Tabs from '@material-ui/core/Tabs'; | ||
import Tab from '@material-ui/core/Tab'; | ||
import Typography from '@material-ui/core/Typography'; | ||
import Box from '@material-ui/core/Box'; | ||
|
||
interface TabPanelProps { | ||
children?: React.ReactNode; | ||
index: number; | ||
value: number; | ||
} | ||
|
||
function TabPanel(props: TabPanelProps) { | ||
const { children, value, index, ...other } = props; | ||
|
||
return ( | ||
<div | ||
role="tabpanel" | ||
hidden={value !== index} | ||
id={`simple-tabpanel-${index}`} | ||
aria-labelledby={`simple-tab-${index}`} | ||
{...other} | ||
> | ||
{value === index && ( | ||
<Box p={3}> | ||
<Typography>{children}</Typography> | ||
</Box> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
interface DemoTabsProps { | ||
labelId: string; | ||
onChange: (event: unknown, value: number) => void; | ||
selectionFollowsFocus?: boolean; | ||
value: number; | ||
} | ||
function DemoTabs(props: DemoTabsProps) { | ||
const { labelId, onChange, selectionFollowsFocus, value } = props; | ||
|
||
return ( | ||
<AppBar position="static"> | ||
<Tabs | ||
aria-labelledby={labelId} | ||
onChange={onChange} | ||
selectionFollowsFocus={selectionFollowsFocus} | ||
value={value} | ||
> | ||
<Tab label="Item One" aria-controls="simple-tabpanel-0" id="simple-tab-0" /> | ||
<Tab label="Item Two" aria-controls="simple-tabpanel-1" id="simple-tab-1" /> | ||
<Tab label="Item Three" aria-controls="simple-tabpanel-2" id="simple-tab-2" /> | ||
</Tabs> | ||
</AppBar> | ||
); | ||
} | ||
|
||
const useStyles = makeStyles({ | ||
root: { | ||
flexGrow: 1, | ||
}, | ||
}); | ||
|
||
export default function AccessibleTabs() { | ||
const classes = useStyles(); | ||
|
||
const [value, setValue] = React.useState(0); | ||
const handleChange = (event: unknown, newValue: number) => { | ||
setValue(newValue); | ||
}; | ||
|
||
return ( | ||
<div className={classes.root}> | ||
<Typography id="demo-a11y-tabs-automatic-label"> | ||
Tabs where selection follows focus | ||
</Typography> | ||
<DemoTabs | ||
labelId="demo-a11y-tabs-automatic-label" | ||
selectionFollowsFocus | ||
onChange={handleChange} | ||
value={value} | ||
/> | ||
<Typography id="demo-a11y-tabs-manual-label"> | ||
Tabs where each tab needs to be selected manually | ||
</Typography> | ||
<DemoTabs labelId="demo-a11y-tabs-manual-label" onChange={handleChange} value={value} /> | ||
<TabPanel value={value} index={0}> | ||
Item One | ||
</TabPanel> | ||
<TabPanel value={value} index={1}> | ||
Item Two | ||
</TabPanel> | ||
<TabPanel value={value} index={2}> | ||
Item Three | ||
</TabPanel> | ||
</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