3
votes

I'm using react-bootstrap lib and need to use an input component. I know how to type a simple input but have a problem with FormControl

class Search extends Component<Props, { searchInput: string }> {

      static defaultProps = {}

      state = {
        searchInput: ""
      }

     // This one works for input 
      searchInputSimple = (e: React.FormEvent<HTMLInputElement>): void => {
        const { getSearch } = this.props
        this.setState(
          { searchInput: e.currentTarget.value },
          (): void => getSearch(this.state.searchInput)
        )
      }

      // for FormControl. What can I use exept any?
      searchInput = (e: React.FormEvent<any>): void => {
        const { getSearch } = this.props
        this.setState(
          { searchInput: e.currentTarget.value },
          (): void => getSearch(this.state.searchInput)
        )
      }

      render() {
        const { searchInput } = this.state
        return (
          <>
            <InputGroup className="mb-3">
              <FormControl placeholder="Search" value={searchInput} onChange={this.searchInput} />
            </InputGroup>

            <input
              placeholder="Search"
              value={searchInput}
              onChange={this.searchInputSimple}
              className="form-control"
            />
          </>
        )
      }
    }

I tried to understand and look at FormControl.d.ts

import Feedback from './Feedback';

import { BsPrefixComponent } from './helpers';

interface FormControlProps {
  innerRef?: React.LegacyRef<this>;
  size?: 'sm' | 'lg';
  plaintext?: boolean;
  readOnly?: boolean;
  disabled?: boolean;
  value?: string;
  onChange?: React.FormEventHandler<this>;
  type?: string;
  id?: string;
  isValid?: boolean;
  isInvalid?: boolean;
}

declare class Form<
  As extends React.ReactType = 'input'
> extends BsPrefixComponent<As, FormControlProps> {
  static Feedback: typeof Feedback;
}

If I use just HTMLInputElement I'm getting this error

Type error: Type '(e: FormEvent) => void' is not assignable to type '(event: FormEvent & FormControlProps>>) => void'. Types of parameters 'e' and 'event' are incompatible. Type 'FormEvent & FormControlProps>>' is not assignable to type 'FormEvent'. Type 'Pick, HTMLInputElement>, "children" | "form" | "style" | "title" | "pattern" | "ref" | "key" | "defaultChecked" | ... 268 more ... | "width"> & BsPrefixProps<...> & FormControlProps' is missing the following properties from type 'HTMLInputElement': align, autocomplete, autofocus, dirName, and 245 more.

So how to do it and not to use any type? Should it be an alias HTMLInputElement and something more?

2

2 Answers

3
votes

You're looking for a React.ChangeEvent, rather than a React.FormEvent:

searchInput = (e: React.ChangeEvent<HTMLInputElement>): void => {
    const { value } = e.currentTarget;
    const { getSearch } = this.props;
    this.setState(state => ({
        ...state,
        searchInput: value
    }), (): void => getSearch(this.state.searchInput))
}
0
votes

Instead of any, use FormControl & FormControlProps.

searchInput = (e: React.FormEvent<FormControl & FormControlProps>): void => {
  const { getSearch } = this.props
  this.setState(
    { searchInput: e.currentTarget.value as string},
    (): void => getSearch(this.state.searchInput)
  )
}

Taken from this comment on the issue in @types/react-bootstrap.