Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #10: Added support for integer fields. #17

Merged
merged 1 commit into from
Dec 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions playground/samples/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import arrays from "./arrays";
import nested from "./nested";
import numbers from "./numbers";
import simple from "./simple";
import widgets from "./widgets";

export const samples = {
Simple: simple,
Nested: nested,
Arrays: arrays,
Numbers: numbers,
Widgets: widgets,
};
26 changes: 26 additions & 0 deletions playground/samples/numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = {
schema: {
type: "object",
properties: {
number: {
title: "Number",
type: "number"
},
integer: {
title: "Integer",
type: "integer"
},
numberEnum: {
type: "number",
title: "Number enum",
enum: [1, 2, 3]
}
}
},
uiSchema: {},
formData: {
number: 3.14,
integer: 42,
numberEnum: 2
}
};
26 changes: 26 additions & 0 deletions src/components/fields/NumberField.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React, { PropTypes } from "react";

import { asNumber } from "../../utils";
import StringField from "./StringField";

function NumberField(props) {
return (
<StringField {...props}
onChange={(value) => props.onChange(asNumber(value))} />
);
}

if (process.env.NODE_ENV !== "production") {
NumberField.propTypes = {
schema: PropTypes.object.isRequired,
onChange: PropTypes.func.isRequired,
formData: PropTypes.number,
required: PropTypes.bool,
};
}

NumberField.defaultProps = {
uiSchema: {}
};

export default NumberField;
10 changes: 6 additions & 4 deletions src/components/fields/SchemaField.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import React, { PropTypes } from "react";

import StringField from "./StringField";
import ArrayField from "./ArrayField";
import BooleanField from "./BooleanField";
import NumberField from "./NumberField";
import ObjectField from "./ObjectField";
import StringField from "./StringField";
import UnsupportedField from "./UnsupportedField";


const COMPONENT_TYPES = {
"string": StringField,
"array": ArrayField,
"boolean": BooleanField,
"object": ObjectField,
"date-time": StringField,
"number": StringField,
"integer": NumberField,
"number": NumberField,
"object": ObjectField,
"string": StringField,
};

function SchemaField(props) {
Expand Down
5 changes: 4 additions & 1 deletion src/components/fields/StringField.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ if (process.env.NODE_ENV !== "production") {
StringField.propTypes = {
schema: PropTypes.object.isRequired,
onChange: PropTypes.func.isRequired,
formData: PropTypes.string,
formData: PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.number,
]),
required: PropTypes.bool,
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/components/widgets/SelectWidget.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { PropTypes } from "react";

import { asNumber } from "../../utils";
import Wrapper from "./../widgets/Wrapper";


Expand All @@ -11,7 +12,7 @@ function processValue(type, value) {
if (type === "boolean") {
return value === "true";
} else if (type === "number") {
return value === Number(value);
return asNumber(value);
}
return value;
}
Expand Down
10 changes: 8 additions & 2 deletions src/components/widgets/TextWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ if (process.env.NODE_ENV !== "production") {
type: PropTypes.string.isRequired,
label: PropTypes.string,
placeholder: PropTypes.string,
value: PropTypes.string,
defaultValue: PropTypes.string,
value: PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.number,
]),
defaultValue: PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.number,
]),
required: PropTypes.bool,
onChange: PropTypes.func,
};
Expand Down
6 changes: 6 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ export function getDefaultFormState(schema) {
}
return defaultTypeValue(schema.type);
}

export function asNumber(value) {
const n = Number(value);
const valid = typeof n === "number" && !Number.isNaN(n);
return valid ? n : value;
}
130 changes: 130 additions & 0 deletions test/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,136 @@ describe("Form", () => {
});
});

describe("NumberField", () => {
describe("TextWidget", () => {
it("should render a string field", () => {
const {node} = createComponent({schema: {
type: "number"
}});

expect(node.querySelectorAll(".field input[type=text]"))
.to.have.length.of(1);
});

it("should render a string field with a label", () => {
const {node} = createComponent({schema: {
type: "number",
title: "foo"
}});

expect(node.querySelector(".field label > span").textContent)
.eql("foo");
});

it("should render a string field with a placeholder", () => {
const {node} = createComponent({schema: {
type: "number",
description: "bar",
}});

expect(node.querySelector(".field input").getAttribute("placeholder"))
.eql("bar");
});

it("should assign a default value", () => {
const {node} = createComponent({schema: {
type: "number",
default: 2,
}});

expect(node.querySelector(".field input").getAttribute("value"))
.eql("2");
});

it("should handle a change event", () => {
const {comp, node} = createComponent({schema: {
type: "number",
}});

Simulate.change(node.querySelector("input"), {
target: {value: "2"}
});

expect(comp.state.formData).eql(2);
});

it("should fill field with data", () => {
const {node} = createComponent({schema: {
type: "number",
}, formData: 2});

expect(node.querySelector(".field input").getAttribute("value"))
.eql("2");
});
});

describe("SelectWidget", () => {
it("should render a number field", () => {
const {node} = createComponent({schema: {
type: "number",
enum: [1, 2]
}});

expect(node.querySelectorAll(".field select"))
.to.have.length.of(1);
});

it("should render a string field with a label", () => {
const {node} = createComponent({schema: {
type: "number",
enum: [1, 2],
title: "foo",
}});

expect(node.querySelector(".field label > span").textContent)
.eql("foo");
});

it("should render a select field with a tooltip", () => {
const {node} = createComponent({schema: {
type: "number",
enum: [1, 2],
description: "baz",
}});

expect(node.querySelector(".field select").getAttribute("title"))
.eql("baz");
});

it("should assign a default value", () => {
const {comp} = createComponent({schema: {
type: "number",
enum: [1, 2],
default: 1,
}});

expect(comp.state.formData).eql(1);
});

it("should handle a change event", () => {
const {comp, node} = createComponent({schema: {
type: "number",
enum: [1, 2],
}});

Simulate.change(node.querySelector("select"), {
target: {value: "2"}
});

expect(comp.state.formData).eql(2);
});

it("should fill field with data", () => {
const {comp} = createComponent({schema: {
type: "number",
enum: [1, 2],
}, formData: 2});

expect(comp.state.formData).eql(2);
});
});
});

describe("BooleanField", () => {
it("should render a boolean field", () => {
const {node} = createComponent({schema: {
Expand Down