Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declaration for FormControlProps.onChange is incorrect #3583

Closed
AndriySvyryd opened this issue Apr 1, 2019 · 4 comments
Closed

Declaration for FormControlProps.onChange is incorrect #3583

AndriySvyryd opened this issue Apr 1, 2019 · 4 comments

Comments

@AndriySvyryd
Copy link

AndriySvyryd commented Apr 1, 2019

@observer
class InputForm extends React.Component<IInputFormProps, {}> {
    @observable outgoingMessage: string = '';

    @action.bound
    handleChange(event: ReplaceProps<"input", BsPrefixProps<"input"> & FormControlProps>) {
        this.outgoingMessage = event.target.value;
    }

    @action.bound
    handleSubmit(event: React.FormEvent<HTMLFormElement>) {
        this.props.sendMessage(this.outgoingMessage);
        this.outgoingMessage = '';
        event.preventDefault();
    }

    render() {
        return <Form inline noValidate onSubmit={this.handleSubmit} autoComplete="off">
            <Form.Control
                type="text"
                name="outgoingMessage"
                value={this.outgoingMessage}
                onChange={this.handleChange}
            />
            <Button type="submit">Send</Button>
        </Form>;
    }
}

interface IInputFormProps {
    sendMessage: (outgoingMessage: string) => void;
}

TS2339: Property 'target' does not exist on type 'ReplaceProps<"input", BsPrefixProps<"input"> & FormControlProps>'.

@mattvonb
Copy link

mattvonb commented Apr 10, 2019

I'm facing this problem as well. (I think.)

With react-bootstrap 0.32.4, I would define the onChange handler with type:

(event: React.FormEvent<FormControl & HTMLInputElement>) => void

If I try this with react-boostrap 1.0.0-beta.6, I get the following compiler error:

Type '(event: FormEvent<Form<"input"> & HTMLInputElement>) => void' is not assignable to type '(event: FormEvent<ReplaceProps<"input", BsPrefixProps<"input"> & FormControlProps>>) => void'.
  Types of parameters 'event' and 'event' are incompatible.
    Type 'FormEvent<ReplaceProps<"input", BsPrefixProps<"input"> & FormControlProps>>' is not assignable to type 'FormEvent<Form<"input"> & HTMLInputElement>'.
      Type 'ReplaceProps<"input", BsPrefixProps<"input"> & FormControlProps>' is not assignable to type 'Form<"input"> & HTMLInputElement'.
      Type 'Pick<DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>, "ref" | "key" | "autoComplete" | "name" | "defaultChecked" | "defaultValue" | "suppressContentEditableWarning" | ... 271 more ... | "width"> & BsPrefixProps<...> & FormControlProps' is missing the following properties from type 'Form<"input">': context, setState, forceUpdate, render, and 3 more.ts(2322)
FormControl.d.ts(14, 3): The expected type comes from property 'onChange' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<Form<"input">> & Readonly<ReplaceProps<"input", BsPrefixProps<"input"> & FormControlProps>> & Readonly<{ children?: ReactNode; }>'

I can get rid of this error by modifying the type of function to be just:

(event: React.FormEvent<FormControl>) => void

but then I can't access any of the input element fields on event.currentTarget. For example:

export interface Props {
  planet?: string;
}

interface State {
  planet: string;
}

export class Hello extends React.Component<Props, State> {
  constructor(props: Props) {
    super(props);
    this.state = { planet: props.planet || "" }
  }

  public render() {
    return (
     <form>
       <FormGroup>
         <FormControl name="planet" type="text" value={this.state.planet}
                      onChange={this.handleChange} />
       </FormGroup>
     </form>
    );
  }

  private handleChange = (event: React.FormEvent<FormControl>) => {
    const state = {...this.state};
    const name = event.currentTarget.name;
    state[name] = event.currentTarget.value;
    this.setState(state);
  };
}

results in the following compiler error: Property 'name' does not exist on type 'EventTarget & Form<"input">'.ts(2339).

Interestingly, if instead of a FormControl, I use a Form.Check, then my code works as it did with react-bootstrap 0.32.4:

export interface Props {
  enabled?: boolean;
}

interface State {
  enabled: boolean;
}

export class HelloCheck extends React.Component<Props, State> {
  constructor(props: Props) {
    super(props);
    this.state = { enabled: this.props.enabled || true };
  }

  public render() {
    return (
     <form>
       <FormGroup>
         <Form.Check type="checkbox"
                     name="enabled"
                     label="Enabled"
                     checked={this.state.enabled}
                     onChange={this.handleChange} />
        </FormGroup>
     </form>
    );
  }

  private handleChange = (event: React.ChangeEvent<FormControl & HTMLInputElement>) => {
    const state = {...this.state};
    const name = event.currentTarget.name;
    state[name] = event.currentTarget.checked;
    this.setState(state);
  };
}

@afeinman
Copy link

I'm also encountering this. Even lovelier would be if there were a simple type alias for React.ChangeEvent<FormControl & HTMLInputElement> as that's a bit cumbersome to remember and type each time.

craigpg added a commit to craigpg/react-bootstrap that referenced this issue Sep 9, 2019
craigpg added a commit to craigpg/react-bootstrap that referenced this issue Sep 9, 2019
mlogan pushed a commit to mlogan/react-bootstrap that referenced this issue Sep 12, 2019
@moiri
Copy link

moiri commented Oct 3, 2019

I also have this problem. For now, to avoid the error I cast the event type:

onChange: (event:React.ChangeEvent<FormControl>) => {
     const castEvent = event as unknown as React.ChangeEvent<HTMLInputElement>;
     setValue(castEvent.target.value);
}

jquense pushed a commit that referenced this issue Oct 18, 2019
@kosmiq
Copy link
Collaborator

kosmiq commented Mar 11, 2020

@jquense can this be closed after the commit?

@jquense jquense closed this as completed Mar 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants