首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TextField mui组件文本的autoFocus端

TextField mui组件文本的autoFocus端
EN

Stack Overflow用户
提问于 2022-10-24 23:53:59
回答 1查看 30关注 0票数 2

现在,autoFocus适用于输入的开头,但我想将重点放在文本的末尾。

代码语言:javascript
复制
export default function BasicTextFields() {
  const [value, setValue] = React.useState(
    "hello world. hello world. hello world"
  );

  return (
    <Box
      component="form"
      sx={{
        "& > :not(style)": { m: 1, width: "25ch" }
      }}
      noValidate
      autoComplete="off"
    >
      <TextField
        id="outlined-basic"
        variant="outlined"
        value={value}
        onChange={(e) => setValue(e.target.value)}
        multiline
        autoFocus
      />
    </Box>
  );
}

这个是可能的吗?

我试过这个链接:https://github.com/mui/material-ui/issues/12779,但这不适用于我的情况。

代码语言:javascript
复制
<TextField
    variant="outlined"
    type="text"
    id="field-comment"
    name="comment"
    label="Label"
    placeholder="Placeholder"
    onChange={(event) => setValue(event.target.value)}
    inputRef={(input) => input && input.focus()}
    onFocus={(e) =>
        e.currentTarget.setSelectionRange(
        e.currentTarget.value.length,
        e.currentTarget.value.length
    )}
    multiline
    rows={4}
    value={value}
    className={classes.sCommentTextField}
/>

我也试过这个。

代码语言:javascript
复制
<TextField
  inputRef={input => input && input.focus()}
/>

但也没用。

我有什么办法可以做到吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-25 14:12:59

这行得通!

代码语言:javascript
复制
<TextField
    variant="outlined"
    type="text"
    id="field-comment"
    name="comment"
    label="Label"
    placeholder="Placeholder"
    onChange={(event) => setValue(event.target.value)}
    inputRef={(input) => input && input.focus()}
    onFocus={(e) =>
        e.currentTarget.setSelectionRange(
        e.currentTarget.value.length,
        e.currentTarget.value.length
    )}
    multiline
    rows={4}
    value={value}
    className={classes.sCommentTextField}
/>

删除autoFocus并添加inputRefonFocus

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74187884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档