Skip to content

Commit

Permalink
More migration of components
Browse files Browse the repository at this point in the history
  • Loading branch information
HarelM committed Dec 21, 2023
1 parent fa182e6 commit 12def85
Show file tree
Hide file tree
Showing 15 changed files with 270 additions and 244 deletions.
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"@types/codemirror": "^5.60.15",
"@types/color": "^3.0.6",
"@types/cors": "^2.8.17",
"@types/file-saver": "^2.0.7",
"@types/lodash.capitalize": "^4.2.9",
"@types/lodash.isequal": "^4.5.8",
"@types/lodash.throttle": "^4.1.9",
Expand Down
20 changes: 0 additions & 20 deletions src/components/FieldCheckbox.jsx

This file was deleted.

18 changes: 18 additions & 0 deletions src/components/FieldCheckbox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react'
import Block from './Block'
import InputCheckbox, {InputCheckboxProps} from './InputCheckbox'


type FieldCheckboxProps = InputCheckboxProps & {
label?: string;
};


export default class FieldCheckbox extends React.Component<FieldCheckboxProps> {
render() {
return <Block label={this.props.label}>
<InputCheckbox {...this.props} />
</Block>
}
}

21 changes: 0 additions & 21 deletions src/components/FieldDynamicArray.jsx

This file was deleted.

16 changes: 16 additions & 0 deletions src/components/FieldDynamicArray.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react'
import InputDynamicArray, {FieldDynamicArrayProps as InputDynamicArrayProps} from './InputDynamicArray'
import Fieldset from './Fieldset'

type FieldDynamicArrayProps = InputDynamicArrayProps & {
name?: string
};

export default class FieldDynamicArray extends React.Component<FieldDynamicArrayProps> {
render() {
return <Fieldset label={this.props.label}>
<InputDynamicArray {...this.props} />
</Fieldset>
}
}

19 changes: 9 additions & 10 deletions src/components/FieldSource.jsx → src/components/FieldSource.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import React from 'react'
import PropTypes from 'prop-types'

import {latest} from '@maplibre/maplibre-gl-style-spec'
import Block from './Block'
import InputAutocomplete from './InputAutocomplete'

export default class FieldSource extends React.Component {
static propTypes = {
value: PropTypes.string,
wdKey: PropTypes.string,
onChange: PropTypes.func,
sourceIds: PropTypes.array,
error: PropTypes.object,
}
type FieldSourceProps = {
value?: string
wdKey?: string
onChange?(...args: unknown[]): unknown
sourceIds?: unknown[]
error?: unknown[]
};

export default class FieldSource extends React.Component<FieldSourceProps> {
static defaultProps = {
onChange: () => {},
sourceIds: [],
Expand All @@ -29,7 +28,7 @@ export default class FieldSource extends React.Component {
<InputAutocomplete
value={this.props.value}
onChange={this.props.onChange}
options={this.props.sourceIds.map(src => [src, src])}
options={this.props.sourceIds?.map(src => [src, src])}
/>
</Block>
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import React from 'react'
import PropTypes from 'prop-types'

import {latest} from '@maplibre/maplibre-gl-style-spec'
import Block from './Block'
import InputAutocomplete from './InputAutocomplete'

export default class FieldSourceLayer extends React.Component {
static propTypes = {
value: PropTypes.string,
onChange: PropTypes.func,
sourceLayerIds: PropTypes.array,
isFixed: PropTypes.bool,
}
type FieldSourceLayerProps = {
value?: string
onChange?(...args: unknown[]): unknown
sourceLayerIds?: unknown[]
isFixed?: boolean
};

export default class FieldSourceLayer extends React.Component<FieldSourceLayerProps> {
static defaultProps = {
onChange: () => {},
sourceLayerIds: [],
Expand All @@ -27,7 +26,7 @@ export default class FieldSourceLayer extends React.Component {
keepMenuWithinWindowBounds={!!this.props.isFixed}
value={this.props.value}
onChange={this.props.onChange}
options={this.props.sourceLayerIds.map(l => [l, l])}
options={this.props.sourceLayerIds?.map(l => [l, l])}
/>
</Block>
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/InputDynamicArray.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import InputUrl from './InputUrl'

export type FieldDynamicArrayProps = {
value?: (string | number)[]
type?: 'url' | 'number' | 'enum'
type?: 'url' | 'number' | 'enum' | 'string'
default?: (string | number)[]
onChange?(...args: unknown[]): unknown
style?: object
Expand Down
8 changes: 4 additions & 4 deletions src/components/InputJson.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type InputJsonProps = {
onChange?(...args: unknown[]): unknown
lineNumbers?: boolean
lineWrapping?: boolean
getValue(data: any): string
getValue?(data: any): string
gutters?: string[]
className?: string
onFocus?(...args: unknown[]): unknown
Expand Down Expand Up @@ -58,13 +58,13 @@ export default class InputJson extends React.Component<InputJsonProps, InputJson
this.state = {
isEditing: false,
showMessage: false,
prevValue: this.props.getValue(this.props.layer),
prevValue: this.props.getValue!(this.props.layer),
};
}

componentDidMount () {
this._doc = CodeMirror(this._el!, {
value: this.props.getValue(this.props.layer),
value: this.props.getValue!(this.props.layer),
mode: this.props.mode || {
name: "mgl",
},
Expand Down Expand Up @@ -117,7 +117,7 @@ export default class InputJson extends React.Component<InputJsonProps, InputJson
if (!this.state.isEditing && prevProps.layer !== this.props.layer) {
this._cancelNextChange = true;
this._doc!.setValue(
this.props.getValue(this.props.layer),
this.props.getValue!(this.props.layer),
)
}
}
Expand Down
69 changes: 36 additions & 33 deletions src/components/ModalAdd.jsx → src/components/ModalAdd.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
import React from 'react'
import PropTypes from 'prop-types'

import {latest} from '@maplibre/maplibre-gl-style-spec'
import InputButton from './InputButton'
import Modal from './Modal'

import FieldType from './FieldType'
import FieldId from './FieldId'
import FieldSource from './FieldSource'
import FieldSourceLayer from './FieldSourceLayer'

export default class ModalAdd extends React.Component {
static propTypes = {
layers: PropTypes.array.isRequired,
onLayersChange: PropTypes.func.isRequired,
isOpen: PropTypes.bool.isRequired,
onOpenToggle: PropTypes.func.isRequired,

// A dict of source id's and the available source layers
sources: PropTypes.object.isRequired,
}

type ModalAddProps = {
layers: unknown[]
onLayersChange(...args: unknown[]): unknown
isOpen: boolean
onOpenToggle(...args: unknown[]): unknown
// A dict of source id's and the available source layers
sources: any
};

type ModalAddState = {
type: string
id: string
source?: string
'source-layer'?: string
};

export default class ModalAdd extends React.Component<ModalAddProps, ModalAddState> {
addLayer = () => {
const changedLayers = this.props.layers.slice(0)
const layer = {
const layer: ModalAddState = {
id: this.state.id,
type: this.state.type,
}
Expand All @@ -41,20 +44,21 @@ export default class ModalAdd extends React.Component {
this.props.onOpenToggle(false)
}

constructor(props) {
constructor(props: ModalAddProps) {
super(props)
this.state = {
const state: ModalAddState = {
type: 'fill',
id: '',
}

if(props.sources.length > 0) {
this.state.source = Object.keys(this.props.sources)[0]
this.state['source-layer'] = this.props.sources[this.state.source][0]
state.source = Object.keys(this.props.sources)[0];
state['source-layer'] = this.props.sources[state.source as keyof ModalAddProps["sources"]][0]
}
this.state = state;
}

componentDidUpdate(prevProps, prevState) {
componentDidUpdate(_prevProps: ModalAddProps, prevState: ModalAddState) {
// Check if source is valid for new type
const oldType = prevState.type;
const newType = this.state.type;
Expand All @@ -67,9 +71,9 @@ export default class ModalAdd extends React.Component {
oldType !== newType
&& prevState.source !== ""
// Was a valid source previously
&& availableSourcesOld.indexOf(prevState.source) > -1
&& availableSourcesOld.indexOf(prevState.source!) > -1
// And is not a valid source now
&& availableSourcesNew.indexOf(this.state.source) < 0
&& availableSourcesNew.indexOf(this.state.source!) < 0
) {
// Clear the source
this.setState({
Expand All @@ -78,12 +82,12 @@ export default class ModalAdd extends React.Component {
}
}

getLayersForSource(source) {
getLayersForSource(source: string) {
const sourceObj = this.props.sources[source] || {};
return sourceObj.layers || [];
}

getSources(type) {
getSources(type: string) {
const sources = [];

const types = {
Expand All @@ -108,8 +112,9 @@ export default class ModalAdd extends React.Component {
]
}

for(let [key, val] of Object.entries(this.props.sources)) {
if(types[val.type] && types[val.type].indexOf(type) > -1) {
for(let [key, val] of Object.entries(this.props.sources) as any) {
const valType = val.type as keyof typeof types;
if(types[valType] && types[valType].indexOf(type) > -1) {
sources.push(key);
}
}
Expand All @@ -120,7 +125,7 @@ export default class ModalAdd extends React.Component {

render() {
const sources = this.getSources(this.state.type);
const layers = this.getLayersForSource(this.state.source);
const layers = this.getLayersForSource(this.state.source!);

return <Modal
isOpen={this.props.isOpen}
Expand All @@ -131,33 +136,31 @@ export default class ModalAdd extends React.Component {
>
<div className="maputnik-add-layer">
<FieldId
label="ID"
fieldSpec={latest.layer.id}
value={this.state.id}
wdKey="add-layer.layer-id"
onChange={v => {
onChange={(v: string) => {
this.setState({ id: v })
}}
/>
<FieldType
value={this.state.type}
wdKey="add-layer.layer-type"
onChange={v => this.setState({ type: v })}
onChange={(v: string) => this.setState({ type: v })}
/>
{this.state.type !== 'background' &&
<FieldSource
sourceIds={sources}
wdKey="add-layer.layer-source-block"
value={this.state.source}
onChange={v => this.setState({ source: v })}
onChange={(v: string) => this.setState({ source: v })}
/>
}
{['background', 'raster', 'hillshade', 'heatmap'].indexOf(this.state.type) < 0 &&
<FieldSourceLayer
isFixed={true}
sourceLayerIds={layers}
value={this.state['source-layer']}
onChange={v => this.setState({ 'source-layer': v })}
onChange={(v: string) => this.setState({ 'source-layer': v })}
/>
}
<InputButton
Expand Down
Loading

0 comments on commit 12def85

Please sign in to comment.