{"version":3,"file":"validatableComponent.mjs","names":[],"sources":["../../../../package/services/formValidation/validatableComponent.tsx"],"sourcesContent":["import React, { PureComponent } from 'react';\nimport { FormValidator } from '@ahrowe/form-validation';\nimport Tooltip from 'components/common/tooltip';\n\nexport interface ValidatableComponentProps {\n  formValidator?: FormValidator | null;\n  onMouseEnter?: ((event: React.MouseEvent) => void) | null;\n  onMouseLeave?: ((event: React.MouseEvent) => void) | null;\n  onFocus?: ((event: React.FocusEvent) => void) | null;\n  onBlur?: ((event: React.FocusEvent) => void) | null;\n  onChange?: ((value: unknown) => void) | null;\n  errorMessage?: string;\n  isValid?: boolean;\n  isRequired?: boolean;\n  label?: string;\n}\n\nexport interface ValidatableComponentState {\n  isFocused: boolean;\n  isHovered: boolean;\n}\n\n/**\n * Base class that extends PureComponent with FormValidator integration.\n * Manages hover/focus state and renders error tooltips.\n */\nexport default class ValidatableComponent<\n  P extends ValidatableComponentProps = ValidatableComponentProps,\n  S extends ValidatableComponentState = ValidatableComponentState,\n> extends PureComponent<P, S> {\n  static defaultProps: Partial<ValidatableComponentProps> = {\n    formValidator: null,\n    onMouseEnter: null,\n    onMouseLeave: null,\n    onFocus: null,\n    onBlur: null,\n    onChange: null,\n    errorMessage: '',\n    isValid: false,\n    isRequired: false,\n    label: '',\n  };\n\n  constructor(props: P) {\n    super(props);\n    const { formValidator } = props;\n\n    if (formValidator) {\n      formValidator.label = props.label ?? '';\n      formValidator.validate();\n      formValidator.registerOnUpdateListener(() => {\n        this.forceUpdate();\n      });\n    }\n\n    this.state = { isFocused: false, isHovered: false } as S;\n\n    this.onMouseEnter = this.onMouseEnter.bind(this);\n    this.onMouseLeave = this.onMouseLeave.bind(this);\n    this.onFocus = this.onFocus.bind(this);\n    this.onBlur = this.onBlur.bind(this);\n    this.hasError = this.hasError.bind(this);\n    this.renderTooltip = this.renderTooltip.bind(this);\n    this.onChange = this.onChange.bind(this);\n  }\n\n  componentDidMount(): void {\n    setTimeout(() => this.setState({ isFocused: false, isHovered: false } as S), 1);\n  }\n\n  componentDidUpdate(prevProps: P): void {\n    const { formValidator } = this.props;\n    if (prevProps.formValidator && formValidator && prevProps.formValidator !== formValidator) {\n      formValidator.registerOnUpdateListener(() => {\n        this.forceUpdate();\n      });\n    }\n  }\n\n  onMouseEnter(event: React.MouseEvent): void {\n    const { onMouseEnter } = this.props;\n    this.setState({ isHovered: true } as unknown as S);\n    if (onMouseEnter) onMouseEnter(event);\n  }\n\n  onMouseLeave(event: React.MouseEvent): void {\n    const { onMouseLeave } = this.props;\n    this.setState({ isHovered: false } as unknown as S);\n    if (onMouseLeave) onMouseLeave(event);\n  }\n\n  onFocus(event: React.FocusEvent): void {\n    const { onFocus } = this.props;\n    this.setState({ isFocused: true } as unknown as S);\n    if (onFocus) onFocus(event);\n  }\n\n  onBlur(event: React.FocusEvent): void {\n    const { formValidator, onBlur } = this.props;\n    if (formValidator) formValidator.touched = true;\n    this.setState({ isFocused: false } as unknown as S);\n    if (onBlur) onBlur(event);\n  }\n\n  hasError(): boolean {\n    const { errorMessage, isValid, formValidator } = this.props;\n    return formValidator ? formValidator.shouldShowError : !isValid || !!errorMessage;\n  }\n\n  isRequired(): boolean {\n    const { isRequired, formValidator } = this.props;\n    return !!(\n      isRequired ||\n      (formValidator &&\n        formValidator.validators.find((v) => (v as { validatorId?: string }).validatorId === 'required'))\n    );\n  }\n\n  renderTooltip(): React.ReactNode {\n    const { errorMessage, formValidator } = this.props;\n    const { isHovered, isFocused } = this.state;\n    return (\n      <Tooltip\n        variant='error'\n        message={formValidator ? formValidator.getCurrentErrorMessage() : errorMessage || ''}\n        isVisible={this.hasError() && (isFocused || isHovered)}\n      />\n    );\n  }\n\n  onChange(newVal: unknown): void {\n    const { onChange, formValidator } = this.props;\n    if (formValidator) {\n      let extractedVal = newVal;\n      if (newVal && (newVal as React.ChangeEvent<HTMLInputElement>).target) {\n        extractedVal = (newVal as React.ChangeEvent<HTMLInputElement>).target.value;\n      }\n      formValidator.set(extractedVal);\n    }\n    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