Skip to content

Commit

Permalink
isObject should return false for Date as well as File, null, and Array
Browse files Browse the repository at this point in the history
Without this, assigning dates to the form's value instead merges them with a default value for objects of `{}` (in mergeDefaultsWithFormData), which stops them being dates.
  • Loading branch information
BenBeattieHood authored and BenBeattieHood committed Dec 21, 2022
1 parent d3b092c commit 4bb6a1a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ should change the heading of the (upcoming) version to include a major version b
## @rjsf/core
- Added `ref` definition to `ThemeProps` fixing [#2135](https://github.com/rjsf-team/react-jsonschema-form/issues/2135)
- Updated the `onChange` handler in `Form` to use the new `preventDuplicates` mode of `mergeObjects()` when merging `extraErrors` when live validation is off, fixing [#3169](https://github.com/rjsf-team/react-jsonschema-form/issues/3169)
- Fix isObject to correctly identify 'Date' as not mergable with Object default value.

## @rjsf/material-ui
- Fix RangeWidget missing htmlFor and schema.title [#3281](https://github.com/rjsf-team/react-jsonschema-form/pull/3281)
Expand Down
3 changes: 3 additions & 0 deletions packages/utils/src/isObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ export default function isObject(thing: any) {
if (typeof File !== "undefined" && thing instanceof File) {
return false;
}
if (typeof Date !== "undefined" && thing instanceof Date) {
return false;
}
return typeof thing === "object" && thing !== null && !Array.isArray(thing);
}
4 changes: 4 additions & 0 deletions packages/utils/test/isObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ describe('isObject()', () => {
const file = new File(["test"], "test.txt");
expect(isObject(file)).toBe(false);
});
it('returns false when a Date is provided', () => {
const date = new Date();
expect(isObject(date)).toBe(false);
});
it('returns false when an array is provided', () => {
expect(isObject(['foo'])).toBe(false);
});
Expand Down

0 comments on commit 4bb6a1a

Please sign in to comment.