Merge pull request #682 from orangemug/fix/issue-681

Fix open modal URL entry to use form submission
This commit is contained in:
Orange Mug 2020-05-14 15:32:09 +01:00 committed by GitHub
commit e3e6647e03
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 19 deletions

View file

@ -11,10 +11,12 @@ class Button extends React.Component {
className: PropTypes.string, className: PropTypes.string,
children: PropTypes.node, children: PropTypes.node,
disabled: PropTypes.bool, disabled: PropTypes.bool,
type: PropTypes.string,
} }
render() { render() {
return <button return <button
type={this.props.type}
onClick={this.props.onClick} onClick={this.props.onClick}
disabled={this.props.disabled} disabled={this.props.disabled}
aria-label={this.props["aria-label"]} aria-label={this.props["aria-label"]}

View file

@ -1,10 +1,14 @@
import React from 'react' import React, {Fragment} from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import StringInput from './StringInput' import StringInput from './StringInput'
import SmallError from '../util/SmallError' import SmallError from '../util/SmallError'
function validate (url) { function validate (url) {
if (url === "") {
return;
}
let error; let error;
const getProtocol = (url) => { const getProtocol = (url) => {
try { try {
@ -16,7 +20,20 @@ function validate (url) {
} }
}; };
const protocol = getProtocol(url); const protocol = getProtocol(url);
if ( const isSsl = window.location.protocol === "https:";
if (!protocol) {
error = (
<SmallError>
Must provide protocol {
isSsl
? <code>https://</code>
: <><code>http://</code> or <code>https://</code></>
}
</SmallError>
);
}
else if (
protocol && protocol &&
protocol === "http:" && protocol === "http:" &&
window.location.protocol === "https:" window.location.protocol === "https:"
@ -61,12 +78,20 @@ class UrlInput extends React.Component {
this.props.onInput(url); this.props.onInput(url);
} }
onChange = (url) => {
this.setState({
error: validate(url)
});
this.props.onChange(url);
}
render () { render () {
return ( return (
<div> <div>
<StringInput <StringInput
{...this.props} {...this.props}
onInput={this.onInput} onInput={this.onInput}
onChange={this.onChange}
/> />
{this.state.error} {this.state.error}
</div> </div>

View file

@ -123,7 +123,8 @@ class OpenModal extends React.Component {
}) })
} }
onOpenUrl = (url) => { onSubmitUrl = (e) => {
e.preventDefault();
this.onStyleSelect(this.state.styleUrl); this.onStyleSelect(this.state.styleUrl);
} }
@ -209,22 +210,25 @@ class OpenModal extends React.Component {
<p> <p>
Load from a URL. Note that the URL must have <a href="https://enable-cors.org" target="_blank" rel="noopener noreferrer">CORS enabled</a>. Load from a URL. Note that the URL must have <a href="https://enable-cors.org" target="_blank" rel="noopener noreferrer">CORS enabled</a>.
</p> </p>
<UrlInput <form onSubmit={this.onSubmitUrl}>
data-wd-key="open-modal.url.input" <UrlInput
type="text" data-wd-key="open-modal.url.input"
className="maputnik-input" type="text"
default="Enter URL..." className="maputnik-input"
value={this.state.styleUrl} default="Enter URL..."
onInput={this.onChangeUrl} value={this.state.styleUrl}
/> onInput={this.onChangeUrl}
<div> onChange={this.onChangeUrl}
<Button />
data-wd-key="open-modal.url.button" <div>
className="maputnik-big-button" <Button
onClick={this.onOpenUrl} data-wd-key="open-modal.url.button"
disabled={this.state.styleUrl.length < 1} type="submit"
>Open URL</Button> className="maputnik-big-button"
</div> disabled={this.state.styleUrl.length < 1}
>Open URL</Button>
</div>
</form>
</section> </section>
<section className="maputnik-modal-section maputnik-modal-section--shrink"> <section className="maputnik-modal-section maputnik-modal-section--shrink">