demarches-normaliennes/app/javascript/controllers/email_france_connect_controller.ts

34 lines
1 KiB
TypeScript
Raw Normal View History

import { ApplicationController } from './application_controller';
export class EmailFranceConnectController extends ApplicationController {
static targets = ['useFranceConnectEmail', 'emailField'];
2024-08-12 11:48:14 +02:00
emailFieldTarget!: HTMLElement;
useFranceConnectEmailTargets!: HTMLInputElement[];
connect() {
this.triggerEmailField();
}
triggerEmailField() {
const checkedTarget = this.useFranceConnectEmailTargets.find(
(target) => target.checked
);
const inputElement = this.emailFieldTarget.querySelector(
2024-08-12 11:48:14 +02:00
'input[type="email"]'
) as HTMLInputElement;
if (checkedTarget && checkedTarget.value === 'false') {
this.emailFieldTarget.classList.remove('hidden');
this.emailFieldTarget.setAttribute('aria-hidden', 'false');
inputElement.setAttribute('required', '');
} else {
this.emailFieldTarget.classList.add('hidden');
this.emailFieldTarget.setAttribute('aria-hidden', 'true');
inputElement.removeAttribute('required');
inputElement.value = '';
}
}
}