Skip to content

Commit

Permalink
create handleCredentialInputs function
Browse files Browse the repository at this point in the history
bad code :/
  • Loading branch information
u-dani committed Feb 2, 2024
1 parent 62fa5ec commit 3adc731
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ function validateUsername({ ignoreInputEmpty }) {
const value = usernameInput.value.trim();
const isValid = false;
const inputElement = usernameInput;
let typeError;
// limpar campo caso só tenha espaços
if (/^[\s]+$/.test(usernameInput.value)) {
usernameInput.value = "";
}
// if (/^[\s]+$/.test(usernameInput!.value)) {
// usernameInput!.value = "";
// }
if (!ignoreInputEmpty && (value === null || value === void 0 ? void 0 : value.length) === 0) {
return { isValid, inputElement, typeError: "valueMissing" };
}
Expand All @@ -31,14 +30,14 @@ function validateUsername({ ignoreInputEmpty }) {
return { isValid, inputElement, typeError: "tooShort" };
}
usernameInput === null || usernameInput === void 0 ? void 0 : usernameInput.classList.remove('is-input-invalid');
return { isValid: false, typeError: undefined, inputElement: usernameInput };
return { isValid: true, inputElement: usernameInput, typeError: undefined };
}
function validatePassword({ ignoreInputEmpty }) {
const value = passwordInput === null || passwordInput === void 0 ? void 0 : passwordInput.value;
if (!ignoreInputEmpty && (value === null || value === void 0 ? void 0 : value.length) === 0) {
passwordError.textContent = 'Preencha o campo';
passwordInput === null || passwordInput === void 0 ? void 0 : passwordInput.classList.add('is-input-invalid');
return;
// return;
}
passwordInput === null || passwordInput === void 0 ? void 0 : passwordInput.classList.remove('is-input-invalid');
}
Expand All @@ -61,20 +60,26 @@ function submitForm(e) {
validateUsername({});
const firstInvalidElement = document.querySelector('.is-input-invalid');
firstInvalidElement === null || firstInvalidElement === void 0 ? void 0 : firstInvalidElement.focus();
if (!firstInvalidElement) {
const target = e.target;
target.submit();
}
// if (!firstInvalidElement) {
// const target = e.target as HTMLFormElement;
// target.submit();
// }
}
function handleCredentialInputs(e) {
console.log('input');
const target = e.target;
target.classList.remove('is-input-invalid');
const username = validateUsername({});
console.log(username);
username.isValid
? submitButton === null || submitButton === void 0 ? void 0 : submitButton.setAttribute('aria-disabled', 'false')
: submitButton === null || submitButton === void 0 ? void 0 : submitButton.setAttribute('aria-disabled', 'true');
}
btnTogglePassword === null || btnTogglePassword === void 0 ? void 0 : btnTogglePassword.addEventListener('click', togglePasswordVisibility);
loginForm === null || loginForm === void 0 ? void 0 : loginForm.addEventListener('submit', submitForm);
usernameInput === null || usernameInput === void 0 ? void 0 : usernameInput.addEventListener('focusout', () => validateUsername({ ignoreInputEmpty: true }));
passwordInput === null || passwordInput === void 0 ? void 0 : passwordInput.addEventListener('focusout', () => validatePassword({ ignoreInputEmpty: true }));
credentialInputs.forEach(input => input.addEventListener('input', (e) => {
const target = e.target;
target.classList.remove('is-input-invalid');
// button aria-disabled
}));
credentialInputs.forEach(input => input.addEventListener('input', handleCredentialInputs));
/*
.is-input-invalid
Expand Down

0 comments on commit 3adc731

Please sign in to comment.