Skip to content

Commit

Permalink
[fix] #3856
Browse files Browse the repository at this point in the history
- update `validation` example to use RJSF v5 API
- add types for all samples and fix/ignore uncovered type issues
- Spread sample data as extra form props
  • Loading branch information
nickgros committed Sep 8, 2023
1 parent b07eda3 commit 83f09f6
Show file tree
Hide file tree
Showing 37 changed files with 221 additions and 73 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ should change the heading of the (upcoming) version to include a major version b

- Fixed a faulty check of the `isMultiple` option in `MultiSchemaField`. It no longer offers multiple choice inside a select field in a `oneOf` case in Chakra UI, fixing [#3848](https://github.com/rjsf-team/react-jsonschema-form/issues/3848)

## Dev / docs / playground

- Fixed custom validation playground example ([#3856](https://github.com/rjsf-team/react-jsonschema-form/issues/3856))

# 5.12.1

## @rjsf/validator-ajv8
Expand Down
34 changes: 22 additions & 12 deletions packages/playground/src/components/Playground.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useState, useRef, useEffect, ComponentType, FormEvent } from 'react';
import { withTheme, IChangeEvent, FormProps } from '@rjsf/core';
import { ErrorSchema, RJSFSchema, RJSFValidationError, TemplatesType, UiSchema, ValidatorType } from '@rjsf/utils';
import { ComponentType, FormEvent, useCallback, useEffect, useRef, useState } from 'react';
import { FormProps, IChangeEvent, withTheme } from '@rjsf/core';
import { ErrorSchema, RJSFSchema, RJSFValidationError, UiSchema, ValidatorType } from '@rjsf/utils';

import { samples } from '../samples';
import Header, { LiveSettings } from './Header';
Expand All @@ -10,6 +10,7 @@ import GeoPosition from './GeoPosition';
import { ThemesType } from './ThemeSelector';
import Editors from './Editors';
import SpecialInput from './SpecialInput';
import { Sample } from '../samples/Sample';

export interface PlaygroundProps {
themes: { [themeName: string]: ThemesType };
Expand Down Expand Up @@ -39,7 +40,7 @@ export default function Playground({ themes, validators }: PlaygroundProps) {
experimental_defaultFormStateBehavior: { arrayMinItems: 'populate', emptyObjectFields: 'populateAllDefaults' },
});
const [FormComponent, setFormComponent] = useState<ComponentType<FormProps>>(withTheme({}));
const [templates, setTemplates] = useState<Partial<TemplatesType>>();
const [otherFormProps, setOtherFormProps] = useState<Partial<FormProps>>({});

const playGroundFormRef = useRef<any>(null);

Expand All @@ -54,12 +55,21 @@ export default function Playground({ themes, validators }: PlaygroundProps) {
);

const load = useCallback(
(data: any) => {
// Reset the ArrayFieldTemplate whenever you load new data
const { templates = {}, extraErrors, liveSettings } = data;
// uiSchema is missing on some examples. Provide a default to
// clear the field in all cases.
const { schema, uiSchema = {}, formData, theme: dataTheme = theme } = data;
(data: Sample & { theme: string; liveSettings: LiveSettings }) => {
const {
schema,
// uiSchema is missing on some examples. Provide a default to
// clear the field in all cases.
uiSchema = {},
// Always reset templates and fields
templates = {},
fields = {},
formData,
theme: dataTheme = theme,
extraErrors,
liveSettings,
...rest
} = data;

onThemeSelected(dataTheme, themes[dataTheme]);

Expand All @@ -70,9 +80,9 @@ export default function Playground({ themes, validators }: PlaygroundProps) {
setFormData(formData);
setExtraErrors(extraErrors);
setTheme(dataTheme);
setTemplates(templates);
setShowForm(true);
setLiveSettings(liveSettings);
setOtherFormProps({ fields, templates, ...rest });
},
[theme, onThemeSelected, themes]
);
Expand Down Expand Up @@ -175,8 +185,8 @@ export default function Playground({ themes, validators }: PlaygroundProps) {
theme={theme}
>
<FormComponent
{...otherFormProps}
{...liveSettings}
templates={templates}
extraErrors={extraErrors}
schema={schema}
uiSchema={uiSchema}
Expand Down
3 changes: 3 additions & 0 deletions packages/playground/src/samples/Sample.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { FormProps } from '@rjsf/core';

export type Sample = Omit<FormProps, 'validator'>;
6 changes: 5 additions & 1 deletion packages/playground/src/samples/additionalProperties.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default {
import { Sample } from './Sample';

const additionalProperties: Sample = {
schema: {
title: 'A customizable registration form',
description: 'A simple form with additional properties example.',
Expand Down Expand Up @@ -30,3 +32,5 @@ export default {
assKickCount: 'infinity',
},
};

export default additionalProperties;
6 changes: 5 additions & 1 deletion packages/playground/src/samples/allOf.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default {
import { Sample } from './Sample';

const allOf: Sample = {
schema: {
type: 'object',
allOf: [
Expand All @@ -24,3 +26,5 @@ export default {
},
formData: {},
};

export default allOf;
6 changes: 5 additions & 1 deletion packages/playground/src/samples/alternatives.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default {
import { Sample } from './Sample';

const alternatives: Sample = {
schema: {
definitions: {
Color: {
Expand Down Expand Up @@ -90,3 +92,5 @@ export default {
blendMode: 'screen',
},
};

export default alternatives;
6 changes: 5 additions & 1 deletion packages/playground/src/samples/anyOf.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default {
import { Sample } from './Sample';

const anyOf: Sample = {
schema: {
type: 'object',
properties: {
Expand Down Expand Up @@ -57,3 +59,5 @@ export default {
},
formData: {},
};

export default anyOf;
6 changes: 5 additions & 1 deletion packages/playground/src/samples/arrays.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default {
import { Sample } from './Sample';

const arrays: Sample = {
schema: {
definitions: {
Thing: {
Expand Down Expand Up @@ -189,3 +191,5 @@ export default {
fixedNoToolbar: [42, true, 'additional item one', 'additional item two'],
},
};

export default arrays;
6 changes: 5 additions & 1 deletion packages/playground/src/samples/custom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default {
import { Sample } from './Sample';

const custom: Sample = {
schema: {
title: 'A localisation form',
type: 'object',
Expand All @@ -20,3 +22,5 @@ export default {
lon: 0,
},
};

export default custom;
17 changes: 9 additions & 8 deletions packages/playground/src/samples/customArray.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const ArrayFieldTemplate: React.FC<{ className: string; items?: any[]; canAdd?: boolean; onAddClick: () => void }> = ({
className,
items,
canAdd,
onAddClick,
}) => {
import { Sample } from './Sample';
import { ArrayFieldTemplateProps } from '@rjsf/utils';

function ArrayFieldTemplate(props: ArrayFieldTemplateProps) {
const { className, items, canAdd, onAddClick } = props;
return (
<div className={className}>
{items &&
Expand Down Expand Up @@ -32,9 +31,9 @@ const ArrayFieldTemplate: React.FC<{ className: string; items?: any[]; canAdd?:
)}
</div>
);
};
}

export default {
export const customArray: Sample = {
schema: {
title: 'Custom array of strings',
type: 'array',
Expand All @@ -45,3 +44,5 @@ export default {
formData: ['react', 'jsonschema', 'form'],
templates: { ArrayFieldTemplate },
};

export default customArray;
6 changes: 5 additions & 1 deletion packages/playground/src/samples/customField.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default {
import { Sample } from './Sample';

const customField: Sample = {
schema: {
title: 'A registration form',
description: 'A custom-field form example.',
Expand Down Expand Up @@ -26,3 +28,5 @@ export default {
mySpecialStringField: 'special-text',
},
};

export default customField;
22 changes: 13 additions & 9 deletions packages/playground/src/samples/customFieldAnyOf.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const UiField: React.FC<{
schema?: any;
idSchema: { $id: string };
formData?: any;
onChange: (...args: any[]) => void;
[key: string]: any;
}> = ({ idSchema: { $id }, formData, onChange }) => {
import { Sample } from './Sample';
import { FieldProps } from '@rjsf/utils';

function UiField(props: FieldProps) {
const {
idSchema: { $id },
formData,
onChange,
} = props;
const changeHandlerFactory = (fieldName: string) => (event: any) => {
onChange(formData ? { ...formData, [fieldName]: event.target.value } : { [fieldName]: event.target.value });
};
Expand Down Expand Up @@ -71,9 +73,9 @@ const UiField: React.FC<{
</div>
</>
);
};
}

export default {
const customFieldAnyOf: Sample = {
schema: {
title: 'Location',
type: 'object',
Expand Down Expand Up @@ -106,3 +108,5 @@ export default {
},
formData: {},
};

export default customFieldAnyOf;
8 changes: 6 additions & 2 deletions packages/playground/src/samples/customObject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
RJSFSchema,
FormContextType,
ObjectFieldTemplateProps,
ObjectFieldTemplatePropertyType,
} from '@rjsf/utils';
import { Sample } from './Sample';

function ObjectFieldTemplate<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
props: ObjectFieldTemplateProps<T, S, F>
Expand All @@ -28,7 +30,7 @@ function ObjectFieldTemplate<T = any, S extends StrictRJSFSchema = RJSFSchema, F
)}{' '}
{description}
<div className='row'>
{properties.map((prop) => (
{properties.map((prop: ObjectFieldTemplatePropertyType) => (
<div className='col-lg-1 col-md-2 col-sm-4 col-xs-6' key={prop.content.key}>
{prop.content}
</div>
Expand All @@ -38,7 +40,7 @@ function ObjectFieldTemplate<T = any, S extends StrictRJSFSchema = RJSFSchema, F
);
}

export default {
const customObject: Sample = {
schema: {
title: 'A registration form',
description:
Expand Down Expand Up @@ -85,3 +87,5 @@ export default {
ObjectFieldTemplate,
},
};

export default customObject;
6 changes: 5 additions & 1 deletion packages/playground/src/samples/date.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default {
import { Sample } from './Sample';

const date: Sample = {
schema: {
title: 'Date and time widgets',
type: 'object',
Expand Down Expand Up @@ -57,3 +59,5 @@ export default {
},
formData: {},
};

export default date;
6 changes: 5 additions & 1 deletion packages/playground/src/samples/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default {
import { Sample } from './Sample';

const defaults: Sample = {
schema: {
title: 'Schema default properties',
type: 'object',
Expand Down Expand Up @@ -68,3 +70,5 @@ export default {
},
},
};

export default defaults;
7 changes: 6 additions & 1 deletion packages/playground/src/samples/enumObjects.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export default {
import { Sample } from './Sample';

const enumObjects: Sample = {
schema: {
definitions: {
locations: {
// @ts-expect-error -- enumNames an RJSF keyword and is not in the JSON Schema spec
enumNames: ['New York', 'Amsterdam', 'Hong Kong'],
enum: [
{
Expand Down Expand Up @@ -66,3 +69,5 @@ export default {
},
},
};

export default enumObjects;
7 changes: 6 additions & 1 deletion packages/playground/src/samples/errorSchema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default {
import { Sample } from './Sample';

const errorSchema: Sample = {
schema: {
title: 'A registration form',
description: 'A simple form example.',
Expand Down Expand Up @@ -67,8 +69,11 @@ export default {
password: 'noneed',
},
extraErrors: {
// @ts-expect-error - extraErrors types are hard
firstName: {
__errors: ['some error that got added as a prop'],
},
},
};

export default errorSchema;
6 changes: 5 additions & 1 deletion packages/playground/src/samples/errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default {
import { Sample } from './Sample';

const errors: Sample = {
schema: {
title: 'Contextualized errors',
type: 'object',
Expand Down Expand Up @@ -40,3 +42,5 @@ export default {
multipleChoicesList: ['foo', 'bar', 'fuzz'],
},
};

export default errors;
6 changes: 5 additions & 1 deletion packages/playground/src/samples/examples.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default {
import { Sample } from './Sample';

const examples: Sample = {
schema: {
title: 'Examples',
description: 'A text field with example values.',
Expand All @@ -12,3 +14,5 @@ export default {
},
},
};

export default examples;
Loading

0 comments on commit 83f09f6

Please sign in to comment.