【问题标题】:MUI TextField component with focus and active cursor具有焦点和活动光标的 MUI TextField 组件
【发布时间】:2022-11-10 04:43:26
【问题描述】:

我将 MUI TextField 组件用作单个输入表单。当组件加载时,它有焦点,但没有光标开始输入。用户仍然必须单击输入以调出光标并开始输入。或者,用户可以点击tab键,光标出现。但是键入其他任何内容都不会激活光标或在 TextField 中注册任何字符。我尝试使用 autoFocus 属性,以及:answer 中的 inputRef={input => input && input.focus()}。

这是表格:

<FormControl variant="standard" fullWidth>
  <TextField 
    id="new-comment" 
    label="Comment"
    multiline
    minRows={3}
    maxRows={5}
    focused={true}
    value={comment} 
    onChange={updateCommentValue}
    onKeyDown={keyPress}
  />
  <Button onClick={saveComment}>
    Comment
  </Button>
</FormControl>

这是一张没有光标的焦点图片:

以及点击后用光标对焦的图片:

【问题讨论】:

    标签: javascript reactjs material-ui focus textfield


    【解决方案1】:

    添加autoFocus 解决了这个问题:

    <FormControl variant="standard" fullWidth>
      <TextField 
        id="new-comment" 
        label="Comment"
        multiline
        minRows={3}
        maxRows={5}
        focused
        autoFocus
        value={comment} 
        onChange={updateCommentValue}
        onKeyDown={keyPress}
      />
      <Button onClick={saveComment}>
        Comment
      </Button>
    </FormControl>
    

    见StackBlitz

    【讨论】:

      猜你喜欢
      • 2019-06-06
      • 2017-03-31
      • 1970-01-01
      • 2023-01-13
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 2019-07-27
      • 2014-08-29
      相关资源
      最近更新 更多