surveyjs / survey-creator

Scalable open-source survey software to generate dynamic JSON-driven forms within your JavaScript application. The form builder features a drag-and-drop UI, CSS Theme Editor, and GUI for conditional logic and form branching.
https://surveyjs.io/open-source
Other
911 stars 373 forks source link

Choices - Implement an event which fires when a choice is being deleted #3801

Closed JosephBrooksbank closed 1 year ago

JosephBrooksbank commented 1 year ago

Are you requesting a feature, reporting a bug or ask a question?

Reporting a bug

What is the current behavior?

event listeners added to 'creator.onCollectionItemDeleting' in SC2 are fired for all choices on a question when the 'choices' property panel is opened, but not when the items are deleted.

What is the expected behavior?

The event listeners fire when items in the collection are deleted, and not when the properties panel is opened.

How would you reproduce the current behavior (if this is a bug)?

See CodeSandbox, select the 'Which of the following features do you value the most?' question, open the properties panel, click the 'choices' option. A bunch of alerts will fire from the event listener. Once those have fired, delete a choice using the properties panel, and the event won't fire.

Reordering the choices using either the properties panel or the adorners also seems to cause this event to fire.

Provide the test code and the tested page URL (if applicable)

Tested page URL: https://codesandbox.io/s/oncollectionitemdeleting-j2j4fs?file=/src/App.tsx

Specify your

JosephBrooksbank commented 1 year ago

Closing after reading the documentation more, it is apparently obsolete. I might suggest using

/**
 * @deprecated Please use onCollectionItemAllowOperations
 */

For more obvious deprecation warnings when attempting to use.

EDIT: I would rather appreciate if the functionality from onCollectionItemDeleting could be replicated in some way. From my initial look at onCollectionItemAllowOperations runs when the choices are rendered, with no callback available specifically when an item is deleted. In Survey Creator 1 we currently use the original callback to do real-time validation and block the user with a modal if there are concerns. Is this functionality reproduceable with the new system? Reopening the ticket for now.

JaneSjs commented 1 year ago

Hello @JosephBrooksbank, Thank you for your input. I'll mark the function as deprecated.

From my initial look at onCollectionItemAllowOperations runs when the choices are rendered, with no callback available specifically when an item is deleted.

Yes, correct. The creator.onCollectionItemAllowOperations function is fired before items are rendered. With this function, you can consider whether or not an item can be edited or deleted. Upon that, an edit/delete button is available for a particular choices.

In Survey Creator 1 we currently use the original callback to do real-time validation and block the user with a modal if there are concerns. Is this functionality reproduceable with the new system?

Unfortunately, no. In Survey Creator, you can consider whether or not an item can be deleted when the item is rendered, using the onCollectionItemAllowOperations function.

Please let me know if any question remains.

JosephBrooksbank commented 1 year ago

Thanks for the response.

This is a major loss in functionality for us. We did not only check if deletion was possible using that callback, but also ran side-effect functionality to alter other parts of the survey JSON.

JaneSjs commented 1 year ago

Hello Joseph, Thank you for the update. I'll discuss this with the team for a possible way to handle this. To allow us consider the best option, would you please describe your usage scenario in greater detail? From what I gather, you cannot consider whether or not a choice can be deleted when it's being rendered. Instrad, you consider this when an item is being deleted, correct? Which additional actions do you when item deletion was cancelled?

JosephBrooksbank commented 1 year ago

Hi, sure. We maintain a list of 'dependent' questions, as in questions that contain a visibleIf statement involving the current question. When the user tries to delete the question or choice (in dropdowns and radiobuttons), it first brings up a modal that says Delete? This will also delete any questions that depend on it. { List of questions that depend on it }.

When the user confirms on the modal, the question or choice is deleted, along with any questions that contain visibleIf statements involving that question/choice. This decision was made to keep surveys from entering a weird state where small dependent questions like 'please explain' are not accidentally left in.

JaneSjs commented 1 year ago

Hi @JosephBrooksbank, Thank you. This makes sence. I'll discuss this with the team and get back to you shortly.

JaneSjs commented 1 year ago

Hello @JosephBrooksbank, I discussed this with the team. We'll consider an event which will be raised when a choice is being removed.

Thank you