import { useAppConfig } from '@/components/AppStateProvider'; import { useTheme } from '@emotion/react'; import { FormHelperText, InputLabel, Stack, SxProps } from '@mui/material'; import React, { PropsWithChildren } from 'react'; import { FieldTitle, useTranslate } from 'react-admin'; import { useFormContext } from 'react-hook-form'; /** * Allows you to display an array of inputs with a label and a helper text with the Mantis theme style. */ function LabeledArrayInput({ label, children, display = 'label', helperText, sx, addLabel, divider = false, fullWidth, ...props }: LabeledArrayInputProps): React.ReactElement { const theme = useTheme() as any; const translate = useTranslate(); const { getCurrentDialog } = useAppConfig(); const { source, resource, isRequired } = props; const { getFieldState, formState } = useFormContext(); const { error } = getFieldState(source, formState); const dialogResource = getCurrentDialog(); return ( {display === 'label' && label !== false && addLabel !== false && ( )} {React.isValidElement(children) ? React.cloneElement(children, { // @ts-ignore ...children.props, ...props, label: display === 'legend' ? label : false }) : children} {display === 'label' && helperText ? ( {React.isValidElement(helperText) ? helperText : typeof helperText === 'string' ? translate(helperText, { _: helperText }) : helperText} ) : null} ); } type LabeledArrayInputProps = PropsWithChildren<{ label?: string | boolean; addLabel?: boolean; resource?: string; isRequired?: boolean; source: string; display?: 'legend' | 'label'; helperText?: string | boolean; divider?: boolean; sx?: SxProps; fullWidth?: boolean; }>; export { LabeledArrayInput }; export type { LabeledArrayInputProps };