import * as React from 'react'; import guid from '../utils/Guid'; import { FormValidationContextProps, FormComponentContextState, } from './FormValidationContext'; import { Button } from './../button/Button'; import classNames from 'classnames'; import * as ArrowIcon from 'material-design-icons/maps/svg/production/ic_my_location_24px.svg'; import { withFormValidationContext } from './withFormValidationContext'; export interface FormValidationSummaryComponentProps { componentsWithErrors: FormComponentContextState[]; } export type FormValidationSummaryProps = { containerClassName?: string; headerClassName?: string; footerClassName?: string; title?: string | JSX.Element; footer?: string | JSX.Element; Component?: | React.ComponentType | React.FC; } & FormValidationContextProps; export interface FormValidationSummaryState {} export const DefaultComponent: React.FC = ( props ) => ( {props.componentsWithErrors.map((component, index) => { return (
{component.validation.name ? component.validation.name : 'unknown'}
{component.validation.errors && component.validation.errors.map((err, errIndex) => ( {err} ))}
{component.componentApi && component.componentApi.focus && component.componentApi.scrollTo && ( )}
); })} ); export class FormValidationSummaryRaw extends React.PureComponent< FormValidationSummaryProps, FormValidationSummaryState > { public static defaultProps = { title: 'Hang on', footer: '... needs fixing', Component: DefaultComponent, containerClassName: 'formValidationSummary', headerClassName: 'formValidationSummary__header', footerClassName: 'formValidationSummary__footer', }; public componentId = guid(); public render() { const componentsWithErrors = this.props.formValidationContext ? Object.keys(this.props.formValidationContext.components) .map((key) => this.props.formValidationContext.components[key]) .filter((component) => { if (!component.validation.isValid && !component.validation.name) { console.warn( component, 'Has validation error. FormValidationSummary is present but the component has no validation name' ); } return !component.validation.isValid; }) : []; const containerClassName = classNames(this.props.containerClassName); const headerClassName = classNames(this.props.headerClassName); const footerClassName = classNames(this.props.footerClassName); return ( componentsWithErrors.length > 0 && (
{this.props.title && (
{this.props.title}
)} {this.props.footer && (
{this.props.footer}
)}
) ); } } export const FormValidationSummary = withFormValidationContext( FormValidationSummaryRaw );