Skip to content

Commit c7c6768

Browse files
vinhlhn1k0
authored andcommitted
Add lazy getDefaultRegistry (rjsf-team#575)
1 parent 299dc6e commit c7c6768

File tree

6 files changed

+40
-27
lines changed

6 files changed

+40
-27
lines changed

src/components/fields/ArrayField.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class ArrayField extends Component {
179179
uiSchema: {},
180180
formData: [],
181181
idSchema: {},
182-
registry: getDefaultRegistry(),
182+
registry: null,
183183
required: false,
184184
disabled: false,
185185
readonly: false,
@@ -203,7 +203,7 @@ class ArrayField extends Component {
203203

204204
onAddClick = event => {
205205
event.preventDefault();
206-
const { schema, registry, formData } = this.props;
206+
const { schema, formData, registry = getDefaultRegistry() } = this.props;
207207
const { definitions } = registry;
208208
let itemSchema = schema.items;
209209
if (isFixedItems(schema) && allowAdditionalItems(schema)) {
@@ -291,7 +291,7 @@ class ArrayField extends Component {
291291
disabled,
292292
readonly,
293293
autofocus,
294-
registry,
294+
registry = getDefaultRegistry(),
295295
formContext,
296296
onBlur,
297297
} = this.props;
@@ -348,9 +348,10 @@ class ArrayField extends Component {
348348
readonly,
349349
autofocus,
350350
onBlur,
351+
registry = getDefaultRegistry(),
351352
} = this.props;
352353
const items = this.props.formData;
353-
const { widgets, definitions, formContext } = this.props.registry;
354+
const { widgets, definitions, formContext } = registry;
354355
const itemsSchema = retrieveSchema(schema.items, definitions);
355356
const enumOptions = optionsList(itemsSchema);
356357
const { widget = "select", ...options } = {
@@ -385,10 +386,11 @@ class ArrayField extends Component {
385386
readonly,
386387
autofocus,
387388
onBlur,
389+
registry = getDefaultRegistry(),
388390
} = this.props;
389391
const title = schema.title || name;
390392
const items = this.props.formData;
391-
const { widgets, formContext } = this.props.registry;
393+
const { widgets, formContext } = registry;
392394
const { widget = "files", ...options } = getUiOptions(uiSchema);
393395
const Widget = getWidget(schema, widget, widgets);
394396
return (
@@ -420,7 +422,7 @@ class ArrayField extends Component {
420422
disabled,
421423
readonly,
422424
autofocus,
423-
registry,
425+
registry = getDefaultRegistry(),
424426
onBlur,
425427
} = this.props;
426428
const title = schema.title || name;
@@ -502,8 +504,13 @@ class ArrayField extends Component {
502504
autofocus,
503505
onBlur,
504506
} = props;
505-
const { SchemaField } = this.props.registry.fields;
506-
const { disabled, readonly, uiSchema } = this.props;
507+
const {
508+
disabled,
509+
readonly,
510+
uiSchema,
511+
registry = getDefaultRegistry(),
512+
} = this.props;
513+
const { fields: { SchemaField } } = registry;
507514
const { orderable, removable } = {
508515
orderable: true,
509516
removable: true,

src/components/fields/BooleanField.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function BooleanField(props) {
1515
uiSchema,
1616
idSchema,
1717
formData,
18-
registry,
18+
registry = getDefaultRegistry(),
1919
required,
2020
disabled,
2121
readonly,
@@ -72,7 +72,7 @@ if (process.env.NODE_ENV !== "production") {
7272

7373
BooleanField.defaultProps = {
7474
uiSchema: {},
75-
registry: getDefaultRegistry(),
75+
registry: null,
7676
disabled: false,
7777
readonly: false,
7878
autofocus: false,

src/components/fields/ObjectField.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ObjectField extends Component {
1313
formData: {},
1414
errorSchema: {},
1515
idSchema: {},
16-
registry: getDefaultRegistry(),
16+
registry: null,
1717
required: false,
1818
disabled: false,
1919
readonly: false,
@@ -44,8 +44,9 @@ class ObjectField extends Component {
4444
disabled,
4545
readonly,
4646
onBlur,
47+
registry = getDefaultRegistry(),
4748
} = this.props;
48-
const { definitions, fields, formContext } = this.props.registry;
49+
const { definitions, fields, formContext } = registry;
4950
const { SchemaField, TitleField, DescriptionField } = fields;
5051
const schema = retrieveSchema(this.props.schema, definitions);
5152
const title = schema.title === undefined ? name : schema.title;
@@ -92,7 +93,7 @@ class ObjectField extends Component {
9293
formData={formData[name]}
9394
onChange={this.onPropertyChange(name)}
9495
onBlur={onBlur}
95-
registry={this.props.registry}
96+
registry={registry}
9697
disabled={disabled}
9798
readonly={readonly}
9899
/>

src/components/fields/SchemaField.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,14 @@ DefaultTemplate.defaultProps = {
132132
};
133133

134134
function SchemaFieldRender(props) {
135-
const { uiSchema, errorSchema, idSchema, name, required, registry } = props;
135+
const {
136+
uiSchema,
137+
errorSchema,
138+
idSchema,
139+
name,
140+
required,
141+
registry = getDefaultRegistry(),
142+
} = props;
136143
const {
137144
definitions,
138145
fields,
@@ -247,7 +254,7 @@ SchemaField.defaultProps = {
247254
uiSchema: {},
248255
errorSchema: {},
249256
idSchema: {},
250-
registry: getDefaultRegistry(),
257+
registry: null,
251258
disabled: false,
252259
readonly: false,
253260
autofocus: false,

src/components/fields/StringField.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ function StringField(props) {
1919
disabled,
2020
readonly,
2121
autofocus,
22-
registry,
2322
onChange,
2423
onBlur,
24+
registry = getDefaultRegistry(),
2525
} = props;
2626
const { title, format } = schema;
2727
const { widgets, formContext } = registry;
@@ -78,7 +78,7 @@ if (process.env.NODE_ENV !== "production") {
7878

7979
StringField.defaultProps = {
8080
uiSchema: {},
81-
registry: getDefaultRegistry(),
81+
registry: null,
8282
disabled: false,
8383
readonly: false,
8484
autofocus: false,

src/utils.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,13 @@ const widgetMap = {
5252
},
5353
};
5454

55-
const defaultRegistry = {
56-
fields: require("./components/fields").default,
57-
widgets: require("./components/widgets").default,
58-
definitions: {},
59-
formContext: {},
60-
};
61-
6255
export function getDefaultRegistry() {
63-
return defaultRegistry;
56+
return {
57+
fields: require("./components/fields").default,
58+
widgets: require("./components/widgets").default,
59+
definitions: {},
60+
formContext: {},
61+
};
6462
}
6563

6664
export function getWidget(schema, widget, registeredWidgets = {}) {
@@ -249,9 +247,9 @@ export function orderProperties(properties, order) {
249247
return prev;
250248
}, {});
251249
const errorPropList = arr =>
252-
(arr.length > 1
250+
arr.length > 1
253251
? `properties '${arr.join("', '")}'`
254-
: `property '${arr[0]}'`);
252+
: `property '${arr[0]}'`;
255253
const propertyHash = arrayToHash(properties);
256254
const orderHash = arrayToHash(order);
257255
const extraneous = order.filter(prop => prop !== "*" && !propertyHash[prop]);

0 commit comments

Comments
 (0)