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

Adding then removing from a number widget field causes the field to have an empty string as it's value #2848

Closed
TidyIQ opened this issue Nov 6, 2019 · 3 comments

Comments

@TidyIQ
Copy link

TidyIQ commented Nov 6, 2019

Describe the bug

When using widget: "number, required: false, if you:

  1. Working correctly: Leave the field blank and publish the item, the yaml file will not include the field name under the item. For example, if the field name is foo and there is another bar field that is required, the yaml file will be:
item:
  - bar: 7
  1. Working correctly: Enter a number to the field and republish the item, the yaml file will include the field with the correct value. For example:
item
  - bar: 7
  - foo: 3
  1. NOT WORKING CORRECTLY: Complete step 2 above, then go back to the item and delete the number from the field then republish the item, the yaml file will include the field with an empty string as it's value. For example:
item
  - bar: 7
  - foo: ''

This causes issues when using GraphQL as:

  • The schema type cannot be inferred. In situation 1 above, there is no foo field so attempting to query foo will result in an error. This means schema type must be explicitly set.

  • You can't explicitly set the schema type for foo as it can be either String or Int, and GraphQL doesn't support union scalar types.

To Reproduce

Create a field with widget: "number", required: false then run through the three situations above.

Expected behavior

If no number is entered then it should either leave the field out of the yaml file, or define the value as null.

Applicable Versions:

  • Netlify CMS version: "netlify-cms-app": "^2.9.7"
  • Git provider: File system ("gatsby-source-filesystem": "^2.1.33")
  • OS: Windows 10
  • Browser version: Chrome Version 78.0.3904.87 (Official Build) (64-bit)
  • Node.JS version: v10.15.3

CMS configuration

Relevant sections only:

const config = {
  load_config_file: false,
  media_folder: "static/img",
  public_folder: "img",
  collections: [
    {
      label: "Settings",
      name: "settings",
      delete: false,
      editor: {
        preview: false
      },
      files: [
        {
          label: "Typography",
          name: "typography",
          file: `${__dirname}/data/typography/typography.yaml`,
          fields: [
            {
              label: "Titles (H1)",
              name: "h1",
              widget: "object",
              fields: [
                {
                  label: "Font",
                  name: "fontFamily",
                  widget: "string"
                },
                {
                  label: "Size",
                  name: "size",
                  required: false,
                  widget: "number"
                }
              ]
            }    
          ]
        }
      ]
    },
  ]
};
@TidyIQ
Copy link
Author

TidyIQ commented Nov 12, 2019

Bump, urgently require a fix.

I believe the issue lies here.

It should be:

    if (!isNaN(value)) {
      onChange(value);
    } else {
-     onChange('');
+     onChange(undefined); 
   }

@eur2
Copy link

eur2 commented Nov 14, 2019

@TidyIQ Having the same problem and behavior with the image widget:
#2763

@erquhart
Copy link
Contributor

Closing in favor of #995.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants