Skip to content

Commit

Permalink
Spread formContext to ArrayTemplateField, files and multiselect arrays (
Browse files Browse the repository at this point in the history
  • Loading branch information
olzraiti authored and n1k0 committed Jan 30, 2017
1 parent 7a3f17c commit dd71a85
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ The following props are passed to each `ArrayFieldTemplate`:
- `required`: A boolean value stating if the array is required.
- `schema`: The schema object for this array.
- `title`: A string value containing the title for the array.
- `formContext`: The `formContext` object that you passed to Form.

The following props are part of each element in `items`:

Expand Down
10 changes: 7 additions & 3 deletions src/components/fields/ArrayField.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ class ArrayField extends Component {
readonly,
autofocus,
registry,
formContext,
onBlur
} = this.props;
const title = (schema.title === undefined) ? name : schema.title;
Expand Down Expand Up @@ -320,7 +321,8 @@ class ArrayField extends Component {
required,
schema,
title,
TitleField
TitleField,
formContext
};

// Check if a custom render function was passed in
Expand All @@ -331,7 +333,7 @@ class ArrayField extends Component {
renderMultiSelect() {
const {schema, idSchema, uiSchema, disabled, readonly, autofocus, onBlur} = this.props;
const {items} = this.state;
const {widgets, definitions} = this.props.registry;
const {widgets, definitions, formContext} = this.props.registry;
const itemsSchema = retrieveSchema(schema.items, definitions);
const enumOptions = optionsList(itemsSchema);
const {widget="select", ...options} = {...getUiOptions(uiSchema), enumOptions};
Expand All @@ -347,6 +349,7 @@ class ArrayField extends Component {
value={items}
disabled={disabled}
readonly={readonly}
formContext={formContext}
autofocus={autofocus}/>
);
}
Expand All @@ -355,7 +358,7 @@ class ArrayField extends Component {
const {schema, uiSchema, idSchema, name, disabled, readonly, autofocus, onBlur} = this.props;
const title = schema.title || name;
const {items} = this.state;
const {widgets} = this.props.registry;
const {widgets, formContext} = this.props.registry;
const {widget="files", ...options} = getUiOptions(uiSchema);
const Widget = getWidget(schema, widget, widgets);
return (
Expand All @@ -370,6 +373,7 @@ class ArrayField extends Component {
value={items}
disabled={disabled}
readonly={readonly}
formContext={formContext}
autofocus={autofocus}/>
);
}
Expand Down
60 changes: 59 additions & 1 deletion test/FormContext_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ describe("FormContext", () => {
const {node} = createFormComponent({
schema: {
type: "object",
title: "A title",
properties: {
prop: {
type: "string"
Expand All @@ -81,6 +80,26 @@ describe("FormContext", () => {
.to.exist;
});

it("should be passed to ArrayTemplateField", () => {
function CustomArrayTemplateField({formContext}) {
return <div id={formContext.foo}/>;
}

const {node} = createFormComponent({
schema: {
type: "array",
items: {
type: "string"
}
},
ArrayFieldTemplate: CustomArrayTemplateField,
formContext
});

expect(node.querySelector("#" + formContext.foo))
.to.exist;
});

it("should be passed to custom TitleField", () => {
const fields = {TitleField: CustomComponent};

Expand Down Expand Up @@ -114,4 +133,43 @@ describe("FormContext", () => {
expect(node.querySelector("#" + formContext.foo))
.to.exist;
});


it("should be passed to multiselect", () => {
const widgets = {SelectWidget: CustomComponent};
const {node} = createFormComponent({
schema: {
type: "array",
items: {
type: "string",
enum: ["foo"],
enumNames: ["bar"]
},
uniqueItems: true
},
widgets,
formContext
});

expect(node.querySelector("#" + formContext.foo))
.to.exist;
});

it("should be passed to files array", () => {
const widgets = {FileWidget: CustomComponent};
const {node} = createFormComponent({
schema: {
type: "array",
items: {
type: "string",
format: "data-url"
}
},
widgets,
formContext
});

expect(node.querySelector("#" + formContext.foo))
.to.exist;
});
});

0 comments on commit dd71a85

Please sign in to comment.