mirror of
https://github.com/a-nyx/maputnik-with-pmtiles.git
synced 2024-12-27 08:05:28 +01:00
Fix URL entry to use form submission and improved errors if protocol isn't present.
This commit is contained in:
parent
c426dd7349
commit
b7e414a042
3 changed files with 49 additions and 19 deletions
|
@ -15,6 +15,7 @@ class Button extends React.Component {
|
||||||
|
|
||||||
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"]}
|
||||||
|
|
|
@ -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>http://</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>
|
||||||
|
|
|
@ -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">
|
||||||
|
|
Loading…
Reference in a new issue