Skip to content

Commit 8ab9bf0

Browse files
authored
fix rjsf-team#3696 - Playground fixes and light improvements (rjsf-team#3699)
* Fix rjsf-team#3696 - Fixed broken playground examples - Added experimental_defaultFormStateBehavior.emptyObjectFields control to Playground - Fixed bug where subthemes would not appear in Playground * Fix rjsf-team#3696 - Changes from review
1 parent 0bcac21 commit 8ab9bf0

File tree

10 files changed

+74
-43
lines changed

10 files changed

+74
-43
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ should change the heading of the (upcoming) version to include a major version b
5555
## Dev / docs / playground
5656

5757
- Updated sample data and documentation about the markdown in `RJSFSchema` description
58+
- Fixed broken playground examples ([#3696](https://github.com/rjsf-team/react-jsonschema-form/issues/3696))
59+
- Added experimental_defaultFormStateBehavior.emptyObjectFields control to Playground
60+
- Fixed bug where subthemes would not appear in Playground
5861

5962
# 5.7.3
6063

packages/docs/docs/api-reference/form-props.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ The following sub-sections represent the different keys in this object, with the
7272
| `populate` | Legacy behavior - populate minItems entries with default values initially and include empty array when no values have been defined |
7373
| `requiredOnly` | Ignore `minItems` on a field when calculating defaults unless the field is required |
7474

75-
### `emptyObjectFields
75+
### `emptyObjectFields`
7676

7777
| Flag Value | Description |
7878
| -------------------------- | -------------------------------------------------------------------------------------------------------------------------- |

packages/docs/docs/usage/validation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ node compileYourSchema.js
7171

7272
> NOTE: You must have your schema provided within a file that can be parsed and turned into the set of precompiled validator functions.
7373
74-
> Additional Note: If you are using Webpack or NextJS and are encountering an issue resolving `fs` during development, consult this [blog post](https://bobbyhadz.com/blog/module-not-found-cant-resolve-fs) for how to solve the issue.
74+
> Additional Note: If you are using Webpack or NextJS and are encountering an issue resolving `fs` during development, consult this [blog post](https://bobbyhadz.com/blog/module-not-found-cant-resolve-fs) for how to solve the issue.
7575
7676
### Using the precompiled validator
7777

@@ -435,7 +435,7 @@ An important note is that these errors are "display only" and will not block the
435435

436436
### ajvOptionsOverrides
437437

438-
In version 5, with the advent of the decoupling of the validation implementation from the `Form`, it is now possible to provide additional options to the `ajv6` instance used within `@rjsf/validator-ajv8`.
438+
In version 5, with the advent of the decoupling of the validation implementation from the `Form`, it is now possible to provide additional options to the `ajv` instance used within `@rjsf/validator-ajv8`.
439439
For instance, if you need more information from `ajv` about errors via the `verbose` option, now you can turn it on!
440440

441441
```tsx

packages/playground/src/components/Header.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,30 @@ const liveSettingsSchema: RJSFSchema = {
8686
},
8787
],
8888
},
89+
emptyObjectFields: {
90+
type: 'string',
91+
title: 'Object fields default behavior',
92+
default: 'populateAllDefaults',
93+
oneOf: [
94+
{
95+
type: 'string',
96+
title:
97+
'Assign value to formData when default is primitive, non-empty object field, or is required (legacy behavior)',
98+
enum: ['populateAllDefaults'],
99+
},
100+
{
101+
type: 'string',
102+
title:
103+
'Assign value to formData when default is an object and parent is required, or default is primitive and is required',
104+
enum: ['populateRequiredDefaults'],
105+
},
106+
{
107+
type: 'string',
108+
title: 'Does not set defaults',
109+
enum: ['skipDefaults'],
110+
},
111+
],
112+
},
89113
},
90114
},
91115
},
@@ -213,7 +237,7 @@ export default function Header({
213237
</div>
214238
<div className='col-sm-2'>
215239
<ThemeSelector themes={themes} theme={theme} select={onThemeSelected} />
216-
{themes[theme] && themes[theme].subthemes && subtheme && (
240+
{themes[theme] && themes[theme].subthemes && (
217241
<SubthemeSelector subthemes={themes[theme].subthemes!} subtheme={subtheme} select={onSubthemeSelected} />
218242
)}
219243
<ValidatorSelector validators={validators} validator={validator} select={onValidatorSelected} />

packages/playground/src/components/Playground.tsx

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
import { useCallback, useState, useRef, useEffect, ComponentType, FormEvent } from 'react';
22
import { withTheme, IChangeEvent, FormProps } from '@rjsf/core';
3-
import {
4-
ErrorSchema,
5-
ArrayFieldTemplateProps,
6-
ObjectFieldTemplateProps,
7-
RJSFSchema,
8-
RJSFValidationError,
9-
TemplatesType,
10-
UiSchema,
11-
ValidatorType,
12-
} from '@rjsf/utils';
3+
import { ErrorSchema, RJSFSchema, RJSFValidationError, TemplatesType, UiSchema, ValidatorType } from '@rjsf/utils';
134

145
import { samples } from '../samples';
156
import Header, { LiveSettings } from './Header';
@@ -44,11 +35,10 @@ export default function Playground({ themes, validators }: PlaygroundProps) {
4435
readonly: false,
4536
omitExtraData: false,
4637
liveOmit: false,
47-
experimental_defaultFormStateBehavior: { arrayMinItems: 'populate' },
38+
experimental_defaultFormStateBehavior: { arrayMinItems: 'populate', emptyObjectFields: 'populateAllDefaults' },
4839
});
4940
const [FormComponent, setFormComponent] = useState<ComponentType<FormProps>>(withTheme({}));
50-
const [ArrayFieldTemplate, setArrayFieldTemplate] = useState<ComponentType<ArrayFieldTemplateProps>>();
51-
const [ObjectFieldTemplate, setObjectFieldTemplate] = useState<ComponentType<ObjectFieldTemplateProps>>();
41+
const [templates, setTemplates] = useState<Partial<TemplatesType>>();
5242

5343
const playGroundFormRef = useRef<any>(null);
5444

@@ -65,7 +55,7 @@ export default function Playground({ themes, validators }: PlaygroundProps) {
6555
const load = useCallback(
6656
(data: any) => {
6757
// Reset the ArrayFieldTemplate whenever you load new data
68-
const { ArrayFieldTemplate, ObjectFieldTemplate, extraErrors, liveSettings } = data;
58+
const { templates = {}, extraErrors, liveSettings } = data;
6959
// uiSchema is missing on some examples. Provide a default to
7060
// clear the field in all cases.
7161
const { schema, uiSchema = {}, formData, theme: dataTheme = theme } = data;
@@ -79,9 +69,7 @@ export default function Playground({ themes, validators }: PlaygroundProps) {
7969
setFormData(formData);
8070
setExtraErrors(extraErrors);
8171
setTheme(dataTheme);
82-
setArrayFieldTemplate(ArrayFieldTemplate);
83-
setObjectFieldTemplate(ObjectFieldTemplate);
84-
setLiveSettings(liveSettings);
72+
setTemplates(templates);
8573
setShowForm(true);
8674
setLiveSettings(liveSettings);
8775
},
@@ -127,14 +115,6 @@ export default function Playground({ themes, validators }: PlaygroundProps) {
127115
window.alert('Form submitted');
128116
}, []);
129117

130-
const templates: Partial<TemplatesType> = {};
131-
if (ArrayFieldTemplate) {
132-
templates.ArrayFieldTemplate = ArrayFieldTemplate;
133-
}
134-
if (ObjectFieldTemplate) {
135-
templates.ObjectFieldTemplate = ObjectFieldTemplate;
136-
}
137-
138118
return (
139119
<>
140120
<Header

packages/playground/src/components/SubthemeSelector.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export interface SubthemesType {
1616
}
1717

1818
interface SubthemeSelectorProps {
19-
subtheme: string;
19+
subtheme: string | null;
2020
subthemes: SubthemesType;
2121
select: (subthemeName: string, subtheme: SubthemeType) => void;
2222
}
@@ -25,6 +25,7 @@ export default function SubthemeSelector({ subtheme, subthemes, select }: Subthe
2525
const schema: RJSFSchema = useMemo(
2626
() => ({
2727
type: 'string',
28+
title: 'Subtheme',
2829
enum: Object.keys(subthemes),
2930
}),
3031
[subthemes]

packages/playground/src/components/ThemeSelector.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ interface ThemeSelectorProps {
1818
export default function ThemeSelector({ theme, themes, select }: ThemeSelectorProps) {
1919
const schema: RJSFSchema = {
2020
type: 'string',
21+
title: 'Theme',
2122
enum: Object.keys(themes),
2223
};
2324

packages/playground/src/components/ValidatorSelector.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ interface ValidatorSelectorProps {
1111
export default function ValidatorSelector({ validator, validators, select }: ValidatorSelectorProps) {
1212
const schema: RJSFSchema = {
1313
type: 'string',
14+
title: 'Validator',
1415
enum: Object.keys(validators),
1516
};
1617

packages/playground/src/samples/customArray.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ export default {
4343
},
4444
},
4545
formData: ['react', 'jsonschema', 'form'],
46-
ArrayFieldTemplate,
46+
templates: { ArrayFieldTemplate },
4747
};

packages/playground/src/samples/customObject.tsx

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,48 @@
1-
const ObjectFieldTemplate: React.FC<{
2-
TitleField: React.FC<{ title: string }>;
3-
title: string;
4-
properties: any[];
5-
description: string;
6-
}> = ({ TitleField, properties, title, description }) => {
1+
import {
2+
getTemplate,
3+
getUiOptions,
4+
titleId,
5+
StrictRJSFSchema,
6+
RJSFSchema,
7+
FormContextType,
8+
ObjectFieldTemplateProps,
9+
} from '@rjsf/utils';
10+
11+
function ObjectFieldTemplate<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
12+
props: ObjectFieldTemplateProps<T, S, F>
13+
) {
14+
const { registry, properties, title, description, uiSchema, required, schema, idSchema } = props;
15+
const options = getUiOptions<T, S, F>(uiSchema);
16+
const TitleFieldTemplate = getTemplate<'TitleFieldTemplate', T, S, F>('TitleFieldTemplate', registry, options);
717
return (
818
<div>
9-
<TitleField title={title} />
19+
{title && (
20+
<TitleFieldTemplate
21+
id={titleId<T>(idSchema)}
22+
title={title}
23+
required={required}
24+
schema={schema}
25+
uiSchema={uiSchema}
26+
registry={registry}
27+
/>
28+
)}{' '}
29+
{description}
1030
<div className='row'>
1131
{properties.map((prop) => (
12-
<div className='col-lg-2 col-md-4 col-sm-6 col-xs-12' key={prop.content.key}>
32+
<div className='col-lg-1 col-md-2 col-sm-4 col-xs-6' key={prop.content.key}>
1333
{prop.content}
1434
</div>
1535
))}
1636
</div>
17-
{description}
1837
</div>
1938
);
20-
};
39+
}
2140

2241
export default {
2342
schema: {
2443
title: 'A registration form',
2544
description:
26-
'This is the same as the simple form, but it is rendered as a bootstrap grid. Try shrinking the browser window to see it in action.',
45+
'This is the same as the simple form, but with an altered bootstrap grid. Set the theme to default, and try shrinking the browser window to see it in action.',
2746
type: 'object',
2847
required: ['firstName', 'lastName'],
2948
properties: {
@@ -62,5 +81,7 @@ export default {
6281
bio: 'Roundhouse kicking asses since 1940',
6382
password: 'noneed',
6483
},
65-
ObjectFieldTemplate,
84+
templates: {
85+
ObjectFieldTemplate,
86+
},
6687
};

0 commit comments

Comments
 (0)