import React, { type JSX } from "react"; import InputString from "./InputString"; import SmallError from "./SmallError"; import { Trans, type WithTranslation, withTranslation } from "react-i18next"; import { type TFunction } from "i18next"; import { ErrorType, validate } from "../libs/urlopen"; function errorTypeToJsx(errorType: ErrorType | undefined, t: TFunction): JSX.Element | undefined { switch (errorType) { case ErrorType.EmptyHttpsProtocol: return ( Must provide protocol: https:// ); case ErrorType.EmptyHttpOrHttpsProtocol: return ( Must provide protocol: http:// or https:// ); case ErrorType.CorsError: return ( CORS policy won't allow fetching resources served over http from https, use a https:// domain ); default: return undefined; } } export type FieldUrlProps = { "data-wd-key"?: string value: string style?: object default?: string onChange(...args: unknown[]): unknown onInput?(...args: unknown[]): unknown multi?: boolean required?: boolean "aria-label"?: string type?: string className?: string }; type InputUrlInternalProps = FieldUrlProps & WithTranslation; type InputUrlState = { error?: ErrorType }; class InputUrlInternal extends React.Component { static defaultProps = { onInput: () => {}, }; constructor (props: InputUrlInternalProps) { super(props); this.state = { error: validate(props.value), }; } onInput = (url: string) => { this.setState({ error: validate(url), }); if (this.props.onInput) this.props.onInput(url); }; onChange = (url: string) => { this.setState({ error: validate(url), }); this.props.onChange(url); }; render () { return (
{errorTypeToJsx(this.state.error, this.props.t)}
); } } const InputUrl = withTranslation()(InputUrlInternal); export default InputUrl;