【问题标题】:How to remove the second x button, the left one inside a TextField inside a Autocomplete?如何删除第二个 x 按钮,即 Autocomplete 内 TextField 内的左侧按钮?
【发布时间】:2021-08-11 02:41:56
【问题描述】:

我有一个搜索栏,它使用材料 ui 中的自动完成功能来提供建议,在其中我有一个文本字段,我将文本作为输入。

唯一的问题是,当我在 TextField 中键入任何内容时,我可以看到 2 个清除按钮(x 按钮),一个在加载屏幕的左侧,一个在右侧,当加载屏幕消失时,我得到 2 个清除按钮紧挨着。我想删除左边的那个,因为它看起来很糟糕,我不知道它为什么在那里。

Search.jsx:

<div className={searchClasses.search}>
      <Autocomplete
        options={isEmpty ? [] : suggestionsList}
        freeSolo
        style={{width: "100%"}}
        getOptionLabel={(option) => option.title}
        renderInput={(params) => (
          <TextField
            {...params}
            variant="outlined"
            margin="normal"
            required
            fullWidth
            autoFocus
            loading={loading}
            style={{margin: 0}}
            classes={{ notchedOutline: classes.input }}
            onChange={handleOnChange}
            onKeyDown={e => handleKeyDown(e)}
            placeholder="Search..."
            type="search"
            InputProps={{
              ...params.InputProps,
              startAdornment: (
                <InputAdornment position="start">
                  <SearchIcon />
                </InputAdornment>
              ),
              endAdornment: (
                <React.Fragment>
                  {loading ? <CircularProgress color="inherit" size={20} /> : null}
                  {params.InputProps.endAdornment}
                </React.Fragment>
              ),
              classes: { notchedOutline: classes.noBorder }
            }}
          />
        )}
        renderOption={(option, { inputValue }) => {
          const matches = match(option.title, inputValue);
          const parts = parse(option.title, matches);

          return (
            <div>
              {parts.map((part, index) => (
                <span
                  key={index}
                  style={{ fontWeight: part.highlight ? 700 : 400 }}
                >
                  {part.text}
                </span>
              ))}
            </div>
          );
        }}
      />
</div>

【问题讨论】:

    标签: reactjs autocomplete material-ui next.js dropdown


    【解决方案1】:

    解决方案

    创建一个新的 CSS 样式表(比如说styles.css)并添加以下代码:

    input[type="search"]::-webkit-search-cancel-button {
      -webkit-appearance: none;
    }
    

    接下来,在 Search.jsx 的开头导入此样式表:

    import "./styles.css";
    
    /* Your code here ... */
    

    说明

    左侧“X”取消按钮之所以存在,是因为基于Webkit 的浏览器(如Chrome 和Safari)会自动将取消按钮添加到所有&lt;input type="search"&gt; 元素中。由于您的TextField 元素具有type="search" 属性,它会将&lt;input type="search"&gt; 呈现到屏幕上,因此您的浏览器会自动添加“X”按钮。

    可以使用::-webkit-search-cancel-button 伪元素选择器选择默认的“X”按钮。在我们的style.css 中,我们选择所有&lt;input type="search"&gt; 元素中的所有默认“X”按钮,并使用-webkit-appearance: none; 隐藏它们

    【讨论】:

    • 谢谢!我已经使用您的解释删除了 x,因为我没有将 type["search"] 用于任何其他样式,因此我删除了它并且它有效,因为我无法在 Next js 中导入我的 css 文件。
    猜你喜欢
    • 2020-08-29
    • 1970-01-01
    • 2023-03-22
    • 2011-12-04
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多