fix(widgets): fix list object field default values (#1826)
This commit is contained in:
parent
a9f69f9ddc
commit
c765793971
@ -1,4 +1,4 @@
|
|||||||
import { fromJS, List } from 'immutable';
|
import { fromJS, List, Map } from 'immutable';
|
||||||
import { actions as notifActions } from 'redux-notifications';
|
import { actions as notifActions } from 'redux-notifications';
|
||||||
import { serializeValues } from 'Lib/serializeEntryValues';
|
import { serializeValues } from 'Lib/serializeEntryValues';
|
||||||
import { currentBackend } from 'src/backend';
|
import { currentBackend } from 'src/backend';
|
||||||
@ -360,15 +360,40 @@ export function traverseCollectionCursor(collection, action) {
|
|||||||
|
|
||||||
export function createEmptyDraft(collection) {
|
export function createEmptyDraft(collection) {
|
||||||
return dispatch => {
|
return dispatch => {
|
||||||
const dataFields = {};
|
const dataFields = createEmptyDraftData(collection.get('fields', List()));
|
||||||
collection.get('fields', List()).forEach(field => {
|
|
||||||
dataFields[field.get('name')] = field.get('default');
|
|
||||||
});
|
|
||||||
const newEntry = createEntry(collection.get('name'), '', '', { data: dataFields });
|
const newEntry = createEntry(collection.get('name'), '', '', { data: dataFields });
|
||||||
dispatch(emptyDraftCreated(newEntry));
|
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) {
|
export function persistEntry(collection) {
|
||||||
return (dispatch, getState) => {
|
return (dispatch, getState) => {
|
||||||
const state = getState();
|
const state = getState();
|
||||||
|
@ -2,7 +2,6 @@ import PropTypes from 'prop-types';
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import ImmutablePropTypes from 'react-immutable-proptypes';
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
||||||
import styled from 'react-emotion';
|
import styled from 'react-emotion';
|
||||||
import { isBoolean } from 'lodash';
|
|
||||||
import { Toggle, ToggleBackground, colors } from 'netlify-cms-ui-default';
|
import { Toggle, ToggleBackground, colors } from 'netlify-cms-ui-default';
|
||||||
|
|
||||||
const BooleanBackground = styled(ToggleBackground)`
|
const BooleanBackground = styled(ToggleBackground)`
|
||||||
@ -13,7 +12,6 @@ export default class BooleanControl extends React.Component {
|
|||||||
render() {
|
render() {
|
||||||
const {
|
const {
|
||||||
value,
|
value,
|
||||||
field,
|
|
||||||
forID,
|
forID,
|
||||||
onChange,
|
onChange,
|
||||||
classNameWrapper,
|
classNameWrapper,
|
||||||
@ -24,7 +22,7 @@ export default class BooleanControl extends React.Component {
|
|||||||
<div className={classNameWrapper}>
|
<div className={classNameWrapper}>
|
||||||
<Toggle
|
<Toggle
|
||||||
id={forID}
|
id={forID}
|
||||||
active={isBoolean(value) ? value : field.get('defaultValue', false)}
|
active={value}
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
onFocus={setActiveStyle}
|
onFocus={setActiveStyle}
|
||||||
onBlur={setInactiveStyle}
|
onBlur={setInactiveStyle}
|
||||||
|
@ -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))
|
- `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`
|
- `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)
|
- `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`
|
- `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)
|
- `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)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user