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

ziL*_*iLk 5 javascript input reactjs antd

设置样式属性正在使用。

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

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

    <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 })}
        />
Run Code Online (Sandbox Code Playgroud)

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

1.案例:

<input placeholder="this works" type="text" class="ant-input" style="border-width: 1px;">
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

2.箱跨块吸式的原因。

运行演示

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

Olu*_*ule 1

style输入组件的实现不支持inputwithsuffix或property上的属性。prefix

请参阅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}
Run Code Online (Sandbox Code Playgroud)

className您可以通过传递组件的属性来解决此问题Input

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

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

.border-lg input, input.border-lg {
  border-width: 4px;
}
Run Code Online (Sandbox Code Playgroud)

您对Inputs 的实现可能如下所示:

//...
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>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)