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

Preselect with default schema values #1193

Closed
johnmorrell opened this issue Dec 13, 2018 · 16 comments
Closed

Preselect with default schema values #1193

johnmorrell opened this issue Dec 13, 2018 · 16 comments
Assignees
Milestone

Comments

@johnmorrell
Copy link

For enumerated properties presented as a drop down list, where a default value is specified in the schema, it would make sense to pre-select that value. For example, 'red' should be pre selected:

      "colours":{
         "type":"string",
         "enum":[
            "red",
            "green",
            "blue"
         ],
         "default":"red"
      }
@edgarmueller
Copy link
Contributor

Thanks for the report. Yes, that'd make sense if the respective property is not initialized.

@edgarmueller edgarmueller added this to the 2.1.0 milestone Dec 14, 2018
@edgarmueller edgarmueller modified the milestones: 2.1.0, 2.2.0 Dec 20, 2018
AlexandraBuzila added a commit to AlexandraBuzila/jsonforms that referenced this issue Jan 14, 2019
- Adds options to createAjv. Clients that want to have preselected
default values should pass in the 'useDefaults: true' option to the
createAjv method and use the Ajv object in the Actions.init call.
AlexandraBuzila added a commit to AlexandraBuzila/jsonforms that referenced this issue Jan 21, 2019
- Adds options to createAjv. Clients that want to have preselected
default values should pass in the 'useDefaults: true' option to the
createAjv method and use the Ajv object in the Actions.init call.
AlexandraBuzila added a commit to AlexandraBuzila/jsonforms that referenced this issue Jan 21, 2019
AlexandraBuzila added a commit to AlexandraBuzila/jsonforms that referenced this issue Jan 21, 2019
AlexandraBuzila added a commit to AlexandraBuzila/jsonforms that referenced this issue Jan 22, 2019
edgarmueller pushed a commit that referenced this issue Jan 23, 2019
* Preselect with default schema values #1193
- Allow for options to be passed into `createAjv`. Clients that want to have preselected
default values should pass in the 'useDefaults: true' option to the
createAjv method and use the Ajv object in the Actions.init call.
@edgarmueller
Copy link
Contributor

@johnmorrell We have made changes that allow options to be passed in into the createAjv function. This enables us to use the useDefaults: true option, which should cover your use case (it's probably best if you take a look at the changes in #1224). Please let us know whether this solves your issue. This change will be part of the 2.2.0 release.

@edgarmueller
Copy link
Contributor

Please re-open if you experience any issues.

@johnmorrell
Copy link
Author

johnmorrell commented Jan 25, 2019

Thank you @edgarmueller, I have taken a look at this in v2.1.1-alpha.3

I can see that this is working for regular string types, but it does not appear to support the case where the string is of an enum type, eg:

"colours":{
  "type":"string",
    "enum":[
      "red",
      "green",
      "blue"
  ],
   "default":"red"
}

I think the ideal behaviour would be that not only would the default value be pre selected but also there is not the option to set 'no value' since it is an enum.

@johnmorrell
Copy link
Author

There is also an unusual behaviour when a default is set for basic strings (react/material).

If you select the field and try to delete the value, it deletes up to the last character then the default value returns. This is also the case if you select all of the text and attempt to delete.

@edgarmueller
Copy link
Contributor

Thanks for the update. We'll look into it again.

@AlexandraBuzila
Copy link
Member

Hi @johnmorrell! I was not able to reproduce the problem with the enums that you mentioned. We also have a test case that verifies the scenario that you mentioned: https://github.com/eclipsesource/jsonforms/blob/master/packages/core/test/util/renderer.test.ts#L593
If you can still reproduce this on your end, could you let me know, if possible, how is your example different than what we test there?

There is also an unusual behaviour when a default is set for basic strings (react/material).

If you select the field and try to delete the value, it deletes up to the last character then the default value returns. This is also the case if you select all of the text and attempt to delete.

I could verify that this is indeed what happens for strings. This is caused by the fact that we are converting empty strings to undefined (see #1168) and the undefined value is instantly converted to the default one. We need to decide how to handle this.

@johnmorrell
Copy link
Author

Hi @AlexandraBuzila, thanks for your response. I took another look at this and it seems the enum default values do populate correctly when the enum is defined inline in the schema, but not when they are referenced.

The following schema demonstrates this, where (using JSONForms v2.2.0) I see the first correctly preselect the default value, but not the second.

{
   "type":"object",
   "properties":{
      "color":{
         "type":"string",
         "enum":[
            "red",
            "green",
            "blue"
         ],
         "default":"green"
      },
      "referencedColor":{
         "$ref":"#/definitions/referencedColor"
      }
   },
   "definitions":{
      "referencedColor":{
         "type":"string",
         "enum":[
            "red",
            "green",
            "blue"
         ],
         "default":"green"
      }
   }
}

@AlexandraBuzila
Copy link
Member

@johnmorrell I just had a look at references and defaults. This is indeed not working and it seems to be a limitation of ajv: ajv-validator/ajv#337 (comment)

One way of working around this would be to add a default alongside your reference:

{
   "type":"object",
   "properties":{
      "color":{
         "type":"string",
         "enum":[
            "red",
            "green",
            "blue"
         ],
         "default":"green"
      },
      "referencedColor":{
         "$ref":"#/definitions/referencedColor",
         "default":"blue"
      }
   },
   "definitions":{
      "referencedColor":{
         "type":"string",
         "enum":[
            "red",
            "green",
            "blue"
         ],
         "default":"green"
      }
   }
}

@johnmorrell
Copy link
Author

Thanks @AlexandraBuzila. I understand the issue now, and thanks for the workaround.

@edgarmueller edgarmueller modified the milestones: 2.3.0, 2.2.2 Mar 8, 2019
@edgarmueller edgarmueller modified the milestones: 2.2.2, 2.2.3 Mar 21, 2019
AlexandraBuzila added a commit to AlexandraBuzila/jsonforms that referenced this issue Apr 26, 2019
- add schema parameter to handleChange, addItem and remoteItems control
and array props
- add clear button to material cells and controls for elements with
default values
@edgarmueller edgarmueller modified the milestones: 2.2.3, 2.3.0 Apr 26, 2019
@marshallmcdonnell
Copy link

I'm trying to do the same as the original post (display default values for enum). Yet, setting up a jsonforms-react-seed to replicate this example for color does not have the pre-selection.

My repo to try and reproduce this is:
https://github.com/marshallmcdonnell/jsonforms-react-default-test

Schema JSON for the colors with default value:
https://github.com/marshallmcdonnell/jsonforms-react-default-test/blob/master/src/schema.json

UI Schema JSON:
https://github.com/marshallmcdonnell/jsonforms-react-default-test/blob/master/src/uischema.json

Deployment on GitHub Pages to see that the form doesn't auto-populate with "green":
https://marshallmcdonnell.github.io/jsonforms-react-default-test/

I must be missing something. Apologies in advance, new to JSONForms, React, and JavaScript.

JSONForms version: 2.2.3

@edgarmueller
Copy link
Contributor

edgarmueller commented May 3, 2019

@marshallmcdonnell Sorry for the late reply: JSON Forms utilizies ajv to set defaults, so you need to create a custom instance and hand it in. In the src/index.tsx file, update the init action call to the following:

const ajv = createAjv({
  useDefaults: true
})
store.dispatch(Actions.init(data, schema, uischema, ajv));

@marshallmcdonnell
Copy link

@edgarmueller Awesome, thanks! I'll try that out.

@marshallmcdonnell
Copy link

That did it, much appreciated!

@eneufeld
Copy link
Member

@edgarmueller I suggest to close this due to f515b14 , what do you think?

@edgarmueller
Copy link
Contributor

@eneufeld Sounds good to me! Closing.

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

No branches or pull requests

5 participants