import React from 'react' import type { BaseProps } from '@toptal/picasso-shared' import { Typography } from '@toptal/picasso-typography' import { Grid } from '@toptal/picasso-grid' import { Collapse } from '@toptal/picasso-collapse' import type { FieldRequirementItemStatus } from './FieldRequirementItem' import FieldRequirementItem from './FieldRequirementItem' import type { FieldRequirement } from './types' export interface Props extends BaseProps { /** A string that defines the title of the requirement list */ description?: string /** Value of the related input. It will be used to validate the requirements */ value?: TValueType /** Open/Close the requirements section. Opening it with focus is the default behavior */ open: boolean /** Indicate whether `PasswordInput` is in error state */ error?: boolean /** Duration for the collapse animation */ timeout?: number /** Array of object to specify requirements. They will be executed */ requirements: FieldRequirement[] testIds?: { root?: string description?: string gridContainer?: string } } const ANIMATION_TIMEOUT = 500 export const FieldRequirements = ({ value = '' as unknown as TValueType, description, open = false, error, timeout = ANIMATION_TIMEOUT, requirements, className, style, testIds, }: Props) => { return ( <> {description && ( {description} )} {requirements.map(requirement => { let status: FieldRequirementItemStatus = 'default' // eslint-disable-next-line @typescript-eslint/no-non-null-assertion if (requirement.validator(value)) { status = 'success' } else if (error) { status = 'error' } return ( {requirement.message} ) })} ) } FieldRequirements.displayName = 'FieldRequirements' export default FieldRequirements