| 1 |
import FormControl from '@mui/material/FormControl'; |
| 2 |
import Typography from '@mui/material/Typography'; |
| 3 |
import { |
| 4 |
FieldTemplateProps, |
| 5 |
FormContextType, |
| 6 |
RJSFSchema, |
| 7 |
StrictRJSFSchema, |
| 8 |
getTemplate, |
| 9 |
getUiOptions, |
| 10 |
} from '@rjsf/utils'; |
| 11 |
|
| 12 |
/** The `FieldTemplate` component is the template used by `SchemaField` to render any field. It renders the field |
| 13 |
* content, (label, description, children, errors and help) inside of a `WrapIfAdditional` component. |
| 14 |
* |
| 15 |
* @param props - The `FieldTemplateProps` for this component |
| 16 |
*/ |
| 17 |
export default function FieldTemplate< |
| 18 |
T = any, |
| 19 |
S extends StrictRJSFSchema = RJSFSchema, |
| 20 |
F extends FormContextType = any |
| 21 |
>(props: FieldTemplateProps<T, S, F>) { |
| 22 |
const { |
| 23 |
id, |
| 24 |
children, |
| 25 |
classNames, |
| 26 |
style, |
| 27 |
disabled, |
| 28 |
displayLabel, |
| 29 |
hidden, |
| 30 |
label, |
| 31 |
onDropPropertyClick, |
| 32 |
onKeyChange, |
| 33 |
readonly, |
| 34 |
required, |
| 35 |
rawErrors = [], |
| 36 |
errors, |
| 37 |
help, |
| 38 |
description, |
| 39 |
rawDescription, |
| 40 |
schema, |
| 41 |
uiSchema, |
| 42 |
registry, |
| 43 |
} = props; |
| 44 |
const uiOptions = getUiOptions<T, S, F>(uiSchema); |
| 45 |
const WrapIfAdditionalTemplate = getTemplate<'WrapIfAdditionalTemplate', T, S, F>( |
| 46 |
'WrapIfAdditionalTemplate', |
| 47 |
registry, |
| 48 |
uiOptions |
| 49 |
); |
| 50 |
|
| 51 |
if (hidden) { |
| 52 |
return <div style={{ display: 'none' }}>{children}</div>; |
| 53 |
} |
| 54 |
return ( |
| 55 |
<WrapIfAdditionalTemplate |
| 56 |
classNames={classNames} |
| 57 |
style={style} |
| 58 |
disabled={disabled} |
| 59 |
id={id} |
| 60 |
label={label} |
| 61 |
onDropPropertyClick={onDropPropertyClick} |
| 62 |
onKeyChange={onKeyChange} |
| 63 |
readonly={readonly} |
| 64 |
required={required} |
| 65 |
schema={schema} |
| 66 |
uiSchema={uiSchema} |
| 67 |
registry={registry} |
| 68 |
> |
| 69 |
<FormControl fullWidth={true} error={rawErrors.length ? true : false} required={required}> |
| 70 |
{children} |
| 71 |
{displayLabel && rawDescription ? ( |
| 72 |
<Typography variant='caption' color='textSecondary'> |
| 73 |
{description} |
| 74 |
</Typography> |
| 75 |
) : null} |
| 76 |
{errors} |
| 77 |
{help} |
| 78 |
</FormControl> |
| 79 |
</WrapIfAdditionalTemplate> |
| 80 |
); |
| 81 |
} |