Skip to content

Commit

Permalink
feat(ValidatedTextInput): add optional onChange prop to ValidatedTe…
Browse files Browse the repository at this point in the history
…xtInput for extra effects defined in the parent component (#97)

In addition to having the onChange option in useFormField, this adds an onChange prop to ValidatedTextInput
in order to trigger additional effects (like resetting query state) at the component level rather than at the form state level.
  • Loading branch information
mturley authored Mar 1, 2022
1 parent d533da2 commit f06de7e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/components/ValidatedTextInput/ValidatedPasswordInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ interface IValidatedPasswordInputProps
greenWhenValid?: boolean;
/** Extra callback to call onBlur in addition to setting the field isTouched in state */
onBlur?: () => void;
/** Extra callback to call onChange in addition to setting the field value in state */
onChange?: (value: string) => void;
/** Any extra props for the PatternFly FormGroup */
formGroupProps?: Partial<FormGroupProps>;
/** Any extra props for the PatternFly TextInput */
Expand All @@ -33,13 +35,14 @@ export const ValidatedPasswordInput: React.FunctionComponent<IValidatedPasswordI
isRequired,
greenWhenValid = false,
onBlur,
onChange,
formGroupProps = {},
inputProps = {},
showPasswordAriaLabel = `Show ${label}`,
hidePasswordAriaLabel = `Hide ${label}`,
}: IValidatedPasswordInputProps) => {
const [isValueVisible, toggleValueVisible] = React.useReducer((isVisible) => !isVisible, false);
const options: TextFieldOptions = { greenWhenValid, onBlur };
const options: TextFieldOptions = { greenWhenValid, onBlur, onChange };
return (
<FormGroup
label={label}
Expand Down
5 changes: 4 additions & 1 deletion src/components/ValidatedTextInput/ValidatedTextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ interface IValidatedTextInputProps
greenWhenValid?: boolean;
/** Extra callback to call onBlur in addition to setting the field isTouched in state */
onBlur?: () => void;
/** Extra callback to call onChange in addition to setting the field value in state */
onChange?: (value: string) => void;
/** Any extra props for the PatternFly FormGroup */
formGroupProps?: Partial<FormGroupProps>;
/** Any extra props for the PatternFly TextInput or TextArea */
Expand All @@ -41,10 +43,11 @@ export const ValidatedTextInput: React.FunctionComponent<IValidatedTextInputProp
type = 'text',
greenWhenValid = false,
onBlur,
onChange,
formGroupProps = {},
inputProps = {},
}: IValidatedTextInputProps) => {
const options: TextFieldOptions = { greenWhenValid, onBlur };
const options: TextFieldOptions = { greenWhenValid, onBlur, onChange };
return (
<FormGroup
label={label}
Expand Down
5 changes: 4 additions & 1 deletion src/hooks/useFormState/useFormState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,17 @@ export const getFormGroupProps = <T>(
export interface TextFieldOptions {
greenWhenValid?: boolean;
onBlur?: () => void;
onChange?: (value: string) => void;
}

export const getTextFieldProps = (
field: IValidatedFormField<string> | IValidatedFormField<string | undefined>,
options?: TextFieldOptions
): Pick<TextInputProps | TextAreaProps, 'value' | 'onChange' | 'onBlur' | 'validated'> => ({
value: field.value,
onChange: field.setValue,
onChange: (value: string) => {
field.setValue(value), options?.onChange?.(value);
},
onBlur: () => {
field.setIsTouched(true);
options?.onBlur?.();
Expand Down

0 comments on commit f06de7e

Please sign in to comment.