Skip to content

Specifying a default value doesn't work for fields in editor components #1406

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

Closed
papandreou opened this issue Jun 5, 2018 · 4 comments · Fixed by #1616
Closed

Specifying a default value doesn't work for fields in editor components #1406

papandreou opened this issue Jun 5, 2018 · 4 comments · Fixed by #1616

Comments

@papandreou
Copy link
Contributor

- 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.

@tomgenoni
Copy link

tomgenoni commented Jun 6, 2018

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" }
    ]
},

@erquhart
Copy link
Contributor

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

@PaulEibensteiner
Copy link

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

@erquhart
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment