-
Notifications
You must be signed in to change notification settings - Fork 903
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core/presentation): Remove validationStatus from forms apis
This removes the validationStatus prop from the FormField and FormikFormField components. These props were previously used to provide an explicit validation status (e.g., 'warning' or 'error') into the Form Components. The functionality has been replaced with message generator functions in `categories.ts`, i.e., errorMessage('Something is wrong'). Before: `<FormField validationMessage='Something is wrong' validationStatus="error" />` After: `<FormField validationMessage={errorMessage('Something is wrong')} />`
- Loading branch information
1 parent
eafb077
commit 4c5f532
Showing
11 changed files
with
201 additions
and
269 deletions.
There are no files selected for viewing
102 changes: 0 additions & 102 deletions
102
app/scripts/modules/core/src/presentation/forms/FormField.tsx
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
app/scripts/modules/core/src/presentation/forms/fields/FormField.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,71 @@ | ||
import * as React from 'react'; | ||
|
||
import { firstDefined, noop } from 'core/utils'; | ||
|
||
import { IControlledInputProps, IFormInputProps, IFormInputValidation } from '../inputs'; | ||
import { ILayoutProps, LayoutContext } from '../layouts'; | ||
import { IValidator, useValidationData } from '../validation'; | ||
|
||
import { ICommonFormFieldProps } from './interface'; | ||
import { createFieldValidator } from './FormikFormField'; | ||
import { renderContent } from './renderContent'; | ||
|
||
import '../forms.less'; | ||
|
||
export type IFormFieldProps = ICommonFormFieldProps & Partial<IControlledInputProps>; | ||
|
||
const { useState, useCallback, useContext, useMemo } = React; | ||
|
||
export function FormField(props: IFormFieldProps): JSX.Element { | ||
const { input, layout, label, help, required, actions } = props; | ||
const { value } = props; | ||
|
||
// Internal validators are defined by an Input component | ||
const [internalValidators, setInternalValidators] = useState([]); | ||
const addValidator = useCallback((v: IValidator) => setInternalValidators(list => list.concat(v)), []); | ||
const removeValidator = useCallback((v: IValidator) => setInternalValidators(list => list.filter(x => x !== v)), []); | ||
|
||
// Capture props.validate when the component initializes (to allow for inline arrow functions) | ||
const validate = useMemo(() => props.validate, []); | ||
|
||
const fieldValidator = useMemo( | ||
() => createFieldValidator(label, required, [].concat(validate || noop).concat(internalValidators)), | ||
[label, required, validate], | ||
); | ||
|
||
const FieldLayoutFromContext = useContext(LayoutContext); | ||
const inputRenderPropOrNode = firstDefined(input, noop); | ||
const layoutFromContext = (fieldLayoutProps: ILayoutProps) => <FieldLayoutFromContext {...fieldLayoutProps} />; | ||
const layoutRenderPropOrNode = firstDefined(layout, layoutFromContext); | ||
|
||
const [hasBlurred, setHasBlurred] = useState(false); | ||
const touched = firstDefined(props.touched, hasBlurred); | ||
const validatorResult = useMemo(() => fieldValidator(value), [fieldValidator, value]); | ||
const validationMessage = firstDefined(props.validationMessage, validatorResult ? validatorResult : undefined); | ||
const validationData = useValidationData(validationMessage, touched); | ||
|
||
const validation: IFormInputValidation = { | ||
touched, | ||
addValidator, | ||
removeValidator, | ||
...validationData, | ||
}; | ||
|
||
const controlledInputProps: IControlledInputProps = { | ||
value: props.value, | ||
name: props.name || '', | ||
onChange: props.onChange || noop, | ||
onBlur: (e: React.FocusEvent) => { | ||
setHasBlurred(true); | ||
props.onBlur && props.onBlur(e); | ||
}, | ||
}; | ||
|
||
// Render the input | ||
const inputProps: IFormInputProps = { ...controlledInputProps, validation }; | ||
const inputElement = renderContent(inputRenderPropOrNode, inputProps); | ||
|
||
// Render the layout passing the rendered input in | ||
const layoutProps: ILayoutProps = { label, help, required, actions, validation, input: inputElement }; | ||
return <>{renderContent(layoutRenderPropOrNode, layoutProps)}</>; | ||
} |
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
Oops, something went wrong.