11import { FormControl , FormHelperText , InputLabel , OutlinedInput } from "@mui/material" ;
2- import { useFormContext } from "react-hook-form" ;
2+ import { Controller , useFieldArray , useFormContext } from "react-hook-form" ;
33import { useFormStyles } from "styles/form" ;
44import { MetricFormValues } from "../MetricForm.types" ;
5+ import { Button , FormControl , FormHelperText , IconButton , InputLabel , OutlinedInput } from "@mui/material" ;
6+ import DeleteIcon from "@mui/icons-material/Delete" ;
7+ import { Autocomplete } from "components/Autocomplete/Autocomplete" ;
8+ import { useMemo } from "react" ;
59
610export const MetricFormStepSQL = ( ) => {
7- const { register, formState : { errors } } = useFormContext < MetricFormValues > ( ) ;
11+ const { control , register, formState : { errors } , clearErrors } = useFormContext < MetricFormValues > ( ) ;
812 const { classes, cx } = useFormStyles ( ) ;
13+ const SqlFields = useFieldArray ( { control, name : "SQLs" } ) ;
914
1015 const hasError = ( field : keyof MetricFormValues ) => ! ! errors [ field ] ;
1116
@@ -16,31 +21,91 @@ export const MetricFormStepSQL = () => {
1621 }
1722 return undefined ;
1823 } ;
24+ const handleSqlAppend = ( ) => {
25+ SqlFields . append ( { Version : "" , SQL : "" } ) ;
26+ clearErrors ( "PresetMetrics" ) ;
27+ } ;
28+
29+ const VersionOptions = useMemo ( ( ) => [
30+ "15" ,
31+ "14" ,
32+ "13" ,
33+ "12" ,
34+ "11" ,
35+ "10" ,
36+ ] . map ( ( key ) => ( { label : key } ) ) ,
37+ [ ]
38+ ) ;
39+
40+ const getArrayError = (
41+ index : number ,
42+ field : "Version" | "SQL"
43+ ) => errors . SQLs ?. [ index ] ?. [ field ] ?. message ;
1944
2045 return (
2146 < div className = { classes . form } >
22- < FormControl
23- className = { cx ( classes . formControlInput , classes . widthFull ) }
24- error = { hasError ( "SQLs" ) }
25- variant = "outlined"
26- >
27- < InputLabel htmlFor = "SQLs" > SQLs</ InputLabel >
28- < OutlinedInput
29- { ...register ( "SQLs" ) }
30- id = "SQLs"
31- label = "SQLs"
32- aria-describedby = "SQLs-error"
33- multiline
34- rows = { 15 }
35- inputProps = { {
36- style : {
37- font : "revert" ,
38- fontSize : "0.7rem" ,
39- }
40- } }
41- />
47+ { SqlFields . fields . map ( ( { id } , index ) => (
48+ < div className = { classes . row } key = { id } >
49+ < FormControl
50+ className = { cx ( classes . formControlInput , classes . widthQuarter ) }
51+ variant = "outlined"
52+ error = { ! ! getArrayError ( index , "Version" ) }
53+ >
54+ < Controller
55+ name = { `SQLs.${ index } .Version` }
56+ control = { control }
57+ render = { ( { field } ) => (
58+ < Autocomplete
59+ { ...field }
60+ id = { `SQLs.${ index } .Version` }
61+ label = "Version"
62+ options = { VersionOptions }
63+ freeSolo
64+ value = { field . value }
65+ onInputChange = { ( _ , value ) => field . onChange ( value ) }
66+ // TDDO: Restrict input to numbers and dots only, but allow free solo for custom versions
67+
68+ />
69+ ) }
70+ />
71+ < FormHelperText > { getArrayError ( index , "Version" ) } </ FormHelperText >
72+ </ FormControl >
73+ < FormControl
74+ className = { cx ( classes . formControlInput , classes . widthThreeQuarter ) }
75+ variant = "outlined"
76+ error = { hasError ( "SQLs" ) }
77+ >
78+ < InputLabel htmlFor = { `SQLs.${ index } .SQL` } > SQL</ InputLabel >
79+ < OutlinedInput
80+ { ...register ( `SQLs.${ index } .SQL` ) }
81+ id = { `SQLs.${ index } .SQL` }
82+ label = "SQL"
83+ multiline
84+ minRows = { 3 }
85+ maxRows = { 12 }
86+ endAdornment = {
87+ < IconButton
88+ key = { `SQLs.${ index } .Delete` }
89+ title = "Delete SQL"
90+ onClick = { ( ) => SqlFields . remove ( index ) }
91+ >
92+ < DeleteIcon />
93+ </ IconButton >
94+ }
95+ />
96+ < FormHelperText > { getArrayError ( index , "SQL" ) } </ FormHelperText >
97+ </ FormControl >
98+ </ div >
99+ ) ) }
100+ < div className = { cx ( classes . row , classes . addButton ) } >
101+ < Button
102+ variant = "contained"
103+ onClick = { handleSqlAppend }
104+ >
105+ Add SQLs
106+ </ Button >
107+ </ div >
42108 < FormHelperText id = "SQLs-error" > { getError ( "SQLs" ) } </ FormHelperText >
43- </ FormControl >
44109 </ div >
45110 ) ;
46111} ;
0 commit comments