Skip to content

Commit 984a5ec

Browse files
author
Oskar Widmark
committed
refactor: remove react/input event types
1 parent 0c98b93 commit 984a5ec

File tree

6 files changed

+41
-43
lines changed

6 files changed

+41
-43
lines changed

src/App.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React, { MouseEvent } from 'react';
1+
import React from 'react';
22
import './App.css';
3-
import { SelectChangeEvent, Stack, Tab, Tabs } from '@mui/material';
3+
import { Stack, Tab, Tabs } from '@mui/material';
44
import {
55
SortValue,
66
SortName,
@@ -127,10 +127,10 @@ class App extends React.Component<Props> {
127127
window.removeEventListener('resize', this.resizeCanvas);
128128
}
129129

130-
drawOnCanvas = (event: MouseEvent<HTMLCanvasElement>) => {
130+
drawOnCanvas = (mouseX: number, mouseY: number) => {
131131
if (!this.canvasController.isDrawing) return;
132132

133-
const drawData = this.canvasController.getDrawData(event);
133+
const drawData = this.canvasController.getDrawData(mouseX, mouseY);
134134
for (const data of drawData) {
135135
this.arr[data.index].value = data.value;
136136
}
@@ -316,10 +316,10 @@ class App extends React.Component<Props> {
316316
this.setState({ areSettingsOpen: !this.state.areSettingsOpen });
317317
};
318318

319-
chooseSortAlg = (event: SelectChangeEvent<string>) => {
319+
chooseSortAlg = (chosenSortAlg: SortName) => {
320320
this.stopSorting();
321321

322-
this.setSettings({ chosenSortAlg: event.target.value as SortName });
322+
this.setSettings({ chosenSortAlg });
323323
};
324324

325325
changeColumnNbr = (_: unknown, value: number | number[]) => {
@@ -348,12 +348,12 @@ class App extends React.Component<Props> {
348348
this.canvasController.redraw(this.arr);
349349
};
350350

351-
startDrawOnCanvas = (event: MouseEvent<HTMLCanvasElement>) => {
351+
startDrawOnCanvas = (mouseX: number, mouseY: number) => {
352352
if (!this.state.canDraw) return;
353353

354354
this.stopSorting();
355355
this.canvasController.isDrawing = true;
356-
this.drawOnCanvas(event);
356+
this.drawOnCanvas(mouseX, mouseY);
357357
};
358358

359359
toggleCanDraw = () => {
@@ -457,8 +457,12 @@ class App extends React.Component<Props> {
457457
<canvas
458458
className="App-canvas"
459459
ref={this.canvasController.canvasRef}
460-
onMouseDown={this.startDrawOnCanvas}
461-
onMouseMove={this.drawOnCanvas}
460+
onMouseDown={(event) =>
461+
this.startDrawOnCanvas(event.clientX, event.clientY)
462+
}
463+
onMouseMove={(event) =>
464+
this.drawOnCanvas(event.clientX, event.clientY)
465+
}
462466
onMouseUp={this.canvasController.endDraw}
463467
onMouseLeave={this.canvasController.endDraw}
464468
/>
@@ -481,7 +485,7 @@ class App extends React.Component<Props> {
481485
<TitledSelect
482486
title="Sorting Algorithm"
483487
value={this.state.settings.chosenSortAlg}
484-
onChange={this.chooseSortAlg}
488+
onSelect={(value) => this.chooseSortAlg(value as SortName)}
485489
options={Object.values(SortName)}
486490
/>
487491
<Options
@@ -519,9 +523,9 @@ class App extends React.Component<Props> {
519523
<TitledSelect
520524
title="Reset Preset"
521525
value={this.state.settings.resetPreset}
522-
onChange={(event) => {
526+
onSelect={(value) => {
523527
this.setSettings({
524-
resetPreset: event.target.value as ResetPreset,
528+
resetPreset: value as ResetPreset,
525529
});
526530
}}
527531
options={Object.values(ResetPreset)}

src/ColorTab.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function ColorTab(props: {
3434
<TitledSelect
3535
title="Preset"
3636
value={colorPreset}
37-
onChange={(e) => setColorPreset(e.target.value as ColorPreset)}
37+
onSelect={(value) => setColorPreset(value as ColorPreset)}
3838
options={Object.values(ColorPreset)}
3939
/>
4040
{(colorPreset === ColorPreset.Custom ||

src/Options.tsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ const getAlgorithmOptionFields = (
2727
}
2828
};
2929

30-
const isValidOption = (field: keyof AlgorithmOptions, value: unknown) => {
30+
const isValidOption = (
31+
field: keyof AlgorithmOptions,
32+
value: unknown,
33+
): value is AlgorithmOptions[typeof field] => {
3134
switch (field) {
3235
case 'base':
3336
return Number(value) >= 2 && Number.isInteger(Number(value));
@@ -93,20 +96,14 @@ export function Options({
9396
useState<AlgorithmOptions>({ ...algorithmOptions });
9497

9598
const handleOptionChange = useCallback(
96-
(
97-
field: keyof AlgorithmOptions,
98-
event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
99-
) => {
99+
(field: keyof AlgorithmOptions, value: unknown) => {
100100
setNonValidatedOptions({
101101
...nonValidatedOptions,
102-
[field]: event.target.value,
102+
[field]: value,
103103
});
104104

105-
if (isValidOption(field, event.target.value)) {
106-
setAlgorithmOption(
107-
field,
108-
event.target.value as AlgorithmOptions[typeof field],
109-
);
105+
if (isValidOption(field, value)) {
106+
setAlgorithmOption(field, value);
110107
}
111108
},
112109
[nonValidatedOptions, setAlgorithmOption],
@@ -135,7 +132,9 @@ export function Options({
135132
select={ALGORITHM_OPTION_TEXT_FIELD_TYPES[field] === 'select'}
136133
label={ALGORITHM_OPTION_LABELS[field]}
137134
value={nonValidatedOptions[field]}
138-
onChange={(event) => handleOptionChange(field, event)}
135+
onChange={(event) =>
136+
handleOptionChange(field, event.target.value)
137+
}
139138
error={!isValidOption(field, nonValidatedOptions[field])}
140139
size="small"
141140
sx={{ width: 120 }}

src/SoundTab.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ export const SoundTab = ({
4444
<TitledSelect
4545
title="Sound type"
4646
value={soundType}
47-
onChange={(event) =>
48-
setSoundType(event.target.value as NonCustomOscillatorType)
49-
}
47+
onSelect={(value) => setSoundType(value as NonCustomOscillatorType)}
5048
options={SOUND_TYPE_OPTIONS}
5149
/>
5250
<TitledSlider

src/TitledSelect.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
import {
2-
FormControl,
3-
Typography,
4-
Select,
5-
MenuItem,
6-
SelectChangeEvent,
7-
} from '@mui/material';
1+
import { FormControl, Typography, Select, MenuItem } from '@mui/material';
82

93
export const TitledSelect = ({
104
title,
115
value,
12-
onChange,
6+
onSelect,
137
options,
148
}: {
159
title: string;
1610
value: string;
17-
onChange: (event: SelectChangeEvent<string>) => void;
11+
onSelect: (value: string) => void;
1812
options: string[];
1913
}) => {
2014
return (
@@ -28,7 +22,11 @@ export const TitledSelect = ({
2822
>
2923
{title}
3024
</Typography>
31-
<Select value={value} onChange={onChange} size="small">
25+
<Select
26+
value={value}
27+
onChange={(event) => onSelect(event.target.value)}
28+
size="small"
29+
>
3230
{options.map((v) => (
3331
<MenuItem value={v} key={v}>
3432
<Typography align="left" variant="body2" color="textSecondary">

src/canvas-controller.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { MouseEvent } from 'react';
21
import { ColorPreset, DrawData, SortValue } from './types';
32
import { hsvToRgbHex, rgbHexToHsv } from './utils';
43

@@ -159,7 +158,7 @@ export class CanvasController {
159158
this.drawAll(arr);
160159
};
161160

162-
getDrawData = (event: MouseEvent<HTMLCanvasElement>): DrawData[] => {
161+
getDrawData = (mouseX: number, mouseY: number): DrawData[] => {
163162
if (!this.isDrawing) return [];
164163

165164
const drawData = [];
@@ -176,10 +175,10 @@ export class CanvasController {
176175
const rect = canvas.getBoundingClientRect();
177176

178177
const colIndex = Math.floor(
179-
((event.clientX - rect.left) / canvas.width) * this.context.columnNbr,
178+
((mouseX - rect.left) / canvas.width) * this.context.columnNbr,
180179
);
181180
const colHeight = Math.floor(
182-
((canvas.height - (event.clientY - rect.top)) / canvas.height) *
181+
((canvas.height - (mouseY - rect.top)) / canvas.height) *
183182
this.context.columnNbr,
184183
);
185184

0 commit comments

Comments
 (0)