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

Components keeping original defaultValue when editing #5123

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

robinmolen
Copy link
Contributor

Closes #5104

Changes

When creating radio component the configuration defined in the component schema will be used. Providing an empty string as the defaultValue. If you leave the defaultValue as an empty string, save this component and edit it again, the defaultValue will change to null. This defaultValue change happens because of how we construct the component data for the edit modal.

In the WebformBuilder.editComponent we receive the component property, which is a simplified representation of the component (a sub-set of all the component information). If the defaultValue is a falsey value, like in this case an empty string, then defaultValue won't be present in this component prop. When reconstructing the component to its full representation, the defaultValue defined in the Formio component schema will be used (null).

To make sure we retain the actual component defaultValue, we combine the original prop (which is the original component data) and the reconstructed component data.

For now i've opted to only added the defaultValue of the original data. Using the entire original object should be save, but to know for sure would require some more testing.

Checklist

Check off the items that are completed or not relevant.

  • Impact on features

    • Checked copying a form
    • Checked import/export of a form
    • Config checks in the configuration overview admin page
    • Problem detection in the admin email digest is handled
  • Release management

    • I have labelled the PR as "needs-backport" accordingly
  • I have updated the translations assets (you do NOT need to provide translations)

    • Ran ./bin/makemessages_js.sh
    • Ran ./bin/compilemessages_js.sh
  • Dockerfile/scripts

    • Updated the Dockerfile with the necessary scripts from the ./bin folder
  • Commit hygiene

    • Commit messages refer to the relevant Github issue
    • Commit messages explain the "why" of change, not the how

The `FormioUtils.fastCloneDeep(component)` makes a new instance of the component (with a new unique id). For the most part this is okay, but when the `defaultValue` is set to a falsey value (like an empty string), the `defaultValue` from `FormioUtils.fastCloneDeep(component)` will be `null`.

For the bug #4659 this was solved by manually correcting the default value of textfield and email components from `null` to `''`. This corrected the problem for these components (and ensured that these components always have a valid default value), but didn't solve the problem for other components.

By merging `original` and `FormioUtils.fastCloneDeep(component)` into one object, we retain the original component configuration and give the `componentCopy` a unique id.
The id must be unique for proper cancelation functionality (if the id of `componentCopy` is the same as the original, an error will be thrown when you edit the component > cancel editing > edit it again)
Copy link

codecov bot commented Feb 26, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.73%. Comparing base (e7e8155) to head (f635fe0).
Report is 14 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #5123   +/-   ##
=======================================
  Coverage   96.73%   96.73%           
=======================================
  Files         775      775           
  Lines       26662    26664    +2     
  Branches     3468     3469    +1     
=======================================
+ Hits        25792    25794    +2     
  Misses        608      608           
  Partials      262      262           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@robinmolen robinmolen marked this pull request as ready for review February 26, 2025 15:55
The previous solution, derived from #4659, is no-longer needed.
- With the previous changes to the `WebformBuilder` (d64fd73) the correct defaultValue is used when editing a component.
- To ensure that the textfield components start with an empty string as `defaultValue`, we can just define it in the schema and point the `get defaultSchema()` to our own schema.

This also makes the textfield components more predictable. If for some reason you set the defaultValue to `null`, then this will be respected.
@robinmolen robinmolen force-pushed the bug/5104-radio-component-default-value-change branch from 339c6c9 to f635fe0 Compare February 26, 2025 16:05
Copy link
Member

@sergei-maertens sergei-maertens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm terrified of this change because of the earlier issues that happened with it, and on how Formio itself figures out the default value for a component: https://github.com/formio/formio.js/blob/bebc2ad73cad138a6de0a8247df47f0085a314cc/src/components/_classes/component/Component.js#L2302

Can you please make sure that all the old reported bugs are not re-introduced and test them manually? You should be able to figure out which ones that are from the commit history/git blame.

@sergei-maertens
Copy link
Member

We should also document the manual JSON editing requirement when backporting the fix. For the master branch, we can do a data migration.

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

Successfully merging this pull request may close these issues.

Radio component defaultValue changes to null after saving. Instead of the desired: "".
2 participants