1
- import React , { useState , useEffect } from 'react'
2
- import PropTypes from 'prop-types'
1
+ import React , { useState , useEffect , ChangeEvent } from 'react'
3
2
import styled , { css } from 'styled-components'
4
- import pathToRegexp from 'path-to-regexp'
3
+ import pathToRegexp , { Token , Key } from 'path-to-regexp'
5
4
import humanizeString from 'humanize-string'
6
- import { objectOfKeyValuesPropType } from '../../lib/service-definitions/service-definition-prop-types'
7
5
import { patternToOptions } from '../../lib/pattern-helpers'
8
6
import { noAutocorrect , StyledInput } from '../common'
9
7
import {
@@ -12,7 +10,11 @@ import {
12
10
BuilderCaption ,
13
11
} from './builder-common'
14
12
15
- const PathBuilderColumn = styled . span `
13
+ interface PathBuilderColumnProps {
14
+ withHorizPadding ?: boolean
15
+ }
16
+
17
+ const PathBuilderColumn = styled . span < PathBuilderColumnProps > `
16
18
height: 78px;
17
19
18
20
float: left;
@@ -28,7 +30,11 @@ const PathBuilderColumn = styled.span`
28
30
` } ;
29
31
`
30
32
31
- const PathLiteral = styled . div `
33
+ interface PathLiteralProps {
34
+ isFirstToken : boolean
35
+ }
36
+
37
+ const PathLiteral = styled . div < PathLiteralProps > `
32
38
margin-top: 39px;
33
39
${ ( { isFirstToken } ) =>
34
40
isFirstToken &&
@@ -68,7 +74,13 @@ const NamedParamCaption = styled(BuilderCaption)`
68
74
text-align: center;
69
75
`
70
76
71
- export function constructPath ( { tokens, namedParams } ) {
77
+ export function constructPath ( {
78
+ tokens,
79
+ namedParams,
80
+ } : {
81
+ tokens : Token [ ]
82
+ namedParams : { [ k : string ] : string }
83
+ } ) {
72
84
let isComplete = true
73
85
const path = tokens
74
86
. map ( token => {
@@ -96,6 +108,17 @@ export default function PathBuilder({
96
108
exampleParams,
97
109
onChange,
98
110
isPrefilled,
111
+ } : {
112
+ pattern : string
113
+ exampleParams : { [ k : string ] : string }
114
+ onChange : ( {
115
+ path,
116
+ isComplete,
117
+ } : {
118
+ path : string
119
+ isComplete : boolean
120
+ } ) => void
121
+ isPrefilled : boolean
99
122
} ) {
100
123
const [ tokens ] = useState ( ( ) => pathToRegexp . parse ( pattern ) )
101
124
const [ namedParams , setNamedParams ] = useState ( ( ) =>
@@ -106,10 +129,14 @@ export default function PathBuilder({
106
129
// objects.
107
130
tokens
108
131
. filter ( t => typeof t !== 'string' )
109
- . reduce ( ( accum , { name } ) => {
110
- accum [ name ] = ''
111
- return accum
112
- } , { } )
132
+ . map ( t => t as Key )
133
+ . reduce (
134
+ ( accum , { name } ) => {
135
+ accum [ name ] = ''
136
+ return accum
137
+ } ,
138
+ { } as { [ k : string ] : string }
139
+ )
113
140
)
114
141
115
142
useEffect ( ( ) => {
@@ -120,25 +147,26 @@ export default function PathBuilder({
120
147
}
121
148
} , [ tokens , namedParams , onChange ] )
122
149
123
- function handleTokenChange ( evt ) {
124
- const { name, value } = evt . target
125
-
150
+ function handleTokenChange ( {
151
+ target : { name, value } ,
152
+ } : ChangeEvent < HTMLInputElement | HTMLSelectElement > ) {
126
153
setNamedParams ( {
127
154
...namedParams ,
128
155
[ name ] : value ,
129
156
} )
130
157
}
131
158
132
- function renderLiteral ( literal , tokenIndex ) {
159
+ function renderLiteral ( literal : string , tokenIndex : number ) {
133
160
return (
134
161
< PathBuilderColumn key = { `${ tokenIndex } -${ literal } ` } >
135
162
< PathLiteral isFirstToken = { tokenIndex === 0 } > { literal } </ PathLiteral >
136
163
</ PathBuilderColumn >
137
164
)
138
165
}
139
166
140
- function renderNamedParamInput ( token ) {
141
- const { name, pattern } = token
167
+ function renderNamedParamInput ( token : Key ) {
168
+ const { pattern } = token
169
+ const name = `${ token . name } `
142
170
const options = patternToOptions ( pattern )
143
171
144
172
const value = namedParams [ name ]
@@ -174,8 +202,13 @@ export default function PathBuilder({
174
202
}
175
203
}
176
204
177
- function renderNamedParam ( token , tokenIndex , namedParamIndex ) {
178
- const { delimiter, name, optional } = token
205
+ function renderNamedParam (
206
+ token : Key ,
207
+ tokenIndex : number ,
208
+ namedParamIndex : number
209
+ ) {
210
+ const { delimiter, optional } = token
211
+ const name = `${ token . name } `
179
212
180
213
const exampleValue = exampleParams [ name ] || '(not set)'
181
214
@@ -209,9 +242,3 @@ export default function PathBuilder({
209
242
</ BuilderContainer >
210
243
)
211
244
}
212
- PathBuilder . propTypes = {
213
- pattern : PropTypes . string . isRequired ,
214
- exampleParams : objectOfKeyValuesPropType ,
215
- onChange : PropTypes . func ,
216
- isPrefilled : PropTypes . bool ,
217
- }
0 commit comments