From d60df8786d7ea01af94c86a6e889654ce20e53ca Mon Sep 17 00:00:00 2001
From: Sanjoth Rai <36308078+sj-rai@users.noreply.github.com>
Date: Wed, 14 Jul 2021 20:13:50 +0530
Subject: [PATCH] feat(list): Add heading for list widgets (#5544)
---
.../src/ListControl.js | 35 ++++++++++++++++---
.../src/__tests__/ListControl.spec.js | 8 ++---
2 files changed, 34 insertions(+), 9 deletions(-)
diff --git a/packages/netlify-cms-widget-list/src/ListControl.js b/packages/netlify-cms-widget-list/src/ListControl.js
index fc5af637..122a0c71 100644
--- a/packages/netlify-cms-widget-list/src/ListControl.js
+++ b/packages/netlify-cms-widget-list/src/ListControl.js
@@ -4,11 +4,17 @@ import ImmutablePropTypes from 'react-immutable-proptypes';
import styled from '@emotion/styled';
import { css, ClassNames } from '@emotion/core';
import { List, Map, fromJS } from 'immutable';
-import { partial, isEmpty } from 'lodash';
+import { partial, isEmpty, uniqueId } from 'lodash';
import uuid from 'uuid/v4';
import { SortableContainer, SortableElement, SortableHandle } from 'react-sortable-hoc';
import NetlifyCmsWidgetObject from 'netlify-cms-widget-object';
-import { ListItemTopBar, ObjectWidgetTopBar, colors, lengths } from 'netlify-cms-ui-default';
+import {
+ ListItemTopBar,
+ ObjectWidgetTopBar,
+ colors,
+ lengths,
+ FieldLabel,
+} from 'netlify-cms-ui-default';
import { stringTemplate, validations } from 'netlify-cms-lib-widgets';
import {
@@ -87,6 +93,14 @@ function validateItem(field, item) {
return true;
}
+function LabelComponent({ field, isActive, hasErrors, uniqueFieldId, isFieldOptional, t }) {
+ const label = `${field.get('label', field.get('name'))}`;
+ return (
+
+ {label} {`${isFieldOptional ? ` (${t('editor.editorControl.field.optional')})` : ''}`}
+
+ );
+}
export default class ListControl extends React.Component {
validations = [];
@@ -164,6 +178,7 @@ export default class ListControl extends React.Component {
}
};
+ uniqueFieldId = uniqueId(`${this.props.field.get('name')}-field-`);
/**
* Always update so that each nested widget has the option to update. This is
* required because ControlHOC provides a default `shouldComponentUpdate`
@@ -493,6 +508,7 @@ export default class ListControl extends React.Component {
resolveWidget,
parentIds,
forID,
+ t,
} = this.props;
const { itemsCollapsed, keys } = this.state;
@@ -500,20 +516,29 @@ export default class ListControl extends React.Component {
const key = keys[index];
let field = this.props.field;
const hasError = this.hasError(index);
-
- if (this.getValueType() === valueTypes.MIXED) {
+ const isVariableTypesList = this.getValueType() === valueTypes.MIXED;
+ if (isVariableTypesList) {
field = getTypedFieldForValue(field, item);
if (!field) {
return this.renderErroneousTypedItem(index, item);
}
}
-
return (
+ {isVariableTypesList && (
+
+ )}
{
],
});
- const { getByText } = render(
+ const { getAllByText } = render(
,
);
- expect(getByText('type_1_object')).toBeInTheDocument();
+ expect(getAllByText('type_1_object')[1]).toBeInTheDocument();
});
it('should use label when no summary is configured for mixed types', () => {
@@ -327,14 +327,14 @@ describe('ListControl', () => {
],
});
- const { getByText } = render(
+ const { getAllByText } = render(
,
);
- expect(getByText('Type 1 Object')).toBeInTheDocument();
+ expect(getAllByText('Type 1 Object')[1]).toBeInTheDocument();
});
it('should use summary when configured for mixed types', () => {