Open
Conversation
|
I would be very interested in such a feature. For now I'm using this callback, but this use case seems to be common enough to have it in core so that more edge cases can be handled: export const preserveFormInputs: MorphCallback = (oldNode, newNode) => {
if (oldNode instanceof HTMLInputElement && newNode instanceof HTMLInputElement) {
// Preserve user-entered values
if (newNode.value !== oldNode.value) {
newNode.value = oldNode.value;
oldNode.dispatchEvent(new Event('change', { bubbles: true }));
}
// Preserve checked state
if ((oldNode.type === 'checkbox' || oldNode.type === 'radio') && newNode.checked !== oldNode.checked) {
newNode.checked = oldNode.checked;
oldNode.dispatchEvent(new Event('change', { bubbles: true }));
}
}
if (oldNode instanceof HTMLTextAreaElement && newNode instanceof HTMLTextAreaElement) {
if (newNode.value !== oldNode.value) {
newNode.value = oldNode.value;
oldNode.dispatchEvent(new Event('change', { bubbles: true }));
}
}
if (oldNode instanceof HTMLSelectElement && newNode instanceof HTMLSelectElement) {
if (newNode.value !== oldNode.value) {
newNode.value = oldNode.value;
oldNode.dispatchEvent(new Event('change', { bubbles: true }));
}
}
return true;
}@MichaelWest22 in our implementation we also send the <quantity-picker>
<button type="button" disabled>Minus</button>
<input type="text" value="1" />
<button type="button">Plus</button>
</quantity-picker>To synchronize the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Here is a possible change to implement a new keepInputValues option while keeping all the old input handling behavior the same
With this setting turned on we are able to optimize the performance of all node updates where there are no innerHTML changes as we can just skip morphing. Before this was not possible because the old sync input value routine would apply changes to reset input and textarea user input state even when there are no no actual node changes to morph. But with the new keepInputValues option this is not an issue anymore. It does mean this currently has to be opt in to not break things. It may be possible to remove the two old input preservation config flags as they may not be needed in a future version as this option mostly replaces their need.
The idea with the keepInputValues is to just skip forcing syncing the input values logic we have now and just now detect if the default value supplied is different than the last one and only update the users input value if this changes. This allows you to push out new identical form updates that will morph without losing any user input. However it has one big downside and this is that forms will not reset on morph so after form submit if you return the same blank form it will retain the old existing input values which may not be what is expected. This is why it is currently an opt in config value so it won't break existing use cases.