Skip to content

Commit c4fde75

Browse files
committed
Add DescriptionFieldTemplate to mantine theme
1 parent 282b8b3 commit c4fde75

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { DescriptionFieldProps, FormContextType, RJSFSchema, StrictRJSFSchema } from '@rjsf/utils';
2+
import { Text } from '@mantine/core';
3+
4+
/** The `DescriptionField` is the template to use to render the description of a field
5+
*
6+
* @param props - The `DescriptionFieldProps` for this component
7+
*/
8+
export default function DescriptionField<
9+
T = any,
10+
S extends StrictRJSFSchema = RJSFSchema,
11+
F extends FormContextType = any
12+
>(props: DescriptionFieldProps<T, S, F>) {
13+
const { id, description } = props;
14+
if (description) {
15+
return (
16+
<Text id={id} mt={3} mb='sm'>
17+
{description}
18+
</Text>
19+
);
20+
}
21+
22+
return null;
23+
}

packages/mantine/src/templates/FieldTemplate.tsx

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,30 @@ import {
66
getTemplate,
77
getUiOptions,
88
} from '@rjsf/utils';
9-
import { Box } from '@mantine/core';
9+
import { Box, Text } from '@mantine/core';
1010

1111
export default function FieldTemplate<
1212
T = any,
1313
S extends StrictRJSFSchema = RJSFSchema,
1414
F extends FormContextType = any
1515
>(props: FieldTemplateProps<T, S, F>) {
16-
const { id, classNames, style, label, errors, help, hidden, schema, uiSchema, registry, children, ...otherProps } =
17-
props;
16+
const {
17+
id,
18+
classNames,
19+
style,
20+
label,
21+
errors,
22+
help,
23+
displayLabel,
24+
description,
25+
rawDescription,
26+
hidden,
27+
schema,
28+
uiSchema,
29+
registry,
30+
children,
31+
...otherProps
32+
} = props;
1833

1934
const uiOptions = getUiOptions<T, S, F>(uiSchema);
2035
const WrapIfAdditionalTemplate = getTemplate<'WrapIfAdditionalTemplate', T, S, F>(
@@ -39,8 +54,13 @@ export default function FieldTemplate<
3954
{...otherProps}
4055
>
4156
{children}
42-
{help}
57+
{displayLabel && rawDescription && (
58+
<Text size='sm' mt={5}>
59+
{description}
60+
</Text>
61+
)}
4362
{errors}
63+
{help}
4464
</WrapIfAdditionalTemplate>
4565
);
4666
}

packages/mantine/src/templates/ObjectFieldTemplate.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export default function ObjectFieldTemplate<
7171
cols={gridCols}
7272
spacing={gridSpacing as MantineSpacing | undefined}
7373
verticalSpacing={gridVerticalSpacing as MantineSpacing | undefined}
74+
mb='sm'
7475
>
7576
{properties
7677
.filter((e) => !e.hidden)

packages/mantine/src/templates/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ArrayFieldItemTemplate from './ArrayFieldItemTemplate';
44
import ArrayFieldTemplate from './ArrayFieldTemplate';
55
import ArrayFieldTitleTemplate from './ArrayFieldTitleTemplate';
66
import BaseInputTemplate from './BaseInputTemplate';
7+
import DescriptionField from './DescriptionField';
78
import ErrorList from './ErrorList';
89
import ButtonTemplates from './ButtonTemplates';
910
import FieldErrorTemplate from './FieldErrorTemplate';
@@ -24,6 +25,7 @@ export function generateTemplates<
2425
ArrayFieldTitleTemplate,
2526
BaseInputTemplate,
2627
ButtonTemplates: ButtonTemplates<T, S, F>(),
28+
DescriptionFieldTemplate: DescriptionField,
2729
ErrorListTemplate: ErrorList,
2830
FieldErrorTemplate,
2931
FieldTemplate,

0 commit comments

Comments
 (0)