Skip to content

fix: make browser autofill work - #266

Merged
arisuryaa merged 3 commits into
mainfrom
fix/html-autocomplete-field
Aug 22, 2026
Merged

fix: make browser autofill work#266
arisuryaa merged 3 commits into
mainfrom
fix/html-autocomplete-field

Conversation

@arisuryaa

@arisuryaa arisuryaa commented Aug 19, 2026

Copy link
Copy Markdown
Contributor
  • Determine the autocomplete token and input type based on field.type.name

  • Replace the hidden input for the country and province fields with a "visually" hidden element Add handleNativeSelectChange to keep the visible dropdown in sync when the browser fills the select setAllFieldsTouched now supports the select elements for country and province.

* Determine the autocomplete token and input type based on field.type.name

* Replace the hidden input for the country and province fields with a "visually" hidden <select> element

* Add handleNativeSelectChange to keep the visible dropdown in sync when the
  browser fills the select

* setAllFieldsTouched now supports the *select* elements for country and province.
@arisuryaa
arisuryaa requested a review from a team as a code owner August 19, 2026 16:59
@arisuryaa
arisuryaa requested review from galihputera, jenpresly and simon-paris and removed request for galihputera and jenpresly August 19, 2026 16:59
<div>
<input type="hidden" name={name} defaultValue="" ref={hiddenFieldRef} />
{/* a `<select>`, not `type="hidden"` browsers only autofill what they render */}
<select

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does <input autoComplete="country"/> work?

Rendering 200+ option elements isn't ideal

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

i tried it but doesn't work. i searching why and i think it's because the input already gets a value from the card's country before input showed up, and browser won't override an input that already has a value. that's why i use select + option, and select + option doesn't have that problem though. Also yeah, rendering 200+ options felt wrong to me too at first, so I wrapped it in a memo. Should be fine to leave as is?

Comment on lines +148 to +154
position: "absolute",
width: "1px",
height: "1px",
overflow: "hidden",
clipPath: "inset(50%)",
whiteSpace: "nowrap",
pointerEvents: "none",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

clipPath is hiding everything here, but should probably also remove the margin, padding, and border

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

noted thankyou

Comment thread sdk/src/components/field-province.tsx Outdated
Comment on lines +39 to +48
// the `<select>` in dropdown mode, or the visible input in free text mode
const hiddenFieldRef = useRef<HTMLInputElement | HTMLSelectElement | null>(
null,
);
const setFieldRef = useCallback(
(element: HTMLInputElement | HTMLSelectElement | null) => {
hiddenFieldRef.current = element;
},
[],
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why change to a function ref here? Also hiddenFieldRef is no longer "hidden"

@arisuryaa arisuryaa Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

so province can render either a select or an input depending on the country, and the ref needs to point to whichever one shows up. and i can't pass one ref object to both since typescript wants it typed to one specific element, so wrapping it in a function was the easiest way that i think?, i can add comment to that if you want. also gonna renaming hiddenFieldRef, thankyou

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Right, I see

[options],
);

const handleNativeSelectChange = useCallback(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you add tests for all the handleNativeSelectChange functions?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

okay simon 🫡

* Add margin/padding/border to the visually-hidden select styling

* Rename hiddenFieldRef to valueFieldRef and explain why a callback ref is needed

* Add tests for handleNativeSelectChange covering both country and province

* Fix a bug surfaced by those tests: province could silently lose its value when switching between dropdown and free-text mode
@arisuryaa
arisuryaa requested a review from simon-paris August 21, 2026 02:45

@simon-paris simon-paris left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good, but can you also assign autocomplete values to the cards address form in the test-data? The server config has them but the mock doesn't

@simon-paris

Copy link
Copy Markdown
Contributor

Oh and could you also make the phone areacode autofill properly?

@arisuryaa

Copy link
Copy Markdown
Contributor Author

Oh and could you also make the phone areacode autofill properly?

sure, gonna fix that

@arisuryaa
arisuryaa requested a review from simon-paris August 21, 2026 08:27
@arisuryaa
arisuryaa merged commit 2cd4b56 into main Aug 22, 2026
14 of 15 checks passed
@arisuryaa
arisuryaa deleted the fix/html-autocomplete-field branch August 22, 2026 02:31
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.

2 participants