Skip to content

Commit b1e8068

Browse files
committed
Added multiple usecallback hooks and multischemafieldtemplate for mantine
1 parent a3a56f7 commit b1e8068

File tree

8 files changed

+68
-18
lines changed

8 files changed

+68
-18
lines changed

packages/mantine/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"peerDependencies": {
6464
"@rjsf/core": "^6.0.0-beta",
6565
"@rjsf/utils": "^6.0.0-beta",
66-
"react": "^16.14.0 || >=17",
66+
"react": ">=18",
6767
"@mantine/core": ">=7",
6868
"@mantine/hooks": ">=7",
6969
"@mantine/dates": ">=7"
@@ -109,4 +109,4 @@
109109
"url": "git+https://github.com/rjsf-team/react-jsonschema-form.git"
110110
},
111111
"license": "Apache-2.0"
112-
}
112+
}

packages/mantine/src/templates/BaseInputTemplate.tsx

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChangeEvent, FocusEvent } from 'react';
1+
import { ChangeEvent, FocusEvent, useCallback } from 'react';
22
import {
33
ariaDescribedByIds,
44
BaseInputTemplateProps,
@@ -48,16 +48,30 @@ export default function BaseInputTemplate<
4848
const inputProps = getInputProps<T, S, F>(schema, type, options, false);
4949
const themeProps = cleanupOptions(options);
5050

51-
const handleNumberChange = (value: number | string) => onChange(value);
51+
const handleNumberChange = useCallback((value: number | string) => onChange(value), [onChange]);
5252

53-
const handleChange = onChangeOverride
54-
? onChangeOverride
55-
: (e: ChangeEvent<HTMLInputElement>) =>
56-
onChange(e.target.value === '' ? (options.emptyValue ?? '') : e.target.value);
53+
const handleChange = useCallback(
54+
(e: ChangeEvent<HTMLInputElement>) => {
55+
const handler = onChangeOverride ? onChangeOverride : onChange;
56+
const value = e.target.value === '' ? (options.emptyValue ?? '') : e.target.value;
57+
handler(value);
58+
},
59+
[onChange, onChangeOverride, options],
60+
);
5761

58-
const handleBlur = (e: FocusEvent<HTMLInputElement>) => onBlur(id, e.target && e.target.value);
62+
const handleBlur = useCallback(
63+
(e: FocusEvent<HTMLInputElement>) => {
64+
onBlur(id, e.target && e.target.value);
65+
},
66+
[onBlur, id],
67+
);
5968

60-
const handleFocus = (e: FocusEvent<HTMLInputElement>) => onFocus(id, e.target && e.target.value);
69+
const handleFocus = useCallback(
70+
(e: FocusEvent<HTMLInputElement>) => {
71+
onFocus(id, e.target && e.target.value);
72+
},
73+
[onFocus, id],
74+
);
6175

6276
const input =
6377
inputProps.type === 'number' || inputProps.type === 'integer' ? (
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Stack } from '@mantine/core';
2+
import { FormContextType, MultiSchemaFieldTemplateProps, RJSFSchema, StrictRJSFSchema } from '@rjsf/utils';
3+
4+
export default function MultiSchemaFieldTemplate<
5+
T = any,
6+
S extends StrictRJSFSchema = RJSFSchema,
7+
F extends FormContextType = any,
8+
>({ selector, optionSchemaField }: MultiSchemaFieldTemplateProps<T, S, F>) {
9+
return (
10+
<Stack style={{ marginBottom: '1rem' }}>
11+
{selector}
12+
{optionSchemaField}
13+
</Stack>
14+
);
15+
}

packages/mantine/src/templates/ObjectFieldTemplate.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export default function ObjectFieldTemplate<
9393
id={buttonId<T>(idSchema, 'add')}
9494
disabled={disabled || readonly}
9595
onClick={onAddClick(schema)}
96-
className='object-property-expand'
96+
className='rjsf-object-property-expand'
9797
uiSchema={uiSchema}
9898
registry={registry}
9999
/>

packages/mantine/src/templates/WrapIfAdditionalTemplate.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FocusEvent } from 'react';
1+
import { FocusEvent, useCallback } from 'react';
22
import {
33
ADDITIONAL_PROPERTY_FLAG,
44
UI_OPTIONS_KEY,
@@ -42,6 +42,11 @@ export default function WrapIfAdditionalTemplate<
4242
const keyLabel = translateString(TranslatableString.KeyLabel, [label]);
4343
const additional = ADDITIONAL_PROPERTY_FLAG in schema;
4444

45+
const handleBlur = useCallback(
46+
({ target }: FocusEvent<HTMLInputElement>) => onKeyChange(target && target.value),
47+
[onKeyChange],
48+
);
49+
4550
if (!additional) {
4651
return (
4752
<div className={classNames} style={style}>
@@ -50,8 +55,6 @@ export default function WrapIfAdditionalTemplate<
5055
);
5156
}
5257

53-
const handleBlur = ({ target }: FocusEvent<HTMLInputElement>) => onKeyChange(target && target.value);
54-
5558
// The `block` prop is not part of the `IconButtonProps` defined in the template, so put it into the uiSchema instead
5659
const uiOptions = uiSchema ? uiSchema[UI_OPTIONS_KEY] : {};
5760
const buttonUiOptions = {

packages/mantine/src/templates/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import GridTemplate from './GridTemplate';
1414
import ObjectFieldTemplate from './ObjectFieldTemplate';
1515
import TitleField from './TitleField';
1616
import WrapIfAdditionalTemplate from './WrapIfAdditionalTemplate';
17+
import MultiSchemaFieldTemplate from './MultiSchemaFieldTemplate';
1718

1819
export function generateTemplates<
1920
T = any,
@@ -35,6 +36,7 @@ export function generateTemplates<
3536
ObjectFieldTemplate,
3637
TitleFieldTemplate: TitleField,
3738
WrapIfAdditionalTemplate,
39+
MultiSchemaFieldTemplate,
3840
};
3941
}
4042

packages/mantine/src/widgets/DateTime/DateTimeInput.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,17 @@ export default function DateTimeInput<
6060
[onChange, valueFormat],
6161
);
6262

63-
const handleBlur = () => onBlur && onBlur(id, value);
63+
const handleBlur = useCallback(() => {
64+
if (onBlur) {
65+
onBlur(id, value);
66+
}
67+
}, [onBlur, id, value]);
6468

65-
const handleFocus = () => onFocus && onFocus(id, value);
69+
const handleFocus = useCallback(() => {
70+
if (onFocus) {
71+
onFocus(id, value);
72+
}
73+
}, [onFocus, id, value]);
6674

6775
return (
6876
<DateInput

packages/mantine/src/widgets/RangeWidget.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,17 @@ export default function RangeWidget<T = any, S extends StrictRJSFSchema = RJSFSc
5050
[onChange, disabled, readonly],
5151
);
5252

53-
const handleBlur = () => onBlur && onBlur(id, value);
53+
const handleBlur = useCallback(() => {
54+
if (onBlur) {
55+
onBlur(id, value);
56+
}
57+
}, [onBlur, id, value]);
5458

55-
const handleFocus = () => onFocus && onFocus(id, value);
59+
const handleFocus = useCallback(() => {
60+
if (onFocus) {
61+
onFocus(id, value);
62+
}
63+
}, [onFocus, id, value]);
5664

5765
return (
5866
<>

0 commit comments

Comments
 (0)