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

Add widget capability to Array and Object fields #225

Closed
wants to merge 1 commit into from
Closed
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ You can provide your own custom widgets to a uiSchema for the following json dat
- `number`
- `integer`
- `boolean`
- `array`
- `object`

```jsx
const schema = {
Expand Down
24 changes: 24 additions & 0 deletions src/components/fields/ArrayField.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Component, PropTypes } from "react";

import {
getDefaultFormState,
getAlternativeWidget,
isMultiSelect,
isFilesArray,
isFixedItems,
Expand Down Expand Up @@ -121,6 +122,10 @@ class ArrayField extends Component {

render() {
const {schema, uiSchema} = this.props;
const widget = uiSchema["ui:widget"] || schema.format;
if (widget && widget !== 'checkboxes') {
return this.renderWidget();
}
if (isFilesArray(schema, uiSchema)) {
return this.renderFiles();
}
Expand All @@ -133,6 +138,25 @@ class ArrayField extends Component {
return this.renderNormalArray();
}

renderWidget() {
const {schema, idSchema, uiSchema, name, required} = this.props;
const {title, description} = schema;
const {items} = this.state;
const {widgets} = this.props.registry;

const widget = uiSchema["ui:widget"] || schema.format;
const Widget = getAlternativeWidget(schema, widget, widgets);
return <Widget
id={idSchema && idSchema.id}
label={title || name}
placeholder={description}
onChange={this.onSelectChange}
schema={schema}
value={items}
required={required}
/>;
}

renderNormalArray() {
const {
schema,
Expand Down
21 changes: 20 additions & 1 deletion src/components/fields/ObjectField.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Component, PropTypes } from "react";

import {
getDefaultFormState,
getAlternativeWidget,
orderProperties,
retrieveSchema,
shouldRender,
Expand Down Expand Up @@ -57,6 +58,10 @@ class ObjectField extends Component {
};
};

onObjectChange = (value) => {
this.asyncSetState(value);
};

render() {
const {
uiSchema,
Expand All @@ -67,10 +72,12 @@ class ObjectField extends Component {
disabled,
readonly
} = this.props;
const {definitions, fields} = this.props.registry;
const {definitions, fields, widgets} = this.props.registry;
const {SchemaField, TitleField, DescriptionField} = fields;
const schema = retrieveSchema(this.props.schema, definitions);
const title = schema.title || name;
const {description} = schema;
const widget = uiSchema['ui:widget'] || schema.format;
let orderedProperties;
try {
const properties = Object.keys(schema.properties);
Expand All @@ -86,6 +93,18 @@ class ObjectField extends Component {
</div>
);
}
if(widget) {
const Widget = getAlternativeWidget(schema, widget, widgets);
return <Widget
id={idSchema && idSchema.id}
label={title}
placeholder={description}
onChange={this.onObjectChange}
schema={schema}
value={this.state}
required={required}
/>;
}
return (
<fieldset>
{title ? <TitleField
Expand Down