feat(widget-object): add "summary" #4291 (#4398)

This commit is contained in:
KoljaTM 2020-10-15 16:55:06 +02:00 committed by GitHub
parent 9e0b8ac4b8
commit 963e775897
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 3 deletions

View File

@ -2,8 +2,9 @@ import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes'; import ImmutablePropTypes from 'react-immutable-proptypes';
import { ClassNames } from '@emotion/core'; import { ClassNames } from '@emotion/core';
import { Map, List } from 'immutable'; import { List, Map } from 'immutable';
import { ObjectWidgetTopBar, lengths, colors } from 'netlify-cms-ui-default'; import { colors, lengths, ObjectWidgetTopBar } from 'netlify-cms-ui-default';
import { stringTemplate } from 'netlify-cms-lib-widgets';
const styleStrings = { const styleStrings = {
nestedObjectControl: ` nestedObjectControl: `
@ -126,6 +127,13 @@ export default class ObjectControl extends React.Component {
return this.controlFor(singleField); return this.controlFor(singleField);
}; };
objectLabel = () => {
const { value, field } = this.props;
const label = field.get('label', field.get('name'));
const summary = field.get('summary');
return summary ? stringTemplate.compileStringTemplate(summary, null, '', value) : label;
};
render() { render() {
const { field, forID, classNameWrapper, forList, hasError } = this.props; const { field, forID, classNameWrapper, forList, hasError } = this.props;
const collapsed = forList ? this.props.collapsed : this.state.collapsed; const collapsed = forList ? this.props.collapsed : this.state.collapsed;
@ -159,6 +167,7 @@ export default class ObjectControl extends React.Component {
<ObjectWidgetTopBar <ObjectWidgetTopBar
collapsed={collapsed} collapsed={collapsed}
onCollapseToggle={this.handleCollapseToggle} onCollapseToggle={this.handleCollapseToggle}
heading={collapsed && this.objectLabel()}
/> />
)} )}
<div <div

View File

@ -11,6 +11,7 @@ The object widget allows you to group multiple widgets together, nested under a
* `default`: you can set defaults within each sub-field's configuration * `default`: you can set defaults within each sub-field's configuration
* `collapsed`: if added and labeled `true`, collapse the widget's content by default * `collapsed`: if added and labeled `true`, collapse the widget's content by default
* `summary`: specify the label displayed when the object is collapsed
* `fields`: (**required**) a nested list of widget fields to include in your widget * `fields`: (**required**) a nested list of widget fields to include in your widget
* **Example:** * **Example:**
@ -18,6 +19,7 @@ The object widget allows you to group multiple widgets together, nested under a
- label: "Profile" - label: "Profile"
name: "profile" name: "profile"
widget: "object" widget: "object"
summary: '{{fields.name}}: {{fields.birthdate}}'
fields: fields:
- {label: "Public", name: "public", widget: "boolean", default: true} - {label: "Public", name: "public", widget: "boolean", default: true}
- {label: "Name", name: "name", widget: "string"} - {label: "Name", name: "name", widget: "string"}