Skip to content

Commit 6028edd

Browse files
committed
Attempt to have a dynamic SchemaField component
1 parent 0bdc4dd commit 6028edd

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

src/components/Form.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React, { Component, PropTypes } from "react";
22
import { Validator } from "jsonschema";
33

4-
import { getDefaultFormState } from "../utils";
5-
import SchemaField from "./fields/SchemaField";
4+
import { getDefaultFormState, getSchemaField } from "../utils";
65
import ErrorList from "./ErrorList";
76

87

@@ -31,6 +30,13 @@ export default class Form extends Component {
3130
};
3231
}
3332

33+
getChildContext() {
34+
const SchemaField = getSchemaField(this.props);
35+
return {
36+
schemaField: SchemaField
37+
};
38+
}
39+
3440
validate(formData) {
3541
const validator = new Validator();
3642
return validator.validate(formData, this.props.schema).errors;
@@ -78,6 +84,8 @@ export default class Form extends Component {
7884
render() {
7985
const {schema, uiSchema} = this.props;
8086
const {formData} = this.state;
87+
const SchemaField = getSchemaField(this.props, this.context);
88+
console.log("THIS IS FORM", SchemaField);
8189
return (
8290
<form className="rjsf" onSubmit={this.onSubmit.bind(this)}>
8391
{this.renderErrors()}
@@ -103,4 +111,8 @@ if (process.env.NODE_ENV !== "production") {
103111
};
104112
}
105113

114+
Form.childContextTypes = {
115+
schemaField: PropTypes.oneOfType([Component, Function])
116+
};
117+
106118
export default Form;

src/components/fields/ArrayField.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { Component, PropTypes } from "react";
22

33
import { getDefaultFormState } from "../../utils";
4-
import SchemaField from "./SchemaField";
54

65

76
class ArrayField extends Component {
@@ -65,6 +64,8 @@ class ArrayField extends Component {
6564
const {schema, uiSchema, name} = this.props;
6665
const title = schema.title || name;
6766
const {items} = this.state;
67+
const SchemaField = this.context.schemaField;
68+
console.log("In ArrayField, got", SchemaField);
6869
return (
6970
<fieldset
7071
className={`field field-array field-array-of-${schema.items.type}`}>
@@ -80,7 +81,8 @@ class ArrayField extends Component {
8081
uiSchema={uiSchema.items}
8182
formData={items[index]}
8283
required={this.isItemRequired(schema.items)}
83-
onChange={this.onChange.bind(this, index)} />
84+
onChange={this.onChange.bind(this, index)}
85+
schemaField={SchemaField}/>
8486
<p className="array-item-remove">
8587
<button type="button"
8688
onClick={this.onDropClick.bind(this, index)}>-</button></p>
@@ -105,4 +107,8 @@ if (process.env.NODE_ENV !== "production") {
105107
};
106108
}
107109

110+
ArrayField.childContextTypes = {
111+
schemaField: PropTypes.oneOfType([Component, Function])
112+
};
113+
108114
export default ArrayField;

src/components/fields/ObjectField.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { Component, PropTypes } from "react";
22

33
import { getDefaultFormState } from "../../utils";
4-
import SchemaField from "./SchemaField";
54

65

76
class ObjectField extends Component {
@@ -40,6 +39,7 @@ class ObjectField extends Component {
4039
render() {
4140
const {schema, uiSchema, name} = this.props;
4241
const title = name || schema.title;
42+
const SchemaField = this.context.schemaField;
4343
return (
4444
<fieldset>
4545
{title ? <legend>{title}</legend> : null}
@@ -72,4 +72,8 @@ if (process.env.NODE_ENV !== "production") {
7272
};
7373
}
7474

75+
ObjectField.childContextTypes = {
76+
schemaField: PropTypes.oneOfType([Component, Function])
77+
};
78+
7579
export default ObjectField;

src/utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import UpDownWidget from "./components/widgets/UpDownWidget";
44
import RangeWidget from "./components/widgets/RangeWidget";
55
import SelectWidget from "./components/widgets/SelectWidget";
66
import TextareaWidget from "./components/widgets/TextareaWidget";
7+
import SchemaField from "./components/fields/SchemaField";
78

89

910
const altWidgetMap = {
@@ -80,3 +81,10 @@ export function asNumber(value) {
8081
const valid = typeof n === "number" && !Number.isNaN(n);
8182
return valid ? n : value;
8283
}
84+
85+
export function getSchemaField(props, context) {
86+
if (props == undefined) {
87+
return SchemaField;
88+
}
89+
return props.hasOwnProperty("schemaField") ? props.schemaField : SchemaField;
90+
}

test/index_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@ describe("Form", () => {
12521252
});
12531253

12541254
describe("array level", () => {
1255-
it("should update form state from new formData prop value", () => {
1255+
it.only("should update form state from new formData prop value", () => {
12561256
const schema = {
12571257
type: "array",
12581258
items: {

0 commit comments

Comments
 (0)