import { isArray, isObject, isString } from 'lodash'; import React from 'react'; import { WizardPage } from './WizardPage'; import { Tooltip } from '../../presentation/Tooltip'; interface IWizardStepLabelProps { current: boolean; onClick: (page: WizardPage) => void; page: WizardPage; } const flattenErrors = (errors: any) => { const traverse = (obj: any, path: string, flattenedErrors: { [key: string]: any }): any => { if (isArray(obj)) { obj.forEach((elem, idx) => traverse(elem, `${path}[${idx}]`, flattenedErrors)); } else if (isString(obj)) { flattenedErrors[path] = obj; } else if (isObject(obj)) { Object.keys(obj).forEach((key) => traverse(obj[key], `${path}.${key}`, flattenedErrors)); } return flattenedErrors; }; return traverse(errors, 'errors', {}); }; export class WizardStepLabel extends React.Component { public render() { const { current, page, onClick } = this.props; const { errors, status } = page.state; const { label } = page.props; const flattenedErrors: { [field: string]: string } = flattenErrors(errors); const hasErrors = !!Object.keys(flattenedErrors).length; const className = `${WizardPage.getStatusClass(status)} ${current ? 'current' : ''}`; const pageLabel = (
  • onClick(page)}> {label}
  • ); if (!hasErrors) { return pageLabel; } const Errors = ( {Object.keys(flattenedErrors).map((key) => ( {flattenedErrors[key]}
    ))}
    ); return {pageLabel}; } }