fix(widgets): fix list object field default values (#1826)

This commit is contained in:
Bartholomew 2018-11-02 15:55:08 +01:00 committed by Shawn Erquhart
parent a9f69f9ddc
commit c765793971
3 changed files with 32 additions and 9 deletions

View File

@ -1,4 +1,4 @@
import { fromJS, List } from 'immutable';
import { fromJS, List, Map } from 'immutable';
import { actions as notifActions } from 'redux-notifications';
import { serializeValues } from 'Lib/serializeEntryValues';
import { currentBackend } from 'src/backend';
@ -360,15 +360,40 @@ export function traverseCollectionCursor(collection, action) {
export function createEmptyDraft(collection) {
return dispatch => {
const dataFields = {};
collection.get('fields', List()).forEach(field => {
dataFields[field.get('name')] = field.get('default');
});
const dataFields = createEmptyDraftData(collection.get('fields', List()));
const newEntry = createEntry(collection.get('name'), '', '', { data: dataFields });
dispatch(emptyDraftCreated(newEntry));
};
}
function createEmptyDraftData(fields) {
return fields.reduce((acc, item) => {
const subfields = item.get('field') || item.get('fields');
const list = item.get('widget') == 'list';
const defaultValue = item.get('default');
if (List.isList(subfields)) {
acc[item.get('name')] = list
? [createEmptyDraftData(subfields)]
: createEmptyDraftData(subfields);
return acc;
}
if (Map.isMap(subfields)) {
acc[item.get('name')] = list
? [createEmptyDraftData([subfields])]
: createEmptyDraftData([subfields]);
return acc;
}
if (defaultValue) {
acc[item.get('name')] = defaultValue;
}
return acc;
}, {});
}
export function persistEntry(collection) {
return (dispatch, getState) => {
const state = getState();

View File

@ -2,7 +2,6 @@ import PropTypes from 'prop-types';
import React from 'react';
import ImmutablePropTypes from 'react-immutable-proptypes';
import styled from 'react-emotion';
import { isBoolean } from 'lodash';
import { Toggle, ToggleBackground, colors } from 'netlify-cms-ui-default';
const BooleanBackground = styled(ToggleBackground)`
@ -13,7 +12,6 @@ export default class BooleanControl extends React.Component {
render() {
const {
value,
field,
forID,
onChange,
classNameWrapper,
@ -24,7 +22,7 @@ export default class BooleanControl extends React.Component {
<div className={classNameWrapper}>
<Toggle
id={forID}
active={isBoolean(value) ? value : field.get('defaultValue', false)}
active={value}
onChange={onChange}
onFocus={setActiveStyle}
onBlur={setInactiveStyle}

View File

@ -189,7 +189,7 @@ The `fields` option maps editor UI widgets to field-value pairs in the saved fil
- `name` (required): unique identifier for the field, used as the key when referenced in other contexts (like the [relation widget](../widgets/#relation))
- `label`: label for the field in the editor UI; defaults to the value of `name`
- `widget`: defines editor UI and inputs and file field data types; details in [Widgets](../widgets)
- `default`: specify a default value for a field; available for most widget types (see [Widgets](../widgets) for details on each widget type)
- `default`: specify a default value for a field; available for most widget types (see [Widgets](../widgets) for details on each widget type). Please note that field default value only works for folder collection type.
- `required`: specify as `false` to make a field optional; defaults to `true`
- `pattern`: add field validation by specifying a list with a regex pattern and an error message; more extensive validation can be achieved with [custom widgets](../custom-widgets/#advanced-field-validation)