javascript - 清除无状态 React 组件中的输入

标签 javascript reactjs semantic-ui

我想在 Input 组件中实现一个 X 图标来清除输入字段。如果我控制状态,我可以轻松做到。但是无状态组件真的有可能吗? 我使用 react-semantic-ui,它们的有状态组件具有自动控制状态。

所以我想创建一个可以像这样使用的输入:

//Controlled
class App extends React.Component {
  state = {
    value:''
  }

  onChange = (event, props) => {
    this.setState({value: props.value});
  }

  onClearInput = () => {
    this.setState({value: ''});
  }
  
  render() {
    return (
        <MyInput
          clearable
          value={this.state.value}
          onChange={this.onChange}
          onClearInput={this.onClearInput}
        />
      )
  }
}

或者

// Uncontrolled
class App extends React.Component {
  onChange = (event, props) => {
    doSomething(props.value);
  }

  render() {
    return (
        <MyInput
          clearable
          onChange={this.onChange}
        />
      )
  }
}

在第二个示例中,clearable 功能将不起作用,因为我们没有控制该值。

MyInput 可以这样实现:

import React from 'react';
import { Input } from 'semantic-ui-react';
import ClearIcon from './ClearIcon';

function MyInput(props) {
  const prepareProps = {...props};
  if (props.clearable) {
    prepareProps.icon=<ClearIcon onClick={props.onClearInput} />;
    delete prepareProps.clearable;
  }
  delete prepareProps.onClearInput;
  return (
    <div className="my-input">
      <Input {...prepareProps} />
    </div>
  );
}
...etc.

我的问题:

  1. clearable 功能必须以受控和非受控方式工作。

  2. clearable 功能不应需要处理程序。如果仅提供一个 Prop 并在后台处理 X 按钮的渲染和行为,那就太好了。

我看不出有什么方法可以让这项工作成功。有什么想法吗?

最佳答案

允许组件的用户通过 props 设置值并且仍然能够清除输入可以很容易地实现,例如像这样:

class MyInput extends React.Component {
    constructor(props) {
        super(props);
        this.state = {value: props.value || ''};
    }

    handleChange = event => {
        const { onChange } = this.props;
        this.setState({ value: event.currentTarget.value });
        onChange && onChange(event);
    };

    handleClear = () => {
        const { onClearInput } = this.props;
        this.setState({ value: "" });
        onClearInput && onClearInput();
    };

    render() {
        const { value } = this.state;
        const { clearable, onChange, ...inputProps } = this.props;

        const clearIcon = clearable && <ClearIcon onClick={this.handleClear} />;

        return (
            <div className="my-input">
                <Input value={value} icon={clearIcon} onChange={this.handleChange} {...inputProps} />
            </div>
        );
    }
}

您甚至可以使用 @pkuzhel 提议的 hoc 或 render props 使其更具可组合性。

this codesandbox example看看它的实际效果。

关于javascript - 清除无状态 React 组件中的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49491574/

相关文章:

javascript - 使用 jquery 设置选择表单的值

javascript - HTML5 响应式 Canvas 鼠标位置和调整大小

javascript - AngularJS ng-show(不随 bool 值改变)

Javascript - 确保字符串字段包含某些字符和数字

html - 当语义 ui 中的屏幕尺寸发生变化时,表格不完全可见

semantic-ui - 在语义用户界面中固定一行的宽度

javascript - VueJS Master Checkbox 切换数组值

reactjs - 停止重新渲染 react 功能组件(使用钩子(Hook))

javascript - "this.setState is not a function"在嵌套的 addEventListener 函数中使用 setState 时

html - 带有 Swiper 的 div 旁边的固定宽度 div 会导致奇怪的行为