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

Preserve input values in cache #666

Merged
merged 1 commit into from
Aug 5, 2022

Conversation

seanpdoyle
Copy link
Contributor

@seanpdoyle seanpdoyle commented Aug 4, 2022

Re-submission of #70

This change will save the values of <input>s, <textarea>s, and
<select>s when creating a snapshot so that when you navigate back or
forward in history, form fields will not be reset.

This behavior was already present prior to 5dfc79e for <input>s
and <textarea>s but not <select>s (see
turbolinks/turbolinks#238).

Copy link
Member

@marcoroth marcoroth left a comment

Choose a reason for hiding this comment

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

Personally, I feel like this is more readable

src/core/drive/page_snapshot.ts Outdated Show resolved Hide resolved
src/core/drive/page_snapshot.ts Outdated Show resolved Hide resolved
Re-submission of hotwired#70

This change will save the values of `<input>`s, `<textarea>`s, and
`<select>`s when creating a snapshot so that when you navigate back or
forward in history, form fields will not be reset.

This behavior was already present prior to [5dfc79e][] for `<input>`s
and `<textarea>`s but not `<select>`s (see
[turbolinks/turbolinks#238][]).

[5dfc79e]: hotwired@5dfc79e
[turbolinks/turbolinks#238]: turbolinks/turbolinks#238
@seanpdoyle seanpdoyle force-pushed the preserve-input-values branch from 5ae9df1 to 712c268 Compare August 5, 2022 11:21
@seanpdoyle
Copy link
Contributor Author

seanpdoyle commented Aug 5, 2022

Thank you for the review @marcoroth. I've re-structured the <select> cloning to avoid doubly index-based iteration and Array.from(...).forEach, and instead only coerce the inner HTMLOptionsCollection, since it doesn't support .entries().

@marcoroth
Copy link
Member

Beautiful, that's even better!

@dhh dhh merged commit f72a40c into hotwired:main Aug 5, 2022
@seanpdoyle seanpdoyle deleted the preserve-input-values branch August 5, 2022 14:18
for (const [optionIndex, option] of Array.from(source.options).entries()) {
clonedSelectElements[index].options[optionIndex].selected = option.selected
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI, the following approach is around 3x to 4x faster. Not an issue for small selects, but if the page contains large selects (e.g. Country Selector) it adds up.

for (const [index, source] of selectElements.entries()) {
  [...clonedSelectElements[index].selectedOptions].forEach(option => { option.selected = false });
  [...source.selectedOptions].forEach(option => clonedSelectElements[index].options[option.index].selected = true)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would it be even faster if the [...] syntax were replaced with a for ... of loop? I can open a follow-up PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've opened #669. Thank you!

seanpdoyle added a commit to seanpdoyle/turbo that referenced this pull request Aug 5, 2022
Follow-up to hotwired#666

Following [a suggestion][] provided by review of hotwired#666, this commit
remove unnecessary loops when cloning a page's form controls to be
cached by a snapshot.

[a suggestion]: hotwired#666 (comment)
dhh pushed a commit that referenced this pull request Aug 8, 2022
Follow-up to #666

Following [a suggestion][] provided by review of #666, this commit
remove unnecessary loops when cloning a page's form controls to be
cached by a snapshot.

[a suggestion]: #666 (comment)
@tonywok
Copy link

tonywok commented Mar 9, 2023

👋 Thanks for this PR and all your hard work! Big fan of the library :)

I've been doing some research here.

I'm familiarizing myself with the cloning process and am curious why certain inputs receive special treatment. As you can see in the linked PR, I'm observing unexpected (to me at least) behavior in radio buttons (and check boxes) on restoration visits.

Curious if anything sticks out to you since you're intimately familiar with this bit of the code :) Also, curious if you find the linked behavior surprising -- or if this is something that must be handled in application code (given it's just vanilla form w/o any third-party libs, I think I expect it to work? 🤔)

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

Successfully merging this pull request may close these issues.

6 participants