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

39 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-04-12 17:59:50 +02:00
import { toggle } from '@utils';
2023-11-20 10:59:57 +01:00
import { ApplicationController } from './application_controller';
2024-04-12 17:59:50 +02:00
function onVisibleEnableInputs(element: HTMLInputElement) {
element.disabled = false;
element.required = true;
}
2023-11-20 10:59:57 +01:00
2024-04-12 17:59:50 +02:00
function onHiddenDisableInputs(element: HTMLInputElement) {
element.disabled = true;
element.required = false;
}
2023-11-20 10:59:57 +01:00
2024-04-12 17:59:50 +02:00
export class ForTiersController extends ApplicationController {
static targets = ['emailContainer', 'emailInput', 'notificationMethod'];
2023-11-20 10:59:57 +01:00
2024-04-12 17:59:50 +02:00
declare notificationMethodTargets: NodeListOf<HTMLInputElement>;
declare emailContainerTarget: HTMLElement;
declare emailInputTarget: HTMLInputElement;
2023-11-20 10:59:57 +01:00
toggleEmailInput() {
const isEmailSelected = this.isEmailSelected();
2024-04-12 17:59:50 +02:00
toggle(this.emailContainerTarget, isEmailSelected);
2023-11-20 10:59:57 +01:00
2024-04-12 17:59:50 +02:00
if (isEmailSelected) {
onVisibleEnableInputs(this.emailInputTarget);
} else {
onHiddenDisableInputs(this.emailInputTarget);
2023-11-20 10:59:57 +01:00
}
}
isEmailSelected() {
return Array.from(this.notificationMethodTargets).some(
(radio) => radio.value === 'email' && radio.checked
);
}
}