javascript - Autosuggest renderInputComponent - inputProps 类型的属性 'onChange' 不兼容

标签 javascript reactjs typescript styled-components

使用 react-autosuggest 时出现此 Typescript 错误使用自定义输入样式组件。

Types of property 'onChange' are incompatible. Type '(event: FormEvent, params: ChangeEvent) => void' is not assignable to type '(event: ChangeEvent) => void'.



代码:
(请注意,它不完整,只是相关部分)

// styled.ts
export const Input = styled.input`
  // styles
`;

// index.tsx
function renderInput(
  inputProps: Autosuggest.InputProps<SuggestionSearch>,
  placeholder: string
) {
  // --> error here
  return <Input {...inputProps} placeholder={placeholder} />;
}

const MyComponent = () => {
  const autosuggestRef = React.useRef<
    Autosuggest<SuggestionSearch, SuggestionSearch>
  >(null);

  const onChange = (event: React.FormEvent, { newValue }: ChangeEvent) =>
    setValue(newValue);

  const inputProps = {
    value,
    onChange,
    onKeyPress: onEnterPress,
  };

  return (
    <Autosuggest
      ref={autosuggestRef}
      renderInputComponent={props => renderInput(props, placeholder)}
      inputProps={inputProps}
      // other props
    />
  )
}

不确定如何解决此问题,因为 Autosuggest onChange 函数会覆盖基本输入 onChange Prop 。

最佳答案

我自己花了整晚的时间来解决这个问题。看来这是一个错误。这是 InputProps 的签名:

    interface InputProps<TSuggestion>
        extends Omit<React.InputHTMLAttributes<any>, 'onChange' | 'onBlur'> {
        onChange(event: React.FormEvent<any>, params: ChangeEvent): void;
        onBlur?(event: React.FocusEvent<any>, params?: BlurEvent<TSuggestion>): void;
        value: string;
    }

看来我们需要创建自己的 Input采用这种类型 onChange 的组件签名或将此 onChange 包装在标准 onChange 中。

所以,这是我的方法:

const renderInputComponent = (inputProps: InputProps<TSuggestion>): React.ReactNode => {
    const onChangeHandler = (event: React.ChangeEvent<HTMLInputElement>): void => {
      inputProps.onChange(event, { newValue: event.target.value, method: 'type' });
    };
   return <Input {...inputProps} onChange={onChangeHandler} />;
}

两个潜在的错误:
  • 注意 event返回的是 React.ChangeEvent而不是 React.FormEvent .微小的差异,但如果您实际使用它并且对事件足够特别,它可能会在运行时出现问题。
  • 返回的params: ChangeEvent只占type事件方法。如果你想要其他的,比如click、esc等等……你必须自己补充(例如通过onKeyUp,然后调用`onChange(...{method: 'esc'})

  • 根据文档:

    Note: When using renderInputComponent, you still need to specify the usual inputProps. Autosuggest will merge the inputProps that you provide with other props that are needed for accessibility (e.g. 'aria-activedescendant'), and will pass the merged inputProps to renderInputComponent.



    你不必这样做:
    renderInputComponent={props => renderInput(props, placeholder)}
    

    只需通过 placeholder直接进入您的inputProps这将在 renderInputComponent 中直接返回给您.
      const inputProps = {
        value,
        onChange,
        onKeyPress: onEnterPress,
        placeholder: 'something!`
      };
    

    关于javascript - Autosuggest renderInputComponent - inputProps 类型的属性 'onChange' 不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61785523/

    相关文章:

    javascript - *安全地将 HTML5 应用程序连接到服务器上的数据库

    javascript - 是否有一个函数可以读取 ERC20 供应并在输出中给出不带小数的数字?

    javascript - 在哪里编码以覆盖 backbone.sync

    angularjs - 因为我现在正在使用 Laravel,所以我应该学习 Vuejs/Angular/React 吗?

    javascript - React中的JS渲染

    javascript - React中如何判断当前路由是否活跃

    javascript - 使用 Typescript 进行 react : Property 'push' does not exist on type 'History'

    javascript - 为什么对象不通过原型(prototype)链继承方法?

    angular - 在二维数组中使用@viewchild

    javascript - Angular 2 如何将单选按钮的检查属性绑定(bind)到组件的 bool 属性?