Skip to content
Merged
Show file tree
Hide file tree
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
77 changes: 35 additions & 42 deletions app/javascript/packs/ssn-field.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,42 @@
import Cleave from 'cleave.js';

function formatSSNFieldAndLimitLength() {
const inputs = document.querySelectorAll<HTMLInputElement>('input.ssn-toggle[type="password"]');

if (inputs) {
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.

For awareness, I snuck an extra change in to remove this redundant condition. querySelectorAll will always return an array, and an array is always truthy.

See: https://dorey.github.io/JavaScript-Equality-Table/#if-statement

inputs.forEach((input) => {
const toggle = document.querySelector<HTMLInputElement>(`[aria-controls="${input.id}"]`)!;

let cleave: Cleave | undefined;

function sync() {
const { value } = input;
cleave?.destroy();
if (toggle.checked) {
cleave = new Cleave(input, {
numericOnly: true,
blocks: [3, 2, 4],
delimiter: '-',
});
} else {
const nextValue = value.replace(/-/g, '');
if (nextValue !== value) {
input.value = nextValue;
}
}
const didFormat = input.value !== value;
if (didFormat) {
input.checkValidity();
}
const inputs = document.querySelectorAll<HTMLInputElement>('input.ssn-toggle[type="password"]');
inputs.forEach((input) => {
const toggle = document.querySelector<HTMLInputElement>(`[aria-controls="${input.id}"]`)!;

let cleave: Cleave | undefined;

function sync() {
const { value } = input;
cleave?.destroy();
if (toggle.checked) {
cleave = new Cleave(input, {
numericOnly: true,
blocks: [3, 2, 4],
delimiter: '-',
});
} else {
const nextValue = value.replace(/-/g, '');
if (nextValue !== value) {
input.value = nextValue;
}
}
const didFormat = input.value !== value;
if (didFormat) {
input.checkValidity();
}
}

sync();
toggle.addEventListener('change', sync);

function limitLength(this: HTMLInputElement) {
const maxLength = 9 + (this.value.match(/-/g) || []).length;
if (this.value.length > maxLength) {
this.value = this.value.slice(0, maxLength);
this.checkValidity();
}
}
sync();
toggle.addEventListener('change', sync);

input.addEventListener('input', limitLength.bind(input));
});
function limitLength(this: HTMLInputElement) {
const maxLength = 9 + (this.value.match(/-/g) || []).length;
if (this.value.length > maxLength) {
this.value = this.value.slice(0, maxLength);
this.checkValidity();
}
}
}

document.addEventListener('DOMContentLoaded', formatSSNFieldAndLimitLength);
input.addEventListener('input', limitLength.bind(input));
});
8 changes: 3 additions & 5 deletions app/javascript/packs/state-guidance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ function onIdentityDocJurisdictionSelection() {

document.getElementById('idv_form_state')?.addEventListener('change', onStateSelectionChange);

document.addEventListener('DOMContentLoaded', () => {
onStateSelectionChange();
onIdentityDocStateSelection();
onIdentityDocJurisdictionSelection();
});
onStateSelectionChange();
onIdentityDocStateSelection();
onIdentityDocJurisdictionSelection();