import * as React from 'react' import { flow, filter, take } from 'lodash/fp' const getFirstError = (stateErrors: Array, fieldErrors: Array) => { // Convert the state errors to a set for constant-time membership checks const errorSet = new Set(stateErrors) // get the first error that matches in the field error set, lazily return flow( filter((e: string) => errorSet.has(e)), take(1) )(fieldErrors) } interface ErrorProps { stateErrors: Array fieldErrors: Array ignoreErrors?: boolean modal?: boolean } export const Errors = ({ stateErrors, fieldErrors, ignoreErrors = false, modal = false }: ErrorProps) => { const errorMsg = ignoreErrors || getFirstError(stateErrors, fieldErrors)[0] const classPostfix = modal ? 'modal-field-error' : 'field-error' return errorMsg ? (
{ errorMsg }
) : (
) }