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

Get SchemaWidget field factories from backend #2651

Merged
merged 3 commits into from
Sep 7, 2021
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

### Internal

- Get SchemaWidget field factories from backend @avoinea
## 13.14.0 (2021-09-02)

### Feature
Expand Down
56 changes: 37 additions & 19 deletions src/components/manage/Widgets/SchemaWidget.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import move from 'lodash-move';
import { Confirm, Form, Grid, Icon, Message, Segment } from 'semantic-ui-react';
import { defineMessages, injectIntl } from 'react-intl';
import { DragDropContext, Draggable, Droppable } from 'react-beautiful-dnd';
import { getFieldsVocabulary } from '@plone/volto/helpers';

import {
Field,
Expand Down Expand Up @@ -129,18 +128,7 @@ const messages = defineMessages({
});

/**
* Makes a list of field types formated for select widget
* @param {Object[]} listOfTypes array of strings
* @param {*} intl
* @returns {Object[]} example [['text', 'text']]
*/
const makeFieldTypes = (listOfTypes, intl) => {
const result = listOfTypes.map((type) => [type.title, type.title]);
return result;
};

/**
* Makes a list of fieldset types formated for select widget
* Makes a list of fieldset types formatted for select widget
* @param {Object[]} listOfTypes array of strings
* @param {*} intl
* @returns {Object[]} example [['default', 'default']]
Expand Down Expand Up @@ -172,23 +160,32 @@ const schemaField = (factory, intl, fieldsets) => ({
return ['maxLength'];
case 'URL':
case 'Password':
case 'label_password_field':
case 'Email':
case 'label_email':
return ['minLength', 'maxLength'];
case 'Integer':
case 'label_integer_field':
return ['minimum', 'maximum'];
case 'Floating-point number':
case 'label_float_field':
case 'Date/Time':
case 'label_datetime_field':
case 'Date':
case 'label_date_field':
case 'File':
case 'File Upload':
case 'Image':
case 'Yes/No':
case 'label_boolean_field':
case 'JSONField':
case 'Relation Choice':
case 'Relation List':
return [];
case 'Multiple Choice':
case 'label_multi_choice_field':
case 'Choice':
case 'label_choice_field':
return ['values'];
default:
return ['minLength', 'maxLength'];
Expand Down Expand Up @@ -228,7 +225,9 @@ const schemaField = (factory, intl, fieldsets) => ({
};
case 'URL':
case 'Password':
case 'label_password_field':
case 'Email':
case 'label_email':
return {
minLength: {
type: 'integer',
Expand All @@ -240,6 +239,7 @@ const schemaField = (factory, intl, fieldsets) => ({
},
};
case 'Integer':
case 'label_integer_field':
return {
minimum: {
type: 'integer',
Expand All @@ -251,18 +251,24 @@ const schemaField = (factory, intl, fieldsets) => ({
},
};
case 'Floating-point number':
case 'label_float_field':
case 'Date/Time':
case 'label_datetime_field':
case 'Date':
case 'label_date_field':
case 'File':
case 'File Upload':
case 'Image':
case 'Yes/No':
case 'label_boolean_field':
case 'JSONField':
case 'Relation Choice':
case 'Relation List':
return {};
case 'Multiple Choice':
case 'label_multi_choice_field':
case 'Choice':
case 'label_choice_field':
return {
values: {
type: 'string',
Expand Down Expand Up @@ -327,7 +333,7 @@ const getItemStyle = (isDragging, draggableStyle) => ({
// change background colour if dragging
background: isDragging ? 'white' : 'transparent',

// styles we need to apply on draggables
// styles we need to apply on draggable
...draggableStyle,
});

Expand Down Expand Up @@ -514,18 +520,21 @@ class SchemaWidget extends Component {
...((factory) => {
switch (factory) {
case 'Date/Time':
case 'label_datetime_field':
return {
type: 'string',
widget: 'datetime',
factory,
};
case 'Date':
case 'label_date_field':
return {
type: 'string',
widget: 'date',
factory,
};
case 'Email':
case 'label_email':
return {
type: 'string',
widget: 'email',
Expand All @@ -538,11 +547,13 @@ class SchemaWidget extends Component {
factory,
};
case 'Floating-point number':
case 'label_float_field':
return {
type: 'number',
factory,
};
case 'Integer':
case 'label_integer_field':
return {
type: 'integer',
factory,
Expand All @@ -559,6 +570,7 @@ class SchemaWidget extends Component {
factory,
};
case 'Multiple Choice':
case 'label_multi_choice_field':
return {
type: 'array',
factory,
Expand All @@ -569,6 +581,7 @@ class SchemaWidget extends Component {
factory,
};
case 'Choice':
case 'label_choice_field':
return {
type: 'string',
choices: [],
Expand All @@ -580,6 +593,7 @@ class SchemaWidget extends Component {
factory,
};
case 'Password':
case 'label_password_field':
return {
type: 'string',
widget: 'password',
Expand All @@ -598,6 +612,7 @@ class SchemaWidget extends Component {
factory,
};
case 'Yes/No':
case 'label_boolean_field':
return {
type: 'boolean',
factory,
Expand Down Expand Up @@ -766,7 +781,9 @@ class SchemaWidget extends Component {

const multiple =
this.props.value.properties[this.state.editField.id]?.factory ===
'Multiple Choice';
'Multiple Choice' ||
this.props.value.properties[this.state.editField.id]?.factory ===
'label_multi_choice_field';
const result = {
...this.props.value,
fieldsets: formattedValues.parentFieldSet
Expand Down Expand Up @@ -1065,7 +1082,6 @@ class SchemaWidget extends Component {
if (!this.props.value) {
return '';
}
const vocabularyFields = getFieldsVocabulary();
const nonUserCreatedFields = this.props.value.fieldsets[
this.state.currentFieldset
].fields.filter(
Expand All @@ -1081,7 +1097,7 @@ class SchemaWidget extends Component {
const lastUserCreatedFieldsIndex = hasChangeNote
? this.props.value.fieldsets[this.state.currentFieldset].fields.length - 1
: this.props.value.fieldsets[this.state.currentFieldset].fields.length;
// fields that were not created by the user, but are part of a behaviour
// fields that were not created by the user, but are part of a behavior
const makeNonUserFields = () =>
map(
this.props.value.fieldsets[this.state.currentFieldset].fields.slice(
Expand All @@ -1101,7 +1117,6 @@ class SchemaWidget extends Component {
draggable={false}
isDisabled={true}
order={index}
vocabularyFields={vocabularyFields}
onDelete={this.onShowDeleteField}
onChange={this.onChangeDefaultValue}
value={this.props.value.properties[field].default}
Expand Down Expand Up @@ -1311,8 +1326,11 @@ class SchemaWidget extends Component {
properties: {
factory: {
type: 'string',
factory: 'Choice',
title: this.props.intl.formatMessage(messages.type),
choices: makeFieldTypes(vocabularyFields?.items),
vocabulary: {
'@id': `Fields`,
},
},
title: {
type: 'string',
Expand Down