Netlify-cms: Specifying a default value doesn't work for fields in editor components

Created on 5 Jun 2018  路  4Comments  路  Source: netlify/netlify-cms

- Do you want to request a feature or report a bug?

bug

- What is the current behavior?

string Fields in editor components that specify the default option don't get populated with the default value when a new component is added. Seems to happen with all widgets (I tried with string, number, and boolean).

- If the current behavior is a bug, please provide the steps to reproduce.

  1. Declare an editor component that contains a field with a default:
CMS.registerEditorComponent({
  id: "youtube",
  label: "Youtube",
  fields: [{
    name: 'id',
    label: 'Youtube Video ID',
    widget: 'string',
    default: 'dQw4w9WgXcQ'
  }],
  pattern: /^youtube (\S+)$/,
  fromBlock: function(match) {
    return { id: match[1] };
  },
  toBlock: function(obj) {
    return 'youtube ' + obj.id;
  },
  // Preview output for this component. Can either be a string or a React component
  // (component gives better render performance)
  toPreview: function(obj) {
    return (
      '<img src="http://img.youtube.com/vi/' + obj.id + '/maxresdefault.jpg" alt="Youtube Video"/>'
    );
  }
});
  1. Add an instance of the component to a markdown editor

Observe that the field is empty rather than being populated with the default value.

- What is the expected behavior?

That the field is populated with the specified default value, similar to how default values work for fields defined on collections.

As a sidecar feature request, it would be nice to be able to specify the default value as a function that returns it -- I would like to use a hidden field for automatically generating a uuid.

- Please mention your versions where applicable.

Netlify CMS version: 1.8.4
Browser version: Chrome 67.0.3396.62/OSX

Node.JS version: 9.5.0
Operating System: OSX 10.13.4 (High Sierra)

- Please link or paste your config.yml below if applicable.

extensioneditor-components beginner good first issue bug

Most helpful comment

Same problem also appears to exist when using the select editor component. No default value is passed through.

{
    name: "type",
    label: "Type",
    widget: "select",
    default: "basic", // <-- doesn't apply
    options: [
        { label: "Basic", value: "basic" },
        { label: "Warning", value: "warning" },
        { label: "Accessibility", value: "accessibility" }
    ]
},

All 4 comments

Same problem also appears to exist when using the select editor component. No default value is passed through.

{
    name: "type",
    label: "Type",
    widget: "select",
    default: "basic", // <-- doesn't apply
    options: [
        { label: "Basic", value: "basic" },
        { label: "Warning", value: "warning" },
        { label: "Accessibility", value: "accessibility" }
    ]
},

To clarify, it looks like default values aren't being handled at all in editor component fields.

Don't know if that's related, but when you don't put something in a text or string field, it outputs "undefined" instead of an empty string as described in the docs:

default: accepts a string; defaults to an empty string

That means there is no way of adding a custom editor component where a field can just be left out by the user, which is really annoying

@PaulEibensteiner agreed, that's discussed in depth in #1449, though I understand it impacts editor component fields differently than standard fields.

Was this page helpful?
0 / 5 - 0 ratings