Skip to content

Commit

Permalink
Support displayFields in the Relation widget
Browse files Browse the repository at this point in the history
  • Loading branch information
zurawiki committed Apr 25, 2018
1 parent 29853b2 commit 7c44dd7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions example/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ collections: # A list of collections the CMS should be able to edit
name: "post"
widget: "relationKitchenSinkPost"
collection: "posts"
dispalyFields: ["title", "date"]
searchFields: ["title", "body"]
valueField: "title"
- {label: "Title", name: "title", widget: "string"}
Expand Down
11 changes: 9 additions & 2 deletions src/components/EditorWidgets/Relation/RelationControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import PropTypes from 'prop-types';
import React, { Component } from 'react';
import Autosuggest from 'react-autosuggest';
import uuid from 'uuid/v4';
import { Map } from 'immutable';
import { List, Map } from 'immutable';
import { connect } from 'react-redux';
import { debounce } from 'lodash';
import { query, clearSearch } from 'Actions/search';
Expand Down Expand Up @@ -90,7 +90,14 @@ class RelationControl extends Component {

renderSuggestion = (suggestion) => {
const { field } = this.props;
const valueField = field.get('valueField');
const valueField = field.get('displayFields') || field.get('valueField');
if (List.isList(valueField)) {
return (
<span>
{valueField.toJS().map(key => <span key={key}>{suggestion.data[key]}{' '}</span>)}
</span>
);
}
return <span>{suggestion.data[valueField]}</span>;
};

Expand Down
4 changes: 3 additions & 1 deletion website/site/content/docs/widgets/relation.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The relation widget allows you to reference items from another collection. It pr
- **Options:**
- `default`: accepts any widget data type; defaults to an empty string
- `collection`: (**required**) name of the collection being referenced (string)
- `displayFields`: list of one or more names of fields in the referenced collection that will render in the autocomplete menu of the control. The value field is used by default.
- `searchFields`: (**required**) list of one or more names of fields in the referenced collection to search for the typed value
- `valueField`: (**required**) name of the field from the referenced collection whose value will be stored for the relation
- **Example** (assuming a separate "authors" collection with "name" and "twitterHandle" fields):
Expand All @@ -22,7 +23,8 @@ The relation widget allows you to reference items from another collection. It pr
name: "author"
widget: "relation"
collection: "authors"
displayFields: ["twitterHandle", "followerCount"]
searchFields: ["name", "twitterHandle"]
valueField: "name"
```
The generated UI input will search the authors collection by name and twitterHandle as the user types. On selection, the author name will be saved for the field.
The generated UI input will search the authors collection by name and twitterHandle as the user types. Each collection will be represented by the author's handle and follower count. On selection, the author name will be saved for the field.

0 comments on commit 7c44dd7

Please sign in to comment.