@@ -142,80 +142,75 @@ export type ValidationOptions<T> = (T extends string
142142 callback ?: ( result : ValidationResult ) => void ;
143143} ;
144144
145- export type ValidatedHtmlInputElement = HTMLInputElement & {
146- getValidationResult : ( ) => ValidationResult ;
147- setValue : ( val : string | null ) => void ;
148- triggerValidation : ( ) => void ;
149- } ;
150- /**
151- * adds an 'InputIndicator` to the given `inputElement` and updates its status depending on the given validation
152- * @param inputElement
153- * @param options
154- */
155- export function validateWithIndicator < T > (
156- inputElement : HTMLInputElement ,
157- options : ValidationOptions < T >
158- ) : ValidatedHtmlInputElement {
159- //use indicator
160- const indicator = new InputIndicator ( inputElement , {
161- success : {
162- icon : "fa-check" ,
163- level : 1 ,
164- } ,
165- failed : {
166- icon : "fa-times" ,
167- level : - 1 ,
168- } ,
169- warning : {
170- icon : "fa-exclamation-triangle" ,
171- level : 1 ,
172- } ,
173- checking : {
174- icon : "fa-circle-notch" ,
175- spinIcon : true ,
176- level : 0 ,
177- } ,
178- } ) ;
179-
180- let currentStatus : ValidationResult = {
145+ export class ValidatedHtmlInputElement < T = string > {
146+ public native : HTMLInputElement ;
147+ private indicator : InputIndicator ;
148+ private currentStatus : ValidationResult = {
181149 status : "checking" ,
182150 } ;
183- const callback = ( result : ValidationResult ) : void => {
184- currentStatus = result ;
185- if ( result . status === "failed" || result . status === "warning" ) {
186- indicator . show ( result . status , result . errorMessage ) ;
187- } else {
188- indicator . show ( result . status ) ;
189- }
190- options . callback ?.( result ) ;
191- } ;
192151
193- const handler = createInputEventHandler (
194- callback ,
195- options ,
196- "inputValueConvert" in options ? options . inputValueConvert : undefined
197- ) ;
152+ constructor ( inputElement : HTMLInputElement , options : ValidationOptions < T > ) {
153+ this . native = inputElement ;
198154
199- inputElement . addEventListener ( "input" , handler ) ;
155+ this . indicator = new InputIndicator ( inputElement , {
156+ success : {
157+ icon : "fa-check" ,
158+ level : 1 ,
159+ } ,
160+ failed : {
161+ icon : "fa-times" ,
162+ level : - 1 ,
163+ } ,
164+ warning : {
165+ icon : "fa-exclamation-triangle" ,
166+ level : 1 ,
167+ } ,
168+ checking : {
169+ icon : "fa-circle-notch" ,
170+ spinIcon : true ,
171+ level : 0 ,
172+ } ,
173+ } ) ;
200174
201- const result = inputElement as ValidatedHtmlInputElement ;
202- result . getValidationResult = ( ) => {
203- return currentStatus ;
204- } ;
205- result . setValue = ( val : string | null ) => {
206- inputElement . value = val ?? "" ;
175+ const callback = ( result : ValidationResult ) : void => {
176+ this . currentStatus = result ;
177+ if ( result . status === "failed" || result . status === "warning" ) {
178+ this . indicator . show ( result . status , result . errorMessage ) ;
179+ } else {
180+ this . indicator . show ( result . status ) ;
181+ }
182+ options . callback ?.( result ) ;
183+ } ;
184+
185+ const handler = createInputEventHandler (
186+ callback ,
187+ options ,
188+ "inputValueConvert" in options ? options . inputValueConvert : undefined
189+ ) ;
190+
191+ inputElement . addEventListener ( "input" , handler ) ;
192+ }
193+
194+ getValidationResult ( ) : ValidationResult {
195+ return this . currentStatus ;
196+ }
197+ setValue ( val : string | null ) : this {
198+ this . native . value = val ?? "" ;
207199 if ( val === null ) {
208- indicator . hide ( ) ;
209- currentStatus = { status : "checking" } ;
200+ this . indicator . hide ( ) ;
201+ this . currentStatus = { status : "checking" } ;
210202 } else {
211- inputElement . dispatchEvent ( new Event ( "input" ) ) ;
203+ this . native . dispatchEvent ( new Event ( "input" ) ) ;
212204 }
213- } ;
214- result . triggerValidation = ( ) => {
215- inputElement . dispatchEvent ( new Event ( "input" ) ) ;
216- } ;
217205
218- return result ;
206+ return this ;
207+ }
208+ getValue ( ) : string {
209+ return this . native . value ;
210+ }
211+ triggerValidation ( ) : void {
212+ this . native . dispatchEvent ( new Event ( "input" ) ) ;
213+ }
219214}
220215
221216export type ConfigInputOptions < K extends ConfigKey , T = ConfigType [ K ] > = {
@@ -260,7 +255,7 @@ export function handleConfigInput<T extends ConfigKey>({
260255 if ( validation !== undefined ) {
261256 const schema = ConfigSchema . shape [ configName ] as ZodType ;
262257
263- validateWithIndicator ( input , {
258+ new ValidatedHtmlInputElement ( input , {
264259 schema : validation . schema ? schema : undefined ,
265260 //@ts -expect-error this is fine
266261 isValid : validation . isValid ,
0 commit comments