Skip to content

Commit

Permalink
Add NullField (#1238)
Browse files Browse the repository at this point in the history
* Add NullField

* Always set the formData to null for a null field

* Don't overwrite existing data

* Use central fields propTypes
  • Loading branch information
faissalMT authored and epicfaace committed Apr 12, 2019
1 parent ac397ca commit c5e8899
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 5 deletions.
7 changes: 2 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions playground/samples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import propertyDependencies from "./propertyDependencies";
import schemaDependencies from "./schemaDependencies";
import additionalProperties from "./additionalProperties";
import nullable from "./nullable";
import nullField from "./null";

export const samples = {
Simple: simple,
Expand All @@ -45,5 +46,6 @@ export const samples = {
"Additional Properties": additionalProperties,
"Any Of": anyOf,
"One Of": oneOf,
"Null fields": nullField,
Nullable: nullable,
};
28 changes: 28 additions & 0 deletions playground/samples/null.js
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: {},
};
20 changes: 20 additions & 0 deletions src/components/fields/NullField.js
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;
1 change: 1 addition & 0 deletions src/components/fields/SchemaField.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const COMPONENT_TYPES = {
number: "NumberField",
object: "ObjectField",
string: "StringField",
null: "NullField",
};

function getFieldComponent(schema, uiSchema, idSchema, fields) {
Expand Down
2 changes: 2 additions & 0 deletions src/components/fields/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ObjectField from "./ObjectField";
import SchemaField from "./SchemaField";
import StringField from "./StringField";
import TitleField from "./TitleField";
import NullField from "./NullField";
import UnsupportedField from "./UnsupportedField";

export default {
Expand All @@ -20,5 +21,6 @@ export default {
SchemaField,
StringField,
TitleField,
NullField,
UnsupportedField,
};
60 changes: 60 additions & 0 deletions test/NullField_test.js
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);
});
});
});

0 comments on commit c5e8899

Please sign in to comment.