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

feat(object): variable types #7031

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions dev-test/config.yml
Expand Up @@ -268,6 +268,20 @@ collections: # A list of collections the CMS should be able to edit
fields:
- { label: 'Image', name: 'image', widget: 'image' }
- { label: 'File', name: 'file', widget: 'file' }
- label: Typed Object
name: typed_object
widget: object
types:
- name: copy
widget: object
fields:
- { name: text, widget: string }
- name: infographic
widget: object
fields:
- { name: caption, widget: string }
- { name: image, widget: image }
- { name: overlay, widget: image }
- name: pages # a nested collection
label: Pages
label_singular: 'Page'
Expand Down
3 changes: 1 addition & 2 deletions packages/decap-cms-app/src/extensions.js
Expand Up @@ -18,7 +18,6 @@ import DecapCmsWidgetFile from 'decap-cms-widget-file';
import DecapCmsWidgetSelect from 'decap-cms-widget-select';
import DecapCmsWidgetMarkdown from 'decap-cms-widget-markdown';
import DecapCmsWidgetList from 'decap-cms-widget-list';
import DecapCmsWidgetObject from 'decap-cms-widget-object';
import DecapCmsWidgetRelation from 'decap-cms-widget-relation';
import DecapCmsWidgetBoolean from 'decap-cms-widget-boolean';
import DecapCmsWidgetMap from 'decap-cms-widget-map';
Expand Down Expand Up @@ -48,7 +47,7 @@ CMS.registerWidget([
DecapCmsWidgetSelect.Widget(),
DecapCmsWidgetMarkdown.Widget(),
DecapCmsWidgetList.Widget(),
DecapCmsWidgetObject.Widget(),
DecapCmsWidgetList.ObjectWidget(),
DecapCmsWidgetRelation.Widget(),
DecapCmsWidgetBoolean.Widget(),
DecapCmsWidgetMap.Widget(),
Expand Down
15 changes: 15 additions & 0 deletions packages/decap-cms-widget-list/src/ListControl.js
Expand Up @@ -749,3 +749,18 @@ export default class ListControl extends React.Component {
}
}
}

export class ObjectControlWrapper extends React.Component {
render() {
if (this.props.field.get('required') === false || this.props.field.get('types')) {
return (
<ListControl
{...this.props}
onChange={e => this.props.onChange(e.get(0))}
value={List([this.props.value].filter(Boolean))}
/>
);
}
return <ObjectControl {...this.props} />;
}
}
13 changes: 11 additions & 2 deletions packages/decap-cms-widget-list/src/index.js
@@ -1,6 +1,6 @@
import DecapCmsWidgetObject from 'decap-cms-widget-object';

import controlComponent from './ListControl';
import controlComponent, { ObjectControlWrapper } from './ListControl';
import schema from './schema';

const previewComponent = DecapCmsWidgetObject.previewComponent;
Expand All @@ -15,5 +15,14 @@ function Widget(opts = {}) {
};
}

export const DecapCmsWidgetList = { Widget, controlComponent, previewComponent };
function ObjectWidget(opts = {}) {
return {
name: 'object',
controlComponent: ObjectControlWrapper,
previewComponent,
...opts,
};
}

export const DecapCmsWidgetList = { Widget, controlComponent, previewComponent, ObjectWidget };
export default DecapCmsWidgetList;
4 changes: 2 additions & 2 deletions website/content/docs/beta-features.md
Expand Up @@ -312,9 +312,9 @@ Supports all of the [`slug` templates](/docs/configuration-options#slug) and:
* `{{media_folder}}` The global `media_folder`.
* `{{public_folder}}` The global `public_folder`.

## List Widget: Variable Types
## List/Object Widget: Variable Types

Before this feature, the [list widget](/docs/widgets/#list) allowed a set of fields to be repeated, but every list item had the same set of fields available. With variable types, multiple named sets of fields can be defined, which opens the door to highly flexible content authoring (even page building) in Decap CMS.
Before this feature, the [list widget](/docs/widgets/#list) and [object widget](/docs/widget/#object) allowed a set of fields to be repeated, but every item had the same set of fields available. With variable types, multiple named sets of fields can be defined, which opens the door to highly flexible content authoring (even page building) in Decap CMS.

**Note: this feature does not yet support default previews and requires [registering a preview template](/docs/customization#registerpreviewtemplate) in order to show up in the preview pane.**

Expand Down