-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor how errors are listened to, use the new "onError" callback
- Loading branch information
1 parent
4097309
commit c2d6931
Showing
22 changed files
with
211 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...management/public/application/sections/edit_policy/form/components/enhanced_use_field.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React, { useEffect, useRef, useMemo, useCallback } from 'react'; | ||
|
||
// We wrap this component for edit policy so we do not export it from the "shared_imports" dir to avoid | ||
// accidentally using the non-enhanced version. | ||
import { UseField } from '../../../../../../../../../src/plugins/es_ui_shared/static/forms/hook_form_lib'; | ||
|
||
import { Phases } from '../../../../../../common/types'; | ||
|
||
import { UseFieldProps, FormData } from '../../../../../shared_imports'; | ||
|
||
import { useFormErrorsContext } from '../form_errors_context'; | ||
|
||
const isXPhaseField = (phase: keyof Phases) => (fieldPath: string): boolean => | ||
fieldPath.startsWith(`phases.${phase}`) || fieldPath.startsWith(`_meta.${phase}`); | ||
|
||
const isHotPhaseField = isXPhaseField('hot'); | ||
const isWarmPhaseField = isXPhaseField('warm'); | ||
const isColdPhaseField = isXPhaseField('cold'); | ||
const isDeletePhaseField = isXPhaseField('delete'); | ||
|
||
const determineFieldPhase = (fieldPath: string): keyof Phases | 'other' => { | ||
if (isHotPhaseField(fieldPath)) { | ||
return 'hot'; | ||
} | ||
if (isWarmPhaseField(fieldPath)) { | ||
return 'warm'; | ||
} | ||
if (isColdPhaseField(fieldPath)) { | ||
return 'cold'; | ||
} | ||
if (isDeletePhaseField(fieldPath)) { | ||
return 'delete'; | ||
} | ||
return 'other'; | ||
}; | ||
|
||
export const EnhancedUseField = <T, F = FormData, I = T>( | ||
props: UseFieldProps<T, F, I> | ||
): React.ReactElement<any, any> | null => { | ||
const { path } = props; | ||
const isMounted = useRef<boolean>(false); | ||
const phase = useMemo(() => determineFieldPhase(path), [path]); | ||
const { addError, clearError } = useFormErrorsContext(); | ||
|
||
const onError = useCallback( | ||
(errors: string[] | null) => { | ||
if (!isMounted.current) { | ||
return; | ||
} | ||
if (errors) { | ||
addError(phase, path, errors); | ||
} else { | ||
clearError(phase, path); | ||
} | ||
}, | ||
[phase, path, addError, clearError] | ||
); | ||
|
||
useEffect(() => { | ||
isMounted.current = true; | ||
return () => { | ||
isMounted.current = false; | ||
}; | ||
}, []); | ||
|
||
return <UseField {...props} onError={onError} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ | |
*/ | ||
|
||
export { Form } from './form'; | ||
|
||
export { EnhancedUseField } from './enhanced_use_field'; |
Oops, something went wrong.