Skip to content

Commit 9a15ea2

Browse files
Restored uiSchema.classNames with deprecation warning (rjsf-team#3041)
* Restored uiSchema.classNames with deprecation warning - Updated `SchemaField` to restore support for `uiSchema.classNames` with big fat deprecation warning - Updated the existing `classNames` test to verify restored code and deprecation warning - NOTE: Will fix up the documentation in the other PR * - Responded to reviewer feedback * - Adjusted warning message * - Responded to reviewer feedback and updated migration guide
1 parent c37a34b commit 9a15ea2

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

docs/5.x upgrade guide.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,33 @@ From v5, the child fields will correctly use the parent id when generating its o
262262

263263
##### Non-standard `enumNames` property
264264

265-
`enumNames` is a non-standard JSON Schema field that was deprecated in version 5. `enumNames` could be included in the schema to apply labels that differed from an enumeration value. This behavior can still be accomplished with `oneOf` or `anyOf` containing `const` values, so this behavior may be removed from a future major version of RJSF. For more information, see [#532](https://github.com/rjsf-team/react-jsonschema-form/issues/532).
265+
`enumNames` is a non-standard JSON Schema field that was deprecated in version 5.
266+
`enumNames` could be included in the schema to apply labels that differed from an enumeration value.
267+
This behavior can still be accomplished with `oneOf` or `anyOf` containing `const` values, so `enumNames` support may be removed from a future major version of RJSF.
268+
For more information, see [#532](https://github.com/rjsf-team/react-jsonschema-form/issues/532).
266269

270+
##### uiSchema.classNames
271+
272+
In versions previous to 5, `uiSchema.classNames` was the only property that did not require the `ui:` prefix.
273+
Additionally, it did not support being added into the `ui:options` object.
274+
This was fixed in version 5 to be consistent with all the other properties in the `uiSchema`, so the `uiSchema.classNames` support may be removed from a future major version of RJSF.
275+
276+
If you are using `classNames` as follows, simply add the `ui:` prefix to it to remove the deprecation warning that will be displayed for each `uiSchema.classNames` you have:
277+
278+
```jsx
279+
// This uiSchema will log a deprecation warning to the console
280+
const uiSchema = {
281+
title: {
282+
"classNames": "myClass"
283+
}
284+
};
285+
// This uiSchema will not
286+
const uiSchema = {
287+
title: {
288+
"ui:classNames": "myClass"
289+
}
290+
};
291+
```
267292

268293
### `@rjsf/material-ui` BREAKING CHANGES
269294

packages/core/src/components/fields/SchemaField.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ function SchemaFieldRender<T, F>(props: FieldProps<T, F>) {
209209

210210
const { __errors, ...fieldErrorSchema } = errorSchema || {};
211211
// See #439: uiSchema: Don't pass consumed class names to child components
212-
const fieldUiSchema = omit(uiSchema, ["ui:classNames"]);
212+
const fieldUiSchema = omit(uiSchema, ["ui:classNames", "classNames"]);
213213
if ("ui:options" in fieldUiSchema) {
214214
fieldUiSchema["ui:options"] = omit(fieldUiSchema["ui:options"], [
215215
"classNames",
@@ -255,6 +255,14 @@ function SchemaFieldRender<T, F>(props: FieldProps<T, F>) {
255255
if (!hideError && errors && errors.length > 0) {
256256
classNames.push("field-error has-error has-danger");
257257
}
258+
if (uiSchema?.classNames) {
259+
if (process.env.NODE_ENV !== "production") {
260+
console.warn(
261+
"'uiSchema.classNames' is deprecated and may be removed in a major release; Use 'ui:classNames' instead."
262+
);
263+
}
264+
classNames.push(uiSchema.classNames);
265+
}
258266
if (uiOptions.classNames) {
259267
classNames.push(uiOptions.classNames);
260268
}

packages/core/test/uiSchema_test.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ describe("uiSchema", () => {
3131
bar: {
3232
type: "string",
3333
},
34+
baz: {
35+
type: "string",
36+
},
3437
},
3538
};
3639

@@ -43,15 +46,24 @@ describe("uiSchema", () => {
4346
classNames: "class-for-bar another-for-bar",
4447
},
4548
},
49+
baz: {
50+
classNames: "class-for-baz",
51+
},
4652
};
4753

4854
it("should apply custom class names to target widgets", () => {
55+
sandbox.stub(console, "warn");
56+
4957
const { node } = createFormComponent({ schema, uiSchema });
50-
const [foo, bar] = node.querySelectorAll(".field-string");
58+
const [foo, bar, baz] = node.querySelectorAll(".field-string");
5159

5260
expect(foo.classList.contains("class-for-foo")).eql(true);
5361
expect(bar.classList.contains("class-for-bar")).eql(true);
5462
expect(bar.classList.contains("another-for-bar")).eql(true);
63+
expect(baz.classList.contains("class-for-baz")).eql(true);
64+
expect(
65+
console.warn.calledWithMatch(/'uiSchema.classNames' is deprecated/)
66+
).to.be.true;
5567
});
5668
});
5769

0 commit comments

Comments
 (0)