-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add NullField * Always set the formData to null for a null field * Don't overwrite existing data * Use central fields propTypes
- Loading branch information
Showing
7 changed files
with
115 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module.exports = { | ||
schema: { | ||
title: "Null field example", | ||
description: "A short form with a null field", | ||
type: "object", | ||
required: ["firstName"], | ||
properties: { | ||
helpText: { | ||
title: "A null field", | ||
description: | ||
"Null fields like this are great for adding extra information", | ||
type: "null", | ||
}, | ||
firstName: { | ||
type: "string", | ||
title: "A regular string field", | ||
default: "Chuck", | ||
}, | ||
}, | ||
}, | ||
uiSchema: { | ||
firstName: { | ||
"ui:autofocus": true, | ||
"ui:emptyValue": "", | ||
}, | ||
}, | ||
formData: {}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Component } from "react"; | ||
import * as types from "../../types"; | ||
|
||
class NullField extends Component { | ||
componentDidMount() { | ||
if (this.props.formData === undefined) { | ||
this.props.onChange(null); | ||
} | ||
} | ||
|
||
render() { | ||
return null; | ||
} | ||
} | ||
|
||
if (process.env.NODE_ENV !== "production") { | ||
NullField.propTypes = types.fieldProps; | ||
} | ||
|
||
export default NullField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { expect } from "chai"; | ||
|
||
import { createFormComponent, createSandbox } from "./test_utils"; | ||
|
||
describe("NullField", () => { | ||
let sandbox; | ||
|
||
beforeEach(() => { | ||
sandbox = createSandbox(); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
describe("No widget", () => { | ||
it("should render a null field", () => { | ||
const { node } = createFormComponent({ | ||
schema: { | ||
type: "null", | ||
}, | ||
}); | ||
|
||
expect(node.querySelectorAll(".field")).to.have.length.of(1); | ||
}); | ||
|
||
it("should render a null field with a label", () => { | ||
const { node } = createFormComponent({ | ||
schema: { | ||
type: "null", | ||
title: "foo", | ||
}, | ||
}); | ||
|
||
expect(node.querySelector(".field label").textContent).eql("foo"); | ||
}); | ||
|
||
it("should assign a default value", () => { | ||
const { comp } = createFormComponent({ | ||
schema: { | ||
type: "null", | ||
default: null, | ||
}, | ||
}); | ||
|
||
expect(comp.state.formData).eql(null); | ||
}); | ||
|
||
it("should not overwrite existing data", () => { | ||
const { comp } = createFormComponent({ | ||
schema: { | ||
type: "null", | ||
}, | ||
formData: 3, | ||
}); | ||
|
||
expect(comp.state.formData).eql(3); | ||
}); | ||
}); | ||
}); |