Instead of expanding Control with CustomControl you should wrap it.
What you really want to do is change the render() Control method and add a custom onKeyPress . The problem with the Control extension is that you can only override the Control rendering method and not make changes to its parts.
However, if you wrap the Control component with your own component, you can influence it the way you want.
If you look at the definition for ControlProps<T> , you will see the following:
export interface ControlProps<T> extends React.HTMLProps<T> {
Since it is extended by React.HTMLProps , it supports the onKeyPress method as a prop.
If we combine all this information together, you can do something like:
import * as React from "react"; import { Control, ControlProps } from "react-redux-form"; export class CustomControl<T> extends React.Component<ControlProps<T>> { onKeyPress(e: any) { if (e.key === "Enter") { e.preventDefault(); } } render() { return <Control {...this.props} onKeyPress={e => this.onKeyPress(e)} />; } }
Please note that the above implementation completely cancels any onKeyPress passed as a support to CustomControl in favor of your custom onKeyPress .
If you also wanted to call any onKeyPress that is passed as a prop, you could add the bottom of your onKeyPress custom function:
// After custom logic call any onKeyPress passed to this this.props.onKeyPress && this.props.onKeyPress(e);
casieber
source share