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

chrome auto fill fix #640

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions packages/textfield/src/Textfield.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
'smui-text-field--standard': variant === 'standard' && !textarea,
'mdc-text-field--no-label': noLabel || (label == null && !$$slots.label),
'mdc-text-field--label-floating':
focused || (value != null && value !== ''),
focused || chromeAutofilled || (value != null && value !== ''),
'mdc-text-field--with-leading-icon': isUninitializedValue(withLeadingIcon)
? $$slots.leadingIcon
: withLeadingIcon,
Expand Down Expand Up @@ -64,7 +64,7 @@
{#if !noLabel && (label != null || $$slots.label)}
<FloatingLabel
bind:this={floatingLabel}
floatAbove={focused ||
floatAbove={focused || chromeAutofilled ||
(value != null &&
value !== '' &&
(typeof value !== 'number' || !isNaN(value)))}
Expand All @@ -84,7 +84,7 @@
{#if !noLabel && (label != null || $$slots.label)}
<FloatingLabel
bind:this={floatingLabel}
floatAbove={focused ||
floatAbove={focused || chromeAutofilled ||
(value != null &&
value !== '' &&
(typeof value !== 'number' || !isNaN(value)))}
Expand Down Expand Up @@ -410,6 +410,10 @@
let characterCounter: MDCTextFieldCharacterCounterFoundation | undefined =
undefined;

let chromeAutofilled: boolean = false;
let chromeAutofilledFirstCheckInterval: NodeJS.Timer;
let chromeAutofilledSecondCheckInterval: NodeJS.Timer;

$: inputElement = input && input.getElement();

$: if (instance && instance.isValid() !== !invalid) {
Expand Down Expand Up @@ -556,6 +560,33 @@
}
);


const chromeAutofilledSecondCheck = () => {
chromeAutofilledSecondCheckInterval = setInterval(() => {
if (inputElement && !inputElement.matches(':-internal-autofill-selected')) {
chromeAutofilled = false;
clearInterval(chromeAutofilledSecondCheckInterval);
}
}, 100);
};

const chromeAutofilledFirstCheck = () => {
chromeAutofilledFirstCheckInterval = setInterval(() => {
if (inputElement && inputElement.matches(':-internal-autofill-selected')) {
chromeAutofilled = true;
instance.notchOutline(true);
clearInterval(chromeAutofilledFirstCheckInterval);
chromeAutofilledSecondCheck();
}
}, 100);

setTimeout(() => {
clearInterval(chromeAutofilledFirstCheckInterval);
}, 1000);
};

chromeAutofilledFirstCheck();

if (valued) {
if (input == null) {
throw new Error(
Expand All @@ -578,6 +609,8 @@

return () => {
instance.destroy();
clearInterval(chromeAutofilledFirstCheckInterval);
clearInterval(chromeAutofilledSecondCheckInterval);
};
});

Expand Down