【问题标题】:Search bar in React TS not working as expectedReact TS 中的搜索栏未按预期工作
【发布时间】:2018-11-04 16:09:58
【问题描述】:

我使用这个用 ReactJS 编写的示例 sn-p 来尝试在我的网站中创建一个搜索框,它是用 React TS 编写的

https://codemyui.com/input-highlight-seen-tripadvisor/

但是当我尝试转换它时,我遇到了以下问题

1) 输入是只读的,即我不能输入它

2) 我不确定如何处理原始 CSS 中的这部分样式,因为当我将其粘贴到我的 CSS 中时它无法编译

&:focus {
    + .input-highlight {
      border-top: 3px solid #fbc91b;
    }
  }

谁能指出我在转换为 TS 时做错了什么?

我的 React 控件

import * as React from 'react';
import './App.css';

interface ISearchBarState {
    inputValue : string
  }

class SearchBar extends React.Component<{}, ISearchBarState> {

    constructor(props: any) {
        super(props);

        this.state = {
          inputValue: ''
        };

        this.onInputChange = this.onInputChange.bind(this);
      }

      public render() {
        const { inputValue } = this.state;

       return (<div className='input-wrapper'>
           <input
             placeholder='Search...'
             value={inputValue}
             spellCheck={false}
             />
           <span className='input-highlight'>
           { inputValue.replace(/ /g, "\u00a0") }
         </span>  
         </div>);          
    }

    private onInputChange(e: any) {
        const { value } = e.target;

        this.setState({
          inputValue: value
        });
      }


}

export default SearchBar;

和我的 CSS

.input-wrapper {
  position: relative;
  width: 350px;
  margin-left: 5px;
}

.input-highlight {
  font-size: 16;
  user-select: none;
  line-height: 20;
  border-top: 1px solid white;
  position: absolute;
  left: 0;
  bottom: 0;
  color: #999999;
  max-width: 100%;
  height: 0;
  color: transparent;
  font-family: Roboto Slab, sans-serif;
  overflow: hidden;
  border-top: 3px solid #fbc91b;
}

input {
  height: 30px;
  width: 100%;
  min-width: 100%;
  padding: 0;
  border-radius: 0;
  line-height: 2;
  background-color: transparent;
  color: #999999;
  font-size: 16;
  border: none;
  outline: none;
  border-bottom: 1px solid #666666;
  font-family: Roboto Slab, sans-serif;
}

【问题讨论】:

    标签: javascript css reactjs typescript


    【解决方案1】:

    看起来有点像有问题的css

    &:focus {
    + .input-highlight {
        border-top: 3px solid #fbc91b;
      }
    }
    

    实际上是scss,你需要先把那个位编译成css或者手动编译成css。这里,下面我帮你手动改成css给你

    input:focus .input-highlight{
       border-top: 3px solid #fbc91b;
    }
    

    您可以将此 css sn-p 添加到您的 css 文件中,将其放在输入 css 块下方。

    【讨论】:

    • 好的,非常感谢。但是,我仍然遇到第一个问题,控件不接受输入并且看起来是只读的
    • 请问您是如何将 tsx 文件添加到您的页面的?它是一个反应页面吗?您是否已将 tsx 代码转换为 js 包?您是否查看过 create-react-app 以了解如何启动 React Web 应用程序?
    • 是的,我使用了带有 TS 选项脚本的 create-react-app 来创建 TS 项目。我在文档中读到了一些内容,如果您设置控件的值,那么它将变为只读 - 但我不太明白 - 因为您肯定希望能够将值绑定到状态并允许用户输入什么?
    • 啊,是的,我删除了输入控件中的行 value={inputValue} 现在它可以工作了。令人惊讶的是,原来的 sn-p online 中出现了这种情况,而且这似乎有效。也许是 TS 的事?
    • 是的,输入中的onChange 处理程序应该已经处理了输入值的状态更新。 value={inputValue} 理论上无论是添加还是删除都应该没有影响。如果删除该属性可以解决问题,请随时从您的代码中删除它!
    猜你喜欢
    • 2017-05-12
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    相关资源
    最近更新 更多