1- import React , { useRef } from 'react'
1+ import React , { useMemo , useRef } from 'react'
22import { RouteComponentProps } from 'react-router-dom'
33import { useTranslation } from 'react-i18next'
44import { Stack , PrimaryButton , DefaultButton , TextField } from 'office-ui-fabric-react'
@@ -17,28 +17,42 @@ const NetworkEditor = ({
1717} : React . PropsWithoutRef < StateWithDispatch & RouteComponentProps < { id : string } > > ) => {
1818 const editor = useNetworkEditor ( )
1919 const [ t ] = useTranslation ( )
20- const inputs = useInputs ( editor )
20+ const cachedNetworks = useRef ( networks )
21+ const cachedNetwork = cachedNetworks . current . find ( network => network . id === id )
22+ const usedNetworkNames = useMemo (
23+ ( ) => networks . map ( n => n . name ) . filter ( name => name !== ( ( cachedNetwork && cachedNetwork . name ) || '' ) ) ,
24+ [ networks ]
25+ )
26+ const inputs = useInputs ( editor , usedNetworkNames , t )
2127 const goBack = useGoBack ( history )
2228 useInitialize ( id , networks , editor . initialize , dispatch )
2329
24- const cachedNetworks = useRef ( networks )
25- const cachedNetwork = cachedNetworks . current . find ( network => network . id === id )
26- const { invalidParams, notModified } = useIsInputsValid ( editor , cachedNetwork )
30+ const { errors, setErrors, notModified } = useIsInputsValid ( editor , cachedNetwork )
2731 const handleSubmit = useHandleSubmit ( id , editor . name . value , editor . remote . value , networks , history , dispatch )
2832
2933 return (
3034 < Stack tokens = { { childrenGap : 15 } } >
3135 < h1 > { t ( 'settings.network.edit-network.title' ) } </ h1 >
3236 < Stack tokens = { { childrenGap : 15 } } >
33- { inputs . map ( inputProps => (
37+ { inputs . map ( ( inputProps , idx ) => (
3438 < Stack . Item key = { inputProps . label } >
35- < TextField { ...inputProps } key = { inputProps . label } required />
39+ < TextField
40+ { ...inputProps }
41+ key = { inputProps . label }
42+ required
43+ validateOnLoad = { false }
44+ onNotifyValidationResult = { ( msg : any ) => {
45+ const errs = [ ...errors ]
46+ errs . splice ( idx , 1 , msg !== '' )
47+ setErrors ( errs )
48+ } }
49+ />
3650 </ Stack . Item >
3751 ) ) }
3852 </ Stack >
3953 < Stack horizontal horizontalAlign = "end" tokens = { { childrenGap : 20 } } >
4054 < DefaultButton onClick = { goBack } text = { t ( 'common.cancel' ) } />
41- < PrimaryButton disabled = { invalidParams || notModified } onClick = { handleSubmit } text = { t ( 'common.save' ) } />
55+ < PrimaryButton disabled = { errors . includes ( true ) || notModified } onClick = { handleSubmit } text = { t ( 'common.save' ) } />
4256 </ Stack >
4357 </ Stack >
4458 )
0 commit comments