-
Notifications
You must be signed in to change notification settings - Fork 373
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
Comments
Thanks for the report. Yes, that'd make sense if the respective property is not initialized. |
- 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.
- 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.
* 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.
@johnmorrell We have made changes that allow options to be passed in into the |
Please re-open if you experience any issues. |
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:
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. |
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. |
Thanks for the update. We'll look into it again. |
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
I could verify that this is indeed what happens for strings. This is caused by the fact that we are converting empty strings to |
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.
|
@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"
}
}
} |
Thanks @AlexandraBuzila. I understand the issue now, and thanks for the workaround. |
- 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
I'm trying to do the same as the original post (display default values for enum). Yet, setting up a My repo to try and reproduce this is: Schema JSON for the colors with default value: UI Schema JSON: Deployment on GitHub Pages to see that the form doesn't auto-populate with "green": I must be missing something. Apologies in advance, new to JSONForms, React, and JavaScript. JSONForms version: 2.2.3 |
@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 const ajv = createAjv({
useDefaults: true
})
store.dispatch(Actions.init(data, schema, uischema, ajv)); |
@edgarmueller Awesome, thanks! I'll try that out. |
That did it, much appreciated! |
@edgarmueller I suggest to close this due to f515b14 , what do you think? |
@eneufeld Sounds good to me! Closing. |
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:
The text was updated successfully, but these errors were encountered: