diff --git a/x-pack/plugins/synthetics/public/legacy_uptime/components/fleet_package/browser/simple_fields.tsx b/x-pack/plugins/synthetics/public/legacy_uptime/components/fleet_package/browser/simple_fields.tsx index e25f135c5efa8..91c34a270b562 100644 --- a/x-pack/plugins/synthetics/public/legacy_uptime/components/fleet_package/browser/simple_fields.tsx +++ b/x-pack/plugins/synthetics/public/legacy_uptime/components/fleet_package/browser/simple_fields.tsx @@ -105,6 +105,7 @@ export const BrowserSimpleFields = memo(({ validate, onFieldBlur }) => { } > void; onFieldBlur: (field: ConfigKey) => void; defaultConfig?: SourceConfig; + validate?: Validation; } export const defaultValues = { @@ -72,33 +73,31 @@ const getDefaultTab = (defaultConfig: SourceConfig, isZipUrlSourceEnabled = true return isZipUrlSourceEnabled ? SourceType.ZIP : SourceType.INLINE; }; -export const SourceField = ({ onChange, onFieldBlur, defaultConfig = defaultValues }: Props) => { +export const SourceField = ({ + onChange, + onFieldBlur, + defaultConfig = defaultValues, + validate, +}: Props) => { const { isZipUrlSourceEnabled } = usePolicyConfigContext(); const [sourceType, setSourceType] = useState( getDefaultTab(defaultConfig, isZipUrlSourceEnabled) ); const [config, setConfig] = useState(defaultConfig); - const [touchedFields, setTouchedFields] = useState>({}); useEffect(() => { onChange(config); }, [config, onChange]); - const onFieldTouched = (field: ConfigKey) => { - if (!touchedFields[field]) { - setTouchedFields((existing) => ({ ...existing, [field]: true })); - } + const isSourceInlineInvalid = + validate?.[ConfigKey.SOURCE_INLINE]?.({ + [ConfigKey.SOURCE_INLINE]: config.inlineScript, + }) ?? false; - onFieldBlur(field); - }; - - const isWithInUptime = window.location.pathname.includes('/app/uptime'); - const isZipUrlInvalid = isWithInUptime - ? touchedFields[ConfigKey.SOURCE_ZIP_URL] && !config.zipUrl - : !config.zipUrl; - const isScriptInvalid = isWithInUptime - ? touchedFields[ConfigKey.SOURCE_INLINE] && !config.inlineScript - : !config.inlineScript; + const isZipUrlInvalid = + validate?.[ConfigKey.SOURCE_ZIP_URL]?.({ + [ConfigKey.SOURCE_ZIP_URL]: config.zipUrl, + }) ?? false; const zipUrlLabel = ( setConfig((prevConfig) => ({ ...prevConfig, zipUrl: value })) } - onBlur={() => onFieldTouched(ConfigKey.SOURCE_ZIP_URL)} + onBlur={() => onFieldBlur(ConfigKey.SOURCE_ZIP_URL)} value={config.zipUrl} data-test-subj="syntheticsBrowserZipUrl" /> @@ -284,7 +283,7 @@ export const SourceField = ({ onChange, onFieldBlur, defaultConfig = defaultValu 'data-test-subj': `syntheticsSourceTab__inline`, content: ( { setConfig((prevConfig) => ({ ...prevConfig, inlineScript: code })); - onFieldTouched(ConfigKey.SOURCE_INLINE); + onFieldBlur(ConfigKey.SOURCE_INLINE); }} value={config.inlineScript} />