import { Components } from '@formio/js'; import moment from 'moment'; import TimeHelper from '../util/TimeHelper'; import { CoreTimeComponent } from '../../types'; const Field = Components.components.field; const Time = Components.components.time; export default class GDSTimeComponent extends Time { public static MAX_LENGTH = 2; declare component: CoreTimeComponent; declare refs: { hour: HTMLInputElement; minute: HTMLInputElement; }; declare data: Record; private timeHelper: TimeHelper = new TimeHelper(); // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types public attach(element: any): Promise { this.loadRefs(element, { hour: 'single', minute: 'single', }); this.addEventListener(this.refs.hour, 'input', () => { this.timeHelper.checkAndValidateHour(this.refs.hour); this.setPristine(false); this.checkComponentValidity(this.data, true); this.updateValue(null, { modified: true }); }); this.addEventListener(this.refs.minute, 'input', () => { this.timeHelper.checkAndValidateMinutes(this.refs.minute); this.setPristine(false); this.checkComponentValidity(this.data, true); this.updateValue(null, { modified: true }); }); this.addEventListener(this.refs.hour, 'keypress', (evt: KeyboardEvent) => { this.timeHelper.preventNonNumericKeyPress(evt); }); this.addEventListener(this.refs.minute, 'keypress', (evt: KeyboardEvent) => { this.timeHelper.preventNonNumericKeyPress(evt); }); return super.attach(element); } // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types public setErrorClasses(elements: any, dirty: any, hasError: any): void { super.setErrorClasses([this.refs.hour, this.refs.minute, ...elements], dirty, hasError); } // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types public addInputError(message: any, dirty: any, element: any): void { super.addInputError(message, dirty, [this.refs.hour, this.refs.minute]); super.addInputError(message, dirty, element); } // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types public removeInputError(elements: any): void { super.removeInputError([this.refs.hour, this.refs.minute]); super.removeInputError(elements); } public getValue(): string { if (!this.refs.hour.value || !this.refs.minute.value) { return ''; } const value = `${this.refs.hour.value}:${this.refs.minute.value}`; return moment(value, this.component.format).format(this.component.dataFormat); } // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types public setValue(value: any, _flags: any): boolean { if (this.refs.hour && this.refs.minute && value) { const parts = (value as string).split(':'); this.refs.hour.value = parts[0]; this.refs.minute.value = parts[1]; return true; } return false; } public render(): string { return Field.prototype.render.call(this, this.renderTemplate('time', { hour: super.getValue().split(':')[0], minute: super.getValue().split(':')[1], })); } }