@@ -4,14 +4,35 @@ import { TestResults } from '@/components/TestResults';
4
4
import { RegexEngine } from '@/engines/RegexEngine' ;
5
5
import OptionsInput from './OptionsInput' ;
6
6
import { runTest as runBrowserTest , type TestInput , type TestOutput } from '@regexplanet/common' ;
7
+
7
8
type TestFormProps = {
8
9
engine : RegexEngine ;
9
10
testUrl ?: string ; // override for use during engine development
10
11
testInput : TestInput ;
11
12
}
12
13
14
+ function setTestInput ( testInput : TestInput ) {
15
+ const searchParams = new URLSearchParams ( ) ;
16
+ searchParams . set ( 'regex' , testInput . regex ) ;
17
+ searchParams . set ( 'replacement' , testInput . replacement ) ;
18
+ testInput . options . forEach ( option => searchParams . append ( 'option' , option ) ) ;
19
+ testInput . inputs . forEach ( input => searchParams . append ( 'input' , input ) ) ;
20
+
21
+ const url = new URL ( window . location . href ) ;
22
+ url . search = searchParams . toString ( ) ;
23
+ window . history . pushState ( { } , '' , url . toString ( ) ) ;
24
+ }
25
+
13
26
async function runTest ( test_url :string , testInput : TestInput ) : Promise < TestOutput > {
14
27
28
+ console . log ( "running test" , testInput ) ;
29
+ if ( ! testInput || ! testInput . regex ) {
30
+ return {
31
+ success : false ,
32
+ message : "Please enter a regular expression to test" ,
33
+ } ;
34
+ }
35
+
15
36
if ( test_url === 'javascript:runBrowserTest' ) {
16
37
return runBrowserTest ( testInput ) ;
17
38
}
@@ -42,16 +63,20 @@ async function runTest(test_url:string, testInput: TestInput): Promise<TestOutpu
42
63
43
64
export default function TestForm ( props : TestFormProps ) {
44
65
const [ testOutput , setTestOutput ] = useState < TestOutput | null > ( ) ;
45
- const [ testInput , setTestInput ] = useState < TestInput > ( props . testInput ) ;
66
+ //const [testInput, setTestInput] = useState<TestInput>(props.testInput);
67
+ const testInput = props . testInput ;
46
68
47
69
const inputRows = testInput . inputs . map ( ( input , index ) => (
48
70
< div className = "mb-2" key = { `ikey${ index } ` } >
49
- { index <= 0 ? < label htmlFor = { `row${ index } ` } className = "col-sm-2 col-form-label" > Inputs</ label > : < > </ > }
50
71
< input type = "text" className = "form-control" id = { `input${ index } ` } name = "input" defaultValue = { input } />
51
72
</ div >
52
73
) ) ;
53
74
console . log ( "render" , testInput . inputs ) ;
54
75
76
+ const onClearResults = ( ) => {
77
+ setTestOutput ( null ) ;
78
+ } ;
79
+
55
80
const onSubmit = async ( event : React . FormEvent < HTMLFormElement > ) => {
56
81
event . preventDefault ( ) ;
57
82
const form = event . currentTarget ;
@@ -60,9 +85,11 @@ export default function TestForm(props: TestFormProps) {
60
85
const testUrl = props . testUrl || props . engine . test_url ;
61
86
console . log ( testUrl , localInput ) ;
62
87
setTestInput ( localInput ) ;
63
- setTestOutput ( null ) ;
88
+ setTestOutput ( { success : true , message : "Loading..." } ) ;
64
89
if ( testUrl ) {
65
- setTestOutput ( await runTest ( testUrl , localInput ) ) ;
90
+ const newTestOutput = await runTest ( testUrl , localInput ) ;
91
+ console . log ( "newTestOutput" , newTestOutput ) ;
92
+ setTestOutput ( newTestOutput ) ;
66
93
}
67
94
} ;
68
95
@@ -107,10 +134,6 @@ export default function TestForm(props: TestFormProps) {
107
134
{
108
135
props . testUrl ? < div className = "alert alert-warning" > Testing against { props . testUrl } !</ div > : < > </ >
109
136
}
110
- { ( testInput . regex ?
111
- ( testOutput ? < TestResults testOutput = { testOutput } /> : < > < h2 > Results</ h2 > < p > Loading...</ p > </ > )
112
- : < > </ > )
113
- }
114
137
< h2 > Expression to test</ h2 >
115
138
< form method = "get" action = "index.html" onSubmit = { onSubmit } >
116
139
{ props . testUrl ? < input type = "hidden" name = "testurl" value = { props . testUrl } /> : < > </ > }
@@ -124,6 +147,11 @@ export default function TestForm(props: TestFormProps) {
124
147
</ div >
125
148
{ props . engine . options . length > 0 ? < OptionsInput engine = { props . engine } options = { testInput . options } /> : < > </ > }
126
149
< button type = "submit" className = "btn btn-primary" > Test</ button >
150
+ {
151
+ ( testOutput ? < TestResults onClear = { onClearResults } testOutput = { testOutput } /> : < > </ > )
152
+
153
+ }
154
+ < h2 className = "pt-3" > Inputs</ h2 >
127
155
{ inputRows }
128
156
< button type = "submit" className = "btn btn-primary" > Test</ button >
129
157
< button className = "ms-3 btn btn-outline-primary" onClick = { onMoreInputs } > More inputs</ button >
@@ -141,5 +169,5 @@ function formDataToTestInput(engineHandle:string, formData: FormData): TestInput
141
169
options : formData . getAll ( 'option' ) as string [ ] ,
142
170
inputs : formData . getAll ( 'input' ) as string [ ]
143
171
} ;
144
- return retVal ; ;
172
+ return retVal ;
145
173
}
0 commit comments