From cfe2a21973c9851917035df55c9722692f545d84 Mon Sep 17 00:00:00 2001 From: Patricio Zavolinsky Date: Sat, 14 Jan 2017 15:21:58 +0000 Subject: [PATCH] React: add typed target for change events --- react/index.d.ts | 21 ++++++++++++++------- react/test/index.ts | 16 ++++++++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/react/index.d.ts b/react/index.d.ts index 2a5e737c58..e59234ae6c 100644 --- a/react/index.d.ts +++ b/react/index.d.ts @@ -270,10 +270,9 @@ declare namespace React { // // Event System // ---------------------------------------------------------------------- - - interface SyntheticEvent { + interface SyntheticEventBase { bubbles: boolean; - currentTarget: EventTarget & T; + currentTarget: EventTarget & CURRENT; cancelable: boolean; defaultPrevented: boolean; eventPhase: number; @@ -284,12 +283,16 @@ declare namespace React { stopPropagation(): void; isPropagationStopped(): boolean; persist(): void; - // If you thought this should be `EventTarget & T`, see https://github.com/DefinitelyTyped/DefinitelyTyped/pull/12239 - target: EventTarget; + target: EventTarget & TARGET; timeStamp: Date; type: string; } + interface SyntheticEvent extends SyntheticEventBase { + // If you thought target should be `EventTarget & T`, + // see https://github.com/DefinitelyTyped/DefinitelyTyped/pull/12239 + } + interface ClipboardEvent extends SyntheticEvent { clipboardData: DataTransfer; } @@ -309,6 +312,9 @@ declare namespace React { interface FormEvent extends SyntheticEvent { } + interface ChangeEvent extends SyntheticEventBase { + } + interface KeyboardEvent extends SyntheticEvent { altKey: boolean; charCode: number; @@ -391,6 +397,7 @@ declare namespace React { type DragEventHandler = EventHandler>; type FocusEventHandler = EventHandler>; type FormEventHandler = EventHandler>; + type ChangeEventHandler = EventHandler>; type KeyboardEventHandler = EventHandler>; type MouseEventHandler = EventHandler>; type TouchEventHandler = EventHandler>; @@ -458,7 +465,7 @@ declare namespace React { onBlurCapture?: FocusEventHandler; // Form Events - onChange?: FormEventHandler; + onChange?: ChangeEventHandler; onChangeCapture?: FormEventHandler; onInput?: FormEventHandler; onInputCapture?: FormEventHandler; @@ -2142,7 +2149,7 @@ declare namespace React { unselectable?: boolean; } - // this list is "complete" in that it contains every SVG attribute + // this list is "complete" in that it contains every SVG attribute // that React supports, but the types can be improved. // Full list here: https://facebook.github.io/react/docs/dom-elements.html // diff --git a/react/test/index.ts b/react/test/index.ts index ab23c65532..0900926cf6 100644 --- a/react/test/index.ts +++ b/react/test/index.ts @@ -672,3 +672,19 @@ class ConstructorSpreadArgsPureComponent extends React.PureComponent<{}, {}> { super(...args); } } + +// +// The SyntheticEvent.target.value should be accessible for onChange +// -------------------------------------------------------------------------- +class SyntheticEventTargetValue extends React.Component<{}, { value: string }> { + constructor(props:{}) { + super(props); + this.state = { value: 'a' }; + } + render() { + return React.DOM.textarea({ + value: this.state.value, + onChange: e => this.setState({value: e.target.value}) + }); + } +}