javascript - 样式属性不适用于带后缀或前缀的输入

标签 javascript reactjs input antd

设置 Style 属性可以解决这个问题。

 <Input
      style={{ borderWidth: this.state.focused ? "4px" : "1px" }}
      placeholder="this works"
      onMouseEnter={() => this.setState({ focused: true })}
      onMouseLeave={() => this.setState({ focused: false })}
    />

但是,当我使用 Input 的后缀或前缀属性时组件,它不起作用。

    <Input
          style={{ borderWidth: this.state.focused ? "4px" : "1px" }}
          placeholder="not this"
          /*only difference is this suffix line*/
          suffix={<Icon type="save" />}
          onMouseEnter={() => this.setState({ focused: true })}
          onMouseLeave={() => this.setState({ focused: false })}
        />

当我在浏览器上查看源代码时,它给了我原因。

1.案例:

<input placeholder="this works" type="text" class="ant-input" style="border-width: 1px;">

2.案例:

<span class="ant-input-affix-wrapper" style="border-width: 1px;"><input placeholder="not this" type="text" class="ant-input"><span class="ant-input-suffix"><i class="anticon anticon-save"></i></span></span>

2.case span block 吸收样式的原因。

running demo

那么如何在具有后缀/前缀属性的输入上设置我的样式。

最佳答案

input 上带有 suffixprefix 属性的

style 属性在 Input 组件的实现中不受支持.

参见 https://github.com/ant-design/ant-design/blob/3.10.7/components/input/Input.tsx#L170

{prefix}
{React.cloneElement(children, { style: null, className: this.getInputClassName() })}
{suffix}

您可以通过为 Input 组件传递 className 属性来解决这个问题。

假设您在样式表中有这些 CSS 定义,

.border-sm input, input.border-sm {
  border-width: 1px;
}

.border-lg input, input.border-lg {
  border-width: 4px;
}

Input 的实现可能如下所示:

//...
import "./style.css"

class ErrorExample extends React.Component {
  //...

  render() {
    return (
      <div>
        <h1>Enter mouse into one of textboxes</h1>
        <Input
          className={this.state.focused ? "border-lg" : "border-sm"}
          //...
        />
        <Input
          className={this.state.focused ? "border-lg" : "border-sm"}
          suffix={<Icon type="save" />}
          //...
        />
      </div>
    );
  }
}

关于javascript - 样式属性不适用于带后缀或前缀的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53337791/

相关文章:

javascript - 需要通过 jQuery 定位元素,请 <3

reactjs - 如何处理 nextJS 中 useRouter() 的异步问题

reactjs - 如何检查哪些附加 Prop 被传递到未定义的 react 组件中?

javascript - React 输入范围更新状态缓慢

javascript - 注册后重定向用户的正确方法

javascript - 可以更改 Highcharts 钻取内的高度吗?

java - 使用 LoadData 时在 WebView 中运行 Javascript

javascript - 最好将一些大型或多个小型商店与 React 和 Reflux 一起使用

php - 在带有搜索字段的查询中包含 <SELECT>

html - 窗体不响应窗口大小调整