diff --git a/src/components/inputs/UrlInput.jsx b/src/components/inputs/UrlInput.jsx index 04bb531..971d241 100644 --- a/src/components/inputs/UrlInput.jsx +++ b/src/components/inputs/UrlInput.jsx @@ -3,6 +3,34 @@ import PropTypes from 'prop-types' import StringInput from './StringInput' import SmallError from '../util/SmallError' + +function validate (url) { + let error; + const getProtocol = (url) => { + try { + const urlObj = new URL(url); + return urlObj.protocol; + } + catch (err) { + return undefined; + } + }; + const protocol = getProtocol(url); + if ( + protocol && + protocol === "http:" && + window.location.protocol === "https:" + ) { + error = ( + + CORs policy won't allow fetching resources served over http from https, use a https:// domain + + ); + } + + return error; +} + class UrlInput extends React.Component { static propTypes = { "data-wd-key": PropTypes.string, @@ -14,35 +42,17 @@ class UrlInput extends React.Component { required: PropTypes.bool, } - state = { - error: null, + constructor (props) { + super(props); + this.state = { + error: validate(props.value) + }; } onInput = (url) => { - let error; - const getProtocol = (url) => { - try { - const urlObj = new URL(url); - return urlObj.protocol; - } - catch (err) { - return undefined; - } - }; - const protocol = getProtocol(url); - if ( - protocol && - protocol === "http:" && - window.location.protocol === "https:" - ) { - error = ( - - CORs policy won't allow fetching resources served over http from https, use a https:// domain - - ); - } - - this.setState({error}); + this.setState({ + error: validate(url) + }); } render () {