2023-01-12 18:34:45 +01:00
|
|
|
import { ApplicationController } from './application_controller';
|
|
|
|
|
|
|
|
export class FormatController extends ApplicationController {
|
|
|
|
connect() {
|
|
|
|
const format = this.element.getAttribute('data-format');
|
|
|
|
switch (format) {
|
|
|
|
case 'list':
|
|
|
|
this.on('change', (event) => {
|
|
|
|
const target = event.target as HTMLInputElement;
|
|
|
|
target.value = this.formatList(target.value);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'iban':
|
|
|
|
this.on('input', (event) => {
|
|
|
|
const target = event.target as HTMLInputElement;
|
|
|
|
target.value = this.formatIBAN(target.value);
|
|
|
|
});
|
2023-05-30 14:42:36 +02:00
|
|
|
break;
|
|
|
|
case 'integer':
|
|
|
|
this.on('input', (event) => {
|
|
|
|
const target = event.target as HTMLInputElement;
|
|
|
|
target.value = this.formatInteger(target.value);
|
|
|
|
});
|
2023-01-12 18:34:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private formatList(value: string) {
|
|
|
|
return value.replace(/;/g, ',');
|
|
|
|
}
|
|
|
|
|
|
|
|
private formatIBAN(value: string) {
|
|
|
|
return value
|
2023-07-17 16:42:37 +02:00
|
|
|
.replace(/[^\dA-Z]/gi, '')
|
2023-01-12 18:34:45 +01:00
|
|
|
.replace(/(.{4})/g, '$1 ')
|
2023-07-17 16:42:37 +02:00
|
|
|
.trim()
|
|
|
|
.toUpperCase();
|
2023-01-12 18:34:45 +01:00
|
|
|
}
|
2023-05-30 14:42:36 +02:00
|
|
|
|
|
|
|
private formatInteger(value: string) {
|
|
|
|
return value.replace(/[^\d]/g, '');
|
|
|
|
}
|
2023-01-12 18:34:45 +01:00
|
|
|
}
|