Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

[Chore]: Technical: Translate components to typescript (#1812)

Mikhail Gorbacheva5a347bacd76
diff --git a/src/components/common/field-selector.tsx b/src/components/common/field-selector.tsx
index 8a5d65ca1..c5459abf8 100644
--- a/src/components/common/field-selector.tsx
+++ b/src/components/common/field-selector.tsx
@@ -64,8 +64,8 @@ type FieldType =
| string
| string[]
| {
- name?: string;
- format?: string;
+ name: string;
+ format: string | null;
}[]
| {
format?: string;
diff --git a/src/components/side-panel/interaction-panel/brush-config.js b/src/components/side-panel/interaction-panel/brush-config.tsx
similarity index 88%
rename from src/components/side-panel/interaction-panel/brush-config.js
rename to src/components/side-panel/interaction-panel/brush-config.tsx
index c82088b55..163c446c8 100644
--- a/src/components/side-panel/interaction-panel/brush-config.js
+++ b/src/components/side-panel/interaction-panel/brush-config.tsx
@@ -27,8 +27,15 @@ import {FormattedMessage} from 'localization';
BrushConfigFactory.deps = [RangeSliderFactory];
-function BrushConfigFactory(RangeSlider) {
- const BrushConfig = ({config, onChange}) => (
+type BrushConfigProps = {
+ config: {
+ size: number;
+ };
+ onChange: (config: {size: number}) => void;
+};
+
+function BrushConfigFactory(RangeSlider: ReturnType<typeof RangeSliderFactory>) {
+ const BrushConfig = ({config, onChange}: BrushConfigProps) => (
<SidePanelSection>
<PanelLabel>
<FormattedMessage id={'misc.brushRadius'} />
diff --git a/src/components/side-panel/interaction-panel/interaction-panel.js b/src/components/side-panel/interaction-panel/interaction-panel.tsx
similarity index 85%
rename from src/components/side-panel/interaction-panel/interaction-panel.js
rename to src/components/side-panel/interaction-panel/interaction-panel.tsx
index f6c4a9ca9..96e6ec071 100644
--- a/src/components/side-panel/interaction-panel/interaction-panel.js
+++ b/src/components/side-panel/interaction-panel/interaction-panel.tsx
@@ -18,8 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
-import React, {Component} from 'react';
-import PropTypes from 'prop-types';
+import React, {Component, ComponentType, ReactElement} from 'react';
import styled from 'styled-components';
import Switch from 'components/common/switch';
@@ -34,22 +33,33 @@ import {
} from 'components/common/styled-components';
import {FormattedMessage} from 'localization';
+interface InteractionPanelProps {
+ datasets: any;
+ config: {
+ id: string;
+ enabled: boolean;
+ label: string;
+ iconComponent: any;
+ config: any;
+ };
+ onConfigChange: any;
+}
+
const StyledInteractionPanel = styled.div`
padding-bottom: 6px;
`;
InteractionPanelFactory.deps = [TooltipConfigFactory, BrushConfigFactory];
-function InteractionPanelFactory(TooltipConfig, BrushConfig) {
- return class InteractionPanel extends Component {
- static propTypes = {
- datasets: PropTypes.object.isRequired,
- config: PropTypes.object.isRequired,
- onConfigChange: PropTypes.func.isRequired
+function InteractionPanelFactory(
+ TooltipConfig: ReturnType<typeof TooltipConfigFactory>,
+ BrushConfig: ReturnType<typeof BrushConfigFactory>
+): ComponentType<InteractionPanelProps> {
+ return class InteractionPanel extends Component<InteractionPanelProps> {
+ state = {
+ isConfigActive: false
};
- state = {isConfigActive: false};
-
_updateConfig = newProp => {
this.props.onConfigChange({
...this.props.config,
@@ -64,7 +74,7 @@ function InteractionPanelFactory(TooltipConfig, BrushConfig) {
render() {
const {config, datasets} = this.props;
const onChange = newConfig => this._updateConfig({config: newConfig});
- let template = null;
+ let template: ReactElement | null = null;
switch (config.id) {
case 'tooltip':
@@ -72,7 +82,6 @@ function InteractionPanelFactory(TooltipConfig, BrushConfig) {
<TooltipConfig datasets={datasets} config={config.config} onChange={onChange} />
);
break;
-
case 'brush':
template = <BrushConfig config={config.config} onChange={onChange} />;
break;
diff --git a/src/components/side-panel/interaction-panel/tooltip-config.js b/src/components/side-panel/interaction-panel/tooltip-config.tsx
similarity index 77%
rename from src/components/side-panel/interaction-panel/tooltip-config.js
rename to src/components/side-panel/interaction-panel/tooltip-config.tsx
index 4e15dfb1c..dcc13b9c7 100644
--- a/src/components/side-panel/interaction-panel/tooltip-config.js
+++ b/src/components/side-panel/interaction-panel/tooltip-config.tsx
@@ -20,7 +20,7 @@
import React from 'react';
import styled from 'styled-components';
-import {injectIntl} from 'react-intl';
+import {injectIntl, IntlShape} from 'react-intl';
import {FormattedMessage} from 'localization';
import {
@@ -68,9 +68,66 @@ const CompareSwitchWrapper = styled.div`
margin-bottom: 8px;
`;
+type TooltipConfigProps = {
+ config: {
+ fieldsToShow: {
+ [key: string]: {name: string; format: string | null}[];
+ };
+ compareMode: boolean;
+ compareType: string[];
+ };
+ onChange: (config: {
+ fieldsToShow: {
+ [key: string]: {name: string; format: string | null}[];
+ };
+ compareMode: boolean;
+ compareType: string[];
+ }) => void;
+ datasets: {
+ [key: string]: {
+ id: string;
+ fields: {
+ format?: string;
+ id?: string;
+ name?: string;
+ fieldIdx?: number;
+ type?: number;
+ }[];
+ };
+ };
+ intl: IntlShape;
+};
+
+type DatasetTooltipConfigProps = {
+ config: {
+ fieldsToShow: {
+ [key: string]: {name: string; format: string | null}[];
+ };
+ compareMode: boolean;
+ compareType: string[];
+ };
+ onChange: (config: {
+ fieldsToShow: {
+ [key: string]: {name: string; format: string | null}[];
+ };
+ compareMode: boolean;
+ compareType: string[];
+ }) => void;
+ dataset: {
+ id: string;
+ fields: {
+ format?: string;
+ id?: string;
+ name?: string;
+ fieldIdx?: number;
+ type?: number;
+ }[];
+ };
+};
+
TooltipConfigFactory.deps = [DatasetTagFactory, FieldSelectorFactory];
-function TooltipConfigFactory(DatasetTag, FieldSelector) {
- const DatasetTooltipConfig = ({config, onChange, dataset}) => {
+function TooltipConfigFactory(DatasetTag, FieldSelector: ReturnType<typeof FieldSelectorFactory>) {
+ const DatasetTooltipConfig = ({config, onChange, dataset}: DatasetTooltipConfigProps) => {
const dataId = dataset.id;
return (
<SidePanelSection key={dataId}>
@@ -102,15 +159,18 @@ function TooltipConfigFactory(DatasetTag, FieldSelector) {
fields={dataset.fields}
value={config.fieldsToShow[dataId]}
onSelect={selected => {
- const newConfig = {
+ const newConfig: DatasetTooltipConfigProps['config'] = {
...config,
fieldsToShow: {
...config.fieldsToShow,
+ //@ts-expect-error
[dataId]: selected.map(
f =>
config.fieldsToShow[dataId].find(
+ //@ts-expect-error
tooltipField => tooltipField.name === f.name
) || {
+ //@ts-expect-error
name: f.name,
// default initial tooltip is null
format: null
@@ -123,13 +183,14 @@ function TooltipConfigFactory(DatasetTag, FieldSelector) {
closeOnSelect={false}
multiSelect
inputTheme="secondary"
+ //@ts-expect-error
CustomChickletComponent={TooltipChickletFactory(dataId, config, onChange, dataset.fields)}
/>
</SidePanelSection>
);
};
- const TooltipConfig = ({config, datasets, onChange, intl}) => {
+ const TooltipConfig = ({config, datasets, onChange, intl}: TooltipConfigProps) => {
return (
<TooltipConfigWrapper>
{Object.keys(config.fieldsToShow).map(dataId =>
@@ -175,8 +236,9 @@ function TooltipConfigFactory(DatasetTag, FieldSelector) {
inputTheme={'secondary'}
getOptionValue={d => d}
onChange={option => {
- const newConfig = {
+ const newConfig: TooltipConfigProps['config'] = {
...config,
+ //@ts-expect-error
compareType: option
};
onChange(newConfig);
diff --git a/src/components/side-panel/interaction-panel/tooltip-config/tooltip-chicklet.js b/src/components/side-panel/interaction-panel/tooltip-config/tooltip-chicklet.tsx
similarity index 82%
rename from src/components/side-panel/interaction-panel/tooltip-config/tooltip-chicklet.js
rename to src/components/side-panel/interaction-panel/tooltip-config/tooltip-chicklet.tsx
index b11cca6ee..c685f1b8c 100644
--- a/src/components/side-panel/interaction-panel/tooltip-config/tooltip-chicklet.js
+++ b/src/components/side-panel/interaction-panel/tooltip-config/tooltip-chicklet.tsx
@@ -18,7 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
-import React, {Component} from 'react';
+import React, {Component, ComponentType} from 'react';
import styled from 'styled-components';
import {ChickletButton, ChickletTag} from 'components/common/item-selector/chickleted-input';
import {Hash, Delete} from 'components/common/icons';
@@ -30,10 +30,40 @@ import {TOOLTIP_FORMATS, TOOLTIP_FORMAT_TYPES, TOOLTIP_KEY} from 'constants/tool
import {getFormatter} from 'utils/data-utils';
import TippyTooltip from 'components/common/tippy-tooltip';
+interface TooltipChickletProps {
+ disabled: boolean;
+ item: {name: string};
+ displayOption: Function;
+ remove: any;
+}
+
+type TooltipConfig = {
+ fieldsToShow: {
+ [key: string]: {name: string; format: string | null}[];
+ };
+ compareMode: boolean;
+ compareType: string[];
+};
+
+type TooltipFields = {
+ format?: string;
+ id?: string;
+ name?: string;
+ fieldIdx?: number;
+ type?: number;
+};
+
+type TimeLabelFormat = {
+ id: string;
+ format: string | null;
+ type: string;
+ label: string;
+};
+
const TIME_DISPLAY = '2020-05-11 14:00';
const getValue = fmt => fmt[TOOLTIP_KEY];
-const addDTimeLabel = formats =>
+const addDTimeLabel = (formats: TimeLabelFormat[]) =>
formats.map(f => ({
...f,
label:
@@ -42,11 +72,12 @@ const addDTimeLabel = formats =>
: f.label
}));
-function getFormatLabels(fields, fieldName) {
- const fieldType = fields.find(f => f.name === fieldName).type;
+function getFormatLabels(fields: any, fieldName: string) {
+ const fieldType = fields.find((f: TooltipFields) => f.name === fieldName).type;
const tooltipTypes = (fieldType && FIELD_OPTS[fieldType].format.tooltip) || [];
- const formatLabels = Object.values(TOOLTIP_FORMATS).filter(t => tooltipTypes.includes(t.type));
-
+ const formatLabels: TimeLabelFormat[] = Object.values(TOOLTIP_FORMATS).filter(t =>
+ tooltipTypes.includes(t.type)
+ );
return addDTimeLabel(formatLabels);
}
@@ -82,7 +113,7 @@ const IconDiv = styled.div.attrs({
: props.theme.textColor};
`;
-function getFormatTooltip(formatLabels, format) {
+function getFormatTooltip(formatLabels: TimeLabelFormat[], format: string | null) {
if (!format) {
return null;
}
@@ -93,11 +124,17 @@ function getFormatTooltip(formatLabels, format) {
return typeof format === 'object' ? JSON.stringify(format, null, 2) : String(format);
}
-function TooltipChickletFactory(dataId, config, onChange, fields) {
- class TooltipChicklet extends Component {
+function TooltipChickletFactory(
+ dataId: string,
+ config: TooltipConfig,
+ onChange: (config: TooltipConfig) => void,
+ fields: TooltipFields[]
+): ComponentType<TooltipChickletProps> {
+ class TooltipChicklet extends Component<TooltipChickletProps> {
state = {
show: false
};
+ private node!: HTMLElement;
componentDidMount() {
document.addEventListener('mousedown', this.handleClickOutside, false);
@@ -107,11 +144,10 @@ function TooltipChickletFactory(dataId, config, onChange, fields) {
document.removeEventListener('mousedown', this.handleClickOutside, false);
}
- handleClickOutside = e => {
+ handleClickOutside = (e: any) => {
if (this.node.contains(e.target)) {
return;
}
- this.setState({show: false});
};
render() {
@@ -129,7 +165,7 @@ function TooltipChickletFactory(dataId, config, onChange, fields) {
const hashStyle = show ? hashStyles.SHOW : hasFormat ? hashStyles.ACTIVE : null;
return (
- <ChickletButton ref={node => (this.node = node)}>
+ <ChickletButton ref={(node: HTMLElement) => (this.node = node)}>
<ChickletTag>{displayOption(item)}</ChickletTag>
{formatLabels.length > 1 && (
<ChickletAddonWrapper>