Skip to content

Commit 1ee343d

Browse files
glompixepicfaace
authored andcommitted
Make numbers nullable by allowing null values to be echoed from asNumber() (rjsf-team#1269)
* Make numbers nullable by allowing null values to be echoed from asNumber() * fix: revert package-lock changes
1 parent fba5bee commit 1ee343d

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

playground/samples/nullable.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = {
1515
title: "Last name",
1616
},
1717
age: {
18-
type: "integer",
18+
type: ["integer", "null"],
1919
title: "Age",
2020
},
2121
bio: {
@@ -43,6 +43,7 @@ module.exports = {
4343
"ui:widget": "updown",
4444
"ui:title": "Age of person",
4545
"ui:description": "(earthian year)",
46+
"ui:emptyValue": null,
4647
},
4748
bio: {
4849
"ui:widget": "textarea",

src/utils.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ export function asNumber(value) {
272272
if (value === "") {
273273
return undefined;
274274
}
275+
if (value === null) {
276+
return null;
277+
}
275278
if (/\.$/.test(value)) {
276279
// "3." can't really be considered a number even if it parses in js. The
277280
// user is most likely entering a float.

test/utils_test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,10 @@ describe("utils", () => {
342342
it("should return undefined if the input is empty", () => {
343343
expect(asNumber("")).eql(undefined);
344344
});
345+
346+
it("should return null if the input is null", () => {
347+
expect(asNumber(null)).eql(null);
348+
});
345349
});
346350

347351
describe("orderProperties()", () => {

0 commit comments

Comments
 (0)