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 5 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions dev-test/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,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 @@ -50,7 +49,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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;
Loading