Skip to content

Commit

Permalink
feat(core/presentation): support JSX for validationMessage in FormFie…
Browse files Browse the repository at this point in the history
…lds (#7083)

e.g.:
<FormikFormField validationMessage={<Spinner />} />
  • Loading branch information
christopherthielen committed Jun 4, 2019
1 parent aaf7745 commit e6d1586
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
5 changes: 3 additions & 2 deletions app/scripts/modules/core/src/presentation/forms/FormField.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { Subject } from 'rxjs';
import { isString } from 'lodash';
import { isNil, isString } from 'lodash';

import { noop } from 'core/utils';

Expand Down Expand Up @@ -37,6 +37,7 @@ interface IFormFieldState {
}

const ifString = (val: any): string => (isString(val) ? val : undefined);
const firstDefinedNode = (...values: React.ReactNode[]): React.ReactNode => values.find(val => !isNil(val));

export class FormField extends React.Component<IFormFieldProps, IFormFieldState> implements IFormFieldApi {
public static defaultProps: Partial<IFormFieldProps> = {
Expand All @@ -63,7 +64,7 @@ export class FormField extends React.Component<IFormFieldProps, IFormFieldState>

public touched = () => this.props.touched;

public validationMessage = () => ifString(this.props.validationMessage) || ifString(this.state.validationMessage);
public validationMessage = () => firstDefinedNode(this.props.validationMessage, this.state.validationMessage);

public validationStatus = () => this.props.validationStatus || this.state.validationStatus;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { isString, isUndefined } from 'lodash';
import { isNil, isString, isUndefined } from 'lodash';
import { Field, FastField, FieldProps, getIn, connect, FormikContext } from 'formik';

import {
Expand Down Expand Up @@ -42,6 +42,7 @@ export type IFormikFormFieldProps<T> = IFormikFieldProps<T> & ICommonFormFieldPr
type IFormikFormFieldImplProps<T> = IFormikFormFieldProps<T> & { formik: FormikContext<T> };

const ifString = (val: any): string => (isString(val) ? val : undefined);
const firstDefinedNode = (...values: React.ReactNode[]): React.ReactNode => values.find(val => !isNil(val));

export class FormikFormFieldImpl<T = any>
extends React.Component<IFormikFormFieldImplProps<T>, IFormikFormFieldImplState>
Expand Down Expand Up @@ -79,7 +80,7 @@ export class FormikFormFieldImpl<T = any>

public validationMessage = () => {
const { name, formik, validationMessage } = this.props;
return ifString(validationMessage) || getIn(formik.errors, name);
return firstDefinedNode(validationMessage, getIn(formik.errors, name));
};

public validationStatus = () => {
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/modules/core/src/presentation/forms/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { FieldProps } from 'formik';

import { IValidator } from './validation';
export type IFieldValidationStatus = 'error' | 'warning' | 'message';
export type IFieldValidationStatus = 'success' | 'none' | 'error' | 'warning' | 'message';

/** These props are used by FormField and FormikFormField components */
export interface IFieldLayoutPropsWithoutInput extends IValidationProps {
Expand Down Expand Up @@ -57,6 +57,6 @@ export interface IFormFieldApi {
label(): string;
value(): any;
touched(): boolean;
validationMessage(): string;
validationMessage(): React.ReactNode;
validationStatus(): IFieldValidationStatus;
}

0 comments on commit e6d1586

Please sign in to comment.