【问题标题】:How to access onClick action in Material UI Link through keyboard Tab key without using component="button" or href when I use tabIndex in it当我在其中使用 tabIndex 时,如何通过键盘 Tab 键访问 Material UI Link 中的 onClick 操作而不使用 component="button" 或 href
【发布时间】:2021-06-06 00:31:05
【问题描述】:

在我使用 tabIndex 通过键盘 tab 键获得 Material UI Link 的焦点后,当我按下 Enter 按钮时,单击操作不起作用。我不明白为什么。谁能帮我这个?谢谢。

沙盒链接:https://codesandbox.io/s/material-demo-forked-4evbh?file=/demo.tsx

代码:

/* eslint-disable jsx-a11y/anchor-is-valid */
 import React from "react";
 import { makeStyles, createStyles, Theme } from "@material-ui/core/styles";
 import Link from "@material-ui/core/Link";
 import Typography from "@material-ui/core/Typography";

 const useStyles = makeStyles((theme: Theme) =>
 createStyles({
 root: {
  "& > * + *": {
    marginLeft: theme.spacing(2)
  }
}
})
);

export default function Links() {
const classes = useStyles();

const [state, setState] = React.useState(true);

const handleClick = () => {
setState(!state);
};

return (
 <Typography className={classes.root}>
 <Link onClick={handleClick} tabIndex={0}>
    Link
 </Link>
 <Typography>Click on above link to display hidden content..</Typography>
    {state ? "" : "focus on Link using Tab key?"}
 </Typography>
 );
 }

【问题讨论】:

    标签: material-ui


    【解决方案1】:

    我使用onKeyPress属性通过键盘tab键实现点击动作,而不使用component="button"href,如下所示。

    function goToDetails(event, id) 
    {
        if(event.key === 'Enter'){
            event.preventDefault();
        history.push(`/`);
        }
        else {
            event.preventDefault();
            history.push(`/`);
        }
    }
    <Link tabIndex={0} onKeyPress={(event) => goToDetails(event, row.id)}  onClick={(event) => goToDetails(event, row.id)}>
     Go to details
    </Link>
    

    【讨论】:

      【解决方案2】:

      component 属性设置为button,如下所示:

      /* eslint-disable jsx-a11y/anchor-is-valid */
      import React from "react";
      import { makeStyles, createStyles, Theme } from "@material-ui/core/styles";
      import Link from "@material-ui/core/Link";
      import Typography from "@material-ui/core/Typography";
      
      const useStyles = makeStyles((theme: Theme) =>
        createStyles({
          root: {
            "& > * + *": {
              marginLeft: theme.spacing(2)
            }
          }
        })
      );
      
      export default function Links() {
        const classes = useStyles();
      
        const [state, setState] = React.useState(true);
      
        const handleClick = () => {
          setState(!state);
        };
      
        return (
          <Typography className={classes.root}>
            <Link onClick={handleClick} tabIndex={0} component="button">
              Link
            </Link>
            <Typography>Click on above link to display hidden content..</Typography>
            {state ? "" : "focus on Link using Tab key?"}
          </Typography>
        );
      }
      

      【讨论】:

      • 我正在使用 NVDA 屏幕阅读器。当我将 component="button" 放在链接中时,链接在屏幕阅读器中显示和阅读为按钮。如何解决这个问题。谢谢
      猜你喜欢
      • 2021-04-18
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      • 2023-01-27
      • 2020-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多