@@ -134,12 +134,41 @@ function getFilterValueProps(hostType: VpcFirewallRuleHostFilter['type']) {
134134export const CommonFields = ( { error, control } : CommonFieldsProps ) => {
135135 const portRangeForm = useForm ( { defaultValues : portRangeDefaultValues } )
136136 const ports = useController ( { name : 'ports' , control } ) . field
137+ const submitPortRange = portRangeForm . handleSubmit ( ( { portRange } ) => {
138+ const portRangeValue = portRange . trim ( )
139+ // ignore click if invalid or already in the list
140+ // TODO: show error instead of ignoring the click
141+ if ( ! parsePortRange ( portRangeValue ) ) return
142+ if ( ports . value . includes ( portRangeValue ) ) return
143+ ports . onChange ( [ ...ports . value , portRangeValue ] )
144+ portRangeForm . reset ( )
145+ } )
137146
138147 const hostForm = useForm ( { defaultValues : hostDefaultValues } )
139148 const hosts = useController ( { name : 'hosts' , control } ) . field
149+ const submitHost = hostForm . handleSubmit ( ( { type, value } ) => {
150+ // ignore click if empty or a duplicate
151+ // TODO: show error instead of ignoring click
152+ if ( ! type || ! value ) return
153+ if ( hosts . value . some ( ( t ) => t . value === value && t . type === type ) ) return
154+
155+ hosts . onChange ( [ ...hosts . value , { type, value } ] )
156+ hostForm . reset ( )
157+ } )
140158
141159 const targetForm = useForm ( { defaultValues : targetDefaultValues } )
142160 const targets = useController ( { name : 'targets' , control } ) . field
161+ const submitTarget = targetForm . handleSubmit ( ( { type, value } ) => {
162+ // TODO: do this with a normal validation
163+ // ignore click if empty or a duplicate
164+ // TODO: show error instead of ignoring click
165+ if ( ! type || ! value ) return
166+ if ( targets . value . some ( ( t ) => t . value === value && t . type === type ) ) return
167+
168+ targets . onChange ( [ ...targets . value , { type, value } ] )
169+ targetForm . reset ( )
170+ } )
171+
143172 return (
144173 < >
145174 { /* omitting value prop makes it a boolean value. beautiful */ }
@@ -204,6 +233,12 @@ export const CommonFields = ({ error, control }: CommonFieldsProps) => {
204233 { ...getFilterValueProps ( targetForm . watch ( 'type' ) ) }
205234 required
206235 control = { targetForm . control }
236+ onKeyDown = { ( e ) => {
237+ if ( e . key === 'Enter' ) {
238+ e . preventDefault ( ) // prevent full form submission
239+ submitTarget ( e )
240+ }
241+ } }
207242 />
208243
209244 < div className = "flex justify-end" >
@@ -216,19 +251,7 @@ export const CommonFields = ({ error, control }: CommonFieldsProps) => {
216251 >
217252 Clear
218253 </ Button >
219- < Button
220- size = "sm"
221- onClick = { targetForm . handleSubmit ( ( { type, value } ) => {
222- // TODO: do this with a normal validation
223- // ignore click if empty or a duplicate
224- // TODO: show error instead of ignoring click
225- if ( ! type || ! value ) return
226- if ( targets . value . some ( ( t ) => t . value === value && t . type === type ) ) return
227-
228- targets . onChange ( [ ...targets . value , { type, value } ] )
229- targetForm . reset ( )
230- } ) }
231- >
254+ < Button size = "sm" onClick = { submitTarget } >
232255 Add target
233256 </ Button >
234257 </ div >
@@ -300,6 +323,12 @@ export const CommonFields = ({ error, control }: CommonFieldsProps) => {
300323 { ...getFilterValueProps ( hostForm . watch ( 'type' ) ) }
301324 required
302325 control = { hostForm . control }
326+ onKeyDown = { ( e ) => {
327+ if ( e . key === 'Enter' ) {
328+ e . preventDefault ( ) // prevent full form submission
329+ submitHost ( e )
330+ }
331+ } }
303332 />
304333
305334 < div className = "flex justify-end" >
@@ -312,18 +341,7 @@ export const CommonFields = ({ error, control }: CommonFieldsProps) => {
312341 >
313342 Clear
314343 </ Button >
315- < Button
316- size = "sm"
317- onClick = { hostForm . handleSubmit ( ( { type, value } ) => {
318- // ignore click if empty or a duplicate
319- // TODO: show error instead of ignoring click
320- if ( ! type || ! value ) return
321- if ( hosts . value . some ( ( t ) => t . value === value && t . type === type ) ) return
322-
323- hosts . onChange ( [ ...hosts . value , { type, value } ] )
324- hostForm . reset ( )
325- } ) }
326- >
344+ < Button size = "sm" onClick = { submitHost } >
327345 Add host filter
328346 </ Button >
329347 </ div >
@@ -377,6 +395,12 @@ export const CommonFields = ({ error, control }: CommonFieldsProps) => {
377395 description = "A single port (1234) or a range (1234-2345)"
378396 required
379397 control = { portRangeForm . control }
398+ onKeyDown = { ( e ) => {
399+ if ( e . key === 'Enter' ) {
400+ e . preventDefault ( ) // prevent full form submission
401+ submitPortRange ( e )
402+ }
403+ } }
380404 />
381405 < div className = "flex justify-end" >
382406 < Button
@@ -388,18 +412,7 @@ export const CommonFields = ({ error, control }: CommonFieldsProps) => {
388412 >
389413 Clear
390414 </ Button >
391- < Button
392- size = "sm"
393- onClick = { portRangeForm . handleSubmit ( ( { portRange } ) => {
394- const portRangeValue = portRange . trim ( )
395- // ignore click if invalid or already in the list
396- // TODO: show error instead of ignoring the click
397- if ( ! parsePortRange ( portRangeValue ) ) return
398- if ( ports . value . includes ( portRangeValue ) ) return
399- ports . onChange ( [ ...ports . value , portRangeValue ] )
400- portRangeForm . reset ( )
401- } ) }
402- >
415+ < Button size = "sm" onClick = { submitPortRange } >
403416 Add port filter
404417 </ Button >
405418 </ div >
0 commit comments