Skip to content

Commit 22890af

Browse files
committed
Review Mantine theme for the rjsf-v6
- Review all templates and widgets - Add FileWidget - Add GridTemplate
1 parent 49256f7 commit 22890af

File tree

9 files changed

+270
-47
lines changed

9 files changed

+270
-47
lines changed

packages/mantine/src/templates/ArrayFieldItemTemplate.tsx

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,46 @@
1-
import { ArrayFieldTemplateItemType, FormContextType, RJSFSchema, StrictRJSFSchema } from '@rjsf/utils';
1+
import {
2+
ArrayFieldItemTemplateType,
3+
FormContextType,
4+
getTemplate,
5+
getUiOptions,
6+
RJSFSchema,
7+
StrictRJSFSchema,
8+
} from '@rjsf/utils';
29
import { Box, Flex, Group } from '@mantine/core';
310

411
/** The `ArrayFieldItemTemplate` component is the template used to render an items of an array.
512
*
6-
* @param props - The `ArrayFieldTemplateItemType` props for the component
13+
* @param props - The `ArrayFieldItemTemplateType` props for the component
714
*/
815
export default function ArrayFieldItemTemplate<
916
T = any,
1017
S extends StrictRJSFSchema = RJSFSchema,
1118
F extends FormContextType = any
12-
>(props: ArrayFieldTemplateItemType<T, S, F>) {
13-
const {
14-
disabled,
15-
className,
16-
hasCopy,
17-
hasMoveDown,
18-
hasMoveUp,
19-
hasRemove,
20-
hasToolbar,
21-
index,
22-
onCopyIndexClick,
23-
onDropIndexClick,
24-
onReorderClick,
25-
readonly,
26-
uiSchema,
19+
>(props: ArrayFieldItemTemplateType<T, S, F>) {
20+
const { buttonsProps, className, hasToolbar, index, uiSchema, registry, children } = props;
21+
const uiOptions = getUiOptions<T, S, F>(uiSchema);
22+
const ArrayFieldItemButtonsTemplate = getTemplate<'ArrayFieldItemButtonsTemplate', T, S, F>(
23+
'ArrayFieldItemButtonsTemplate',
2724
registry,
28-
children,
29-
} = props;
30-
const { CopyButton, MoveDownButton, MoveUpButton, RemoveButton } = registry.templates.ButtonTemplates;
25+
uiOptions
26+
);
3127

3228
return (
3329
<Box key={`array-item-${index}`} className={className || 'array-item'} mb='xs'>
3430
<Flex gap='xs' align='end' justify='center'>
3531
<Box w='100%'>{children}</Box>
3632
{hasToolbar && (
37-
<Group wrap='nowrap' gap={2}>
38-
{(hasMoveUp || hasMoveDown) && (
33+
<Group wrap='nowrap' gap={2} mb={7}>
34+
<ArrayFieldItemButtonsTemplate {...buttonsProps} />
35+
</Group>
36+
)}
37+
</Flex>
38+
</Box>
39+
);
40+
}
41+
42+
/*
43+
{(hasMoveUp || hasMoveDown) && (
3944
<MoveUpButton
4045
iconType='sm'
4146
className='array-item-move-up'
@@ -75,9 +80,4 @@ export default function ArrayFieldItemTemplate<
7580
registry={registry}
7681
/>
7782
)}
78-
</Group>
79-
)}
80-
</Flex>
81-
</Box>
82-
);
83-
}
83+
*/

packages/mantine/src/templates/ArrayFieldTemplate.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
getTemplate,
33
getUiOptions,
44
ArrayFieldTemplateProps,
5-
ArrayFieldTemplateItemType,
5+
ArrayFieldItemTemplateType,
66
FormContextType,
77
RJSFSchema,
88
StrictRJSFSchema,
@@ -11,7 +11,7 @@ import { Fieldset, Box, Group } from '@mantine/core';
1111

1212
/** The `ArrayFieldTemplate` component is the template used to render all items in an array.
1313
*
14-
* @param props - The `ArrayFieldTemplateItemType` props for the component
14+
* @param props - The `ArrayFieldItemTemplateType` props for the component
1515
*/
1616
export default function ArrayFieldTemplate<
1717
T = any,
@@ -79,7 +79,7 @@ export default function ArrayFieldTemplate<
7979

8080
<Box className='row array-item-list'>
8181
{items &&
82-
items.map(({ key, ...itemProps }: ArrayFieldTemplateItemType<T, S, F>) => (
82+
items.map(({ key, ...itemProps }: ArrayFieldItemTemplateType<T, S, F>) => (
8383
<ArrayFieldItemTemplate key={key} {...itemProps} />
8484
))}
8585
</Box>
@@ -92,6 +92,7 @@ export default function ArrayFieldTemplate<
9292
onClick={onAddClick}
9393
uiSchema={uiSchema}
9494
registry={registry}
95+
iconType='md'
9596
/>
9697
</Group>
9798
)}

packages/mantine/src/templates/BaseInputTemplate.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default function BaseInputTemplate<
3535
onFocus,
3636
options,
3737
rawErrors,
38+
children,
3839
} = props;
3940

4041
const inputProps = getInputProps<T, S, F>(schema, type, options, false);
@@ -97,6 +98,7 @@ export default function BaseInputTemplate<
9798
return (
9899
<>
99100
{input}
101+
{children}
100102
{Array.isArray(schema.examples) && (
101103
<datalist id={examplesId<T>(id)}>
102104
{(schema.examples as string[])

packages/mantine/src/templates/ButtonTemplates/IconButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type MantineIconButtonProps<
1212
export default function IconButton<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
1313
props: MantineIconButtonProps<T, S, F>
1414
) {
15-
const { icon, iconType, color, onClick, uiSchema, registry, ...otherProps } = props;
15+
const { icon, iconType = 'sm', color, onClick, uiSchema, registry, ...otherProps } = props;
1616
return (
1717
<ActionIcon
1818
size={iconType as ActionIconProps['size']}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { GridTemplateProps } from '@rjsf/utils';
2+
import { Grid } from '@mantine/core';
3+
4+
/** Renders a `GridTemplate` for mantine, which is expecting the column sizing information coming in via the
5+
* extra props provided by the caller, which are spread directly on the `Grid`/`Grid.Col`.
6+
*
7+
* @param props - The GridTemplateProps, including the extra props containing the chakra-ui grid positioning details
8+
*/
9+
export default function GridTemplate(props: GridTemplateProps) {
10+
const { children, column, ...rest } = props;
11+
if (column) {
12+
return <Grid.Col {...rest}>{children}</Grid.Col>;
13+
}
14+
return (
15+
<Grid grow {...rest}>
16+
{children}
17+
</Grid>
18+
);
19+
}

packages/mantine/src/templates/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import ButtonTemplates from './ButtonTemplates';
1010
import FieldErrorTemplate from './FieldErrorTemplate';
1111
import FieldTemplate from './FieldTemplate';
1212
import FieldHelpTemplate from './FieldHelpTemplate';
13+
import GridTemplate from './GridTemplate';
1314
import ObjectFieldTemplate from './ObjectFieldTemplate';
1415
import TitleField from './TitleField';
1516
import WrapIfAdditionalTemplate from './WrapIfAdditionalTemplate';
@@ -30,6 +31,7 @@ export function generateTemplates<
3031
FieldErrorTemplate,
3132
FieldTemplate,
3233
FieldHelpTemplate,
34+
GridTemplate,
3335
ObjectFieldTemplate,
3436
TitleFieldTemplate: TitleField,
3537
WrapIfAdditionalTemplate,

packages/mantine/src/widgets/CheckboxWidget.tsx

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import React, { useCallback } from 'react';
22
import {
3+
descriptionId,
4+
getTemplate,
35
StrictRJSFSchema,
46
RJSFSchema,
57
FormContextType,
@@ -25,15 +27,24 @@ export default function CheckboxWidget<
2527
autofocus,
2628
label,
2729
hideLabel,
30+
schema,
2831
rawErrors,
2932
options,
3033
onChange,
3134
onBlur,
3235
onFocus,
36+
registry,
37+
uiSchema,
3338
} = props;
3439

3540
const themeProps = cleanupOptions(options);
3641

42+
const DescriptionFieldTemplate = getTemplate<'DescriptionFieldTemplate', T, S, F>(
43+
'DescriptionFieldTemplate',
44+
registry,
45+
options
46+
);
47+
3748
const handleCheckboxChange = useCallback(
3849
(e: React.ChangeEvent<HTMLInputElement>) => {
3950
if (!disabled && !readonly && onChange) {
@@ -61,21 +72,33 @@ export default function CheckboxWidget<
6172
[onFocus, id]
6273
);
6374

75+
const description = options.description || schema.description;
6476
return (
65-
<Checkbox
66-
id={id}
67-
name={name}
68-
label={labelValue(label || undefined, hideLabel, false)}
69-
disabled={disabled || readonly}
70-
required={required}
71-
autoFocus={autofocus}
72-
checked={typeof value === 'undefined' ? false : value === 'true' || value}
73-
onChange={handleCheckboxChange}
74-
onBlur={handleBlur}
75-
onFocus={handleFocus}
76-
error={rawErrors && rawErrors.length > 0 ? rawErrors.join('\n') : undefined}
77-
aria-describedby={ariaDescribedByIds<T>(id)}
78-
{...themeProps}
79-
/>
77+
<>
78+
{!hideLabel && !!description && (
79+
<DescriptionFieldTemplate
80+
id={descriptionId<T>(id)}
81+
description={description}
82+
schema={schema}
83+
uiSchema={uiSchema}
84+
registry={registry}
85+
/>
86+
)}
87+
<Checkbox
88+
id={id}
89+
name={name}
90+
label={labelValue(label || undefined, hideLabel, false)}
91+
disabled={disabled || readonly}
92+
required={required}
93+
autoFocus={autofocus}
94+
checked={typeof value === 'undefined' ? false : value === 'true' || value}
95+
onChange={handleCheckboxChange}
96+
onBlur={handleBlur}
97+
onFocus={handleFocus}
98+
error={rawErrors && rawErrors.length > 0 ? rawErrors.join('\n') : undefined}
99+
aria-describedby={ariaDescribedByIds<T>(id)}
100+
{...themeProps}
101+
/>
102+
</>
80103
);
81104
}

0 commit comments

Comments
 (0)