1
- /* eslint-disable react/prop-types */
2
-
3
- import React , { useState } from 'react' ;
1
+ import React , { ChangeEventHandler , FormEventHandler , useState } from 'react' ;
4
2
import Button from '../button' ;
5
3
import { denominatorTerm } from '../../utils/denominator-term' ;
6
4
import { numeratorTerm } from '../../utils/numerator-term' ;
7
5
import { ValidationWrapper } from './validated-wrapper' ;
6
+ import { Rhythm , UnsavedRhythm } from '../../types' ;
8
7
9
8
const sharedSelectAndInputClassNames = [
10
9
'text-gray-800' ,
@@ -28,7 +27,10 @@ const selectClassNames = [
28
27
'w-48' ,
29
28
] . join ( ' ' ) ;
30
29
31
- const generateNumeratorSelect = ( selected , onChange ) => {
30
+ const generateNumeratorSelect = (
31
+ selected : string ,
32
+ onChange : ChangeEventHandler < HTMLSelectElement >
33
+ ) => {
32
34
const options = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] . map ( ( number ) => {
33
35
return (
34
36
< option key = { number } value = { number } >
@@ -49,7 +51,10 @@ const generateNumeratorSelect = (selected, onChange) => {
49
51
) ;
50
52
} ;
51
53
52
- const generateDenominatorSelect = ( selected , onChange ) => {
54
+ const generateDenominatorSelect = (
55
+ selected : string ,
56
+ onChange : ChangeEventHandler < HTMLSelectElement >
57
+ ) => {
53
58
const options = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] . map ( ( number ) => {
54
59
return (
55
60
< option key = { number } value = { number } >
@@ -70,7 +75,7 @@ const generateDenominatorSelect = (selected, onChange) => {
70
75
) ;
71
76
} ;
72
77
73
- function validate ( rhythm ) {
78
+ function validate ( rhythm : Rhythm | UnsavedRhythm ) {
74
79
const [ numerator , denominator ] = rhythm . frequency ;
75
80
76
81
const result = {
@@ -104,7 +109,15 @@ function validate(rhythm) {
104
109
return result ;
105
110
}
106
111
107
- export default function RhythmEdit ( { rhythm, onClose, onSubmit } ) {
112
+ export default function RhythmEdit ( {
113
+ rhythm,
114
+ onClose,
115
+ onSubmit,
116
+ } : {
117
+ rhythm : Rhythm | UnsavedRhythm ;
118
+ onClose : ( ) => void ;
119
+ onSubmit : ( rhythm : Rhythm | UnsavedRhythm ) => void ;
120
+ } ) {
108
121
const [ rhythmAction , setRhythmAction ] = useState ( rhythm . action ) ;
109
122
const [ rhythmFrequency , setRhythmFrequency ] = useState ( rhythm . frequency ) ;
110
123
const [ rhythmNumerator , rhythmDenominator ] = rhythmFrequency ;
@@ -120,7 +133,7 @@ export default function RhythmEdit({ rhythm, onClose, onSubmit }) {
120
133
121
134
const validationResult = validate ( editedRhythm ) ;
122
135
123
- const submitHandler = ( e ) => {
136
+ const submitHandler : FormEventHandler = ( e ) => {
124
137
e . preventDefault ( ) ;
125
138
setHasSubmitted ( true ) ;
126
139
@@ -129,20 +142,23 @@ export default function RhythmEdit({ rhythm, onClose, onSubmit }) {
129
142
}
130
143
} ;
131
144
132
- const numeratorSelect = generateNumeratorSelect ( rhythmNumerator , ( e ) => {
133
- let numeratorValue = Number ( e . target . value ) ;
134
- let denominatorValue = rhythmDenominator ;
145
+ const numeratorSelect = generateNumeratorSelect (
146
+ rhythmNumerator . toString ( ) ,
147
+ ( e ) => {
148
+ let numeratorValue = Number ( e . target . value ) ;
149
+ let denominatorValue = rhythmDenominator ;
135
150
136
- if ( numeratorValue === denominatorValue ) {
137
- numeratorValue = 1 ;
138
- denominatorValue = 1 ;
139
- }
151
+ if ( numeratorValue === denominatorValue ) {
152
+ numeratorValue = 1 ;
153
+ denominatorValue = 1 ;
154
+ }
140
155
141
- setRhythmFrequency ( [ numeratorValue , denominatorValue ] ) ;
142
- } ) ;
156
+ setRhythmFrequency ( [ numeratorValue , denominatorValue ] ) ;
157
+ }
158
+ ) ;
143
159
144
160
const denominatorSelect = generateDenominatorSelect (
145
- rhythmDenominator ,
161
+ rhythmDenominator . toString ( ) ,
146
162
( e ) => {
147
163
let numeratorValue = rhythmNumerator ;
148
164
let denominatorValue = Number ( e . target . value ) ;
@@ -210,7 +226,7 @@ export default function RhythmEdit({ rhythm, onClose, onSubmit }) {
210
226
</ ValidationWrapper >
211
227
</ div >
212
228
< Button attrs = { { type : 'submit' , className : '' } } >
213
- { rhythm . id ? 'Update' : 'Create' }
229
+ { 'id' in rhythm && rhythm ? 'Update' : 'Create' }
214
230
</ Button >
215
231
</ div >
216
232
</ form >
0 commit comments