Skip to content

Commit

Permalink
Add widget capability to Array and Object fields
Browse files Browse the repository at this point in the history
  • Loading branch information
kdmcclel committed Jun 6, 2016
1 parent 83bbbfa commit b0e0725
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
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
18 changes: 17 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 @@ -67,10 +68,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 +89,19 @@ class ObjectField extends Component {
</div>
);
}
console.log(widget)
if(widget) {
const Widget = getAlternativeWidget(schema, widget, widgets);
return <Widget
id={idSchema && idSchema.id}
label={title}
placeholder={description}
onChange={this.onPropertyChange(name)}
schema={schema}
value={this.state}
required={required}
/>;
}
return (
<fieldset>
{title ? <TitleField
Expand Down

0 comments on commit b0e0725

Please sign in to comment.