Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
Switch to using a single generic change handler
Browse files Browse the repository at this point in the history
  • Loading branch information
mezzode committed Jan 18, 2019
1 parent 7e1cccf commit 545ec7a
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import classNames from 'classnames';
import 'milligram';
import 'normalize.css';
import React, { ChangeEventHandler, Component, MouseEventHandler } from 'react';
import React, { ChangeEvent, Component, MouseEventHandler } from 'react';
import {
DragDropContext,
Draggable,
Expand Down Expand Up @@ -209,7 +209,8 @@ export class App extends Component<Props, State> {
>
<input
type='text'
onChange={this.changeUrl}
id='remoteUrl'
onChange={this.changeInput}
value={remoteUrl}
placeholder='URL'
/>
Expand All @@ -236,9 +237,9 @@ export class App extends Component<Props, State> {
>
<label htmlFor='spacing'>Spacing (px)</label>
<input
id='spacing'
id='spacingInput'
type='number'
onChange={this.changeSpacing}
onChange={this.changeInput}
value={spacingInput}
style={{
marginLeft: 12,
Expand All @@ -258,9 +259,9 @@ export class App extends Component<Props, State> {
>
<label htmlFor='margin'>Margin (px)</label>
<input
id='margin'
id='marginInput'
type='number'
onChange={this.changeMargin}
onChange={this.changeInput}
value={marginInput}
style={{
marginLeft: 12,
Expand Down Expand Up @@ -353,14 +354,12 @@ export class App extends Component<Props, State> {
});
}

private changeMargin: ChangeEventHandler<HTMLInputElement> = e =>
this.setState({ marginInput: e.target.value })

private changeSpacing: ChangeEventHandler<HTMLInputElement> = e =>
this.setState({ spacingInput: e.target.value })

private changeUrl: ChangeEventHandler<HTMLInputElement> = e =>
this.setState({ remoteUrl: e.target.value })
private changeInput = <T extends keyof State>(
e: ChangeEvent<HTMLInputElement>,
) => {
const newState: { [P in T]: State[P] } = { [e.target.id]: e.target.value };
this.setState(newState);
}

private clear: MouseEventHandler<HTMLButtonElement> = () => {
this.setState({
Expand Down

0 comments on commit 545ec7a

Please sign in to comment.