-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate Map visualization to React (#4278)
- Loading branch information
1 parent
5cd6913
commit c6a2725
Showing
18 changed files
with
968 additions
and
587 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React, { useMemo } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import cx from 'classnames'; | ||
|
||
import { validateColor, getColorName } from './utils'; | ||
import './label.less'; | ||
|
||
export default function Label({ className, color, presetColors, ...props }) { | ||
const name = useMemo( | ||
() => getColorName(validateColor(color), presetColors), | ||
[color, presetColors], | ||
); | ||
|
||
return <span className={cx('color-label', className)} {...props}>{name}</span>; | ||
} | ||
|
||
Label.propTypes = { | ||
className: PropTypes.string, | ||
color: PropTypes.string, | ||
presetColors: PropTypes.oneOfType([ | ||
PropTypes.arrayOf(PropTypes.string), // array of colors (no tooltips) | ||
PropTypes.objectOf(PropTypes.string), // color name => color value | ||
]), | ||
}; | ||
|
||
Label.defaultProps = { | ||
className: null, | ||
color: '#FFFFFF', | ||
presetColors: null, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.color-label { | ||
vertical-align: middle; | ||
|
||
.color-swatch + & { | ||
margin-left: 7px; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { isArray, findKey } from 'lodash'; | ||
import tinycolor from 'tinycolor2'; | ||
|
||
export function validateColor(value, fallback = null) { | ||
value = tinycolor(value); | ||
return value.isValid() ? '#' + value.toHex().toUpperCase() : fallback; | ||
} | ||
|
||
export function getColorName(color, presetColors) { | ||
if (isArray(presetColors)) { | ||
return color; | ||
} | ||
return findKey(presetColors, v => validateColor(v) === color) || color; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { isNil, map, filter, difference } from 'lodash'; | ||
import React, { useMemo } from 'react'; | ||
import Select from 'antd/lib/select'; | ||
import { EditorPropTypes } from '@/visualizations'; | ||
|
||
function getColumns(column, unusedColumns) { | ||
return filter( | ||
[column, ...unusedColumns], | ||
v => !isNil(v), | ||
); | ||
} | ||
|
||
export default function GeneralSettings({ options, data, onOptionsChange }) { | ||
const unusedColumns = useMemo( | ||
() => difference(map(data.columns, c => c.name), [options.latColName, options.lonColName, options.classify]), | ||
[data, options.latColName, options.lonColName, options.classify], | ||
); | ||
|
||
return ( | ||
<React.Fragment> | ||
<div className="m-b-15"> | ||
<label htmlFor="map-editor-latitude-column-name">Latitude Column Name</label> | ||
<Select | ||
data-test="Map.Editor.LatitudeColumnName" | ||
id="map-editor-latitude-column-name" | ||
className="w-100" | ||
value={options.latColName} | ||
onChange={latColName => onOptionsChange({ latColName })} | ||
> | ||
{map(getColumns(options.latColName, unusedColumns), col => ( | ||
<Select.Option key={col} data-test={'Map.Editor.LatitudeColumnName.' + col}>{col}</Select.Option> | ||
))} | ||
</Select> | ||
</div> | ||
|
||
<div className="m-b-15"> | ||
<label htmlFor="map-editor-longitude-column-name">Longitude Column Name</label> | ||
<Select | ||
data-test="Map.Editor.LongitudeColumnName" | ||
id="map-editor-longitude-column-name" | ||
className="w-100" | ||
value={options.lonColName} | ||
onChange={lonColName => onOptionsChange({ lonColName })} | ||
> | ||
{map(getColumns(options.lonColName, unusedColumns), col => ( | ||
<Select.Option key={col} data-test={'Map.Editor.LongitudeColumnName.' + col}>{col}</Select.Option> | ||
))} | ||
</Select> | ||
</div> | ||
|
||
<div className="m-b-15"> | ||
<label className="control-label" htmlFor="map-editor-group-by">Group By</label> | ||
<Select | ||
data-test="Map.Editor.GroupBy" | ||
id="map-editor-group-by" | ||
className="w-100" | ||
allowClear | ||
placeholder="none" | ||
value={options.classify || undefined} | ||
onChange={column => onOptionsChange({ classify: column || null })} | ||
> | ||
{map(getColumns(options.classify, unusedColumns), col => ( | ||
<Select.Option key={col} data-test={'Map.Editor.GroupBy.' + col}>{col}</Select.Option> | ||
))} | ||
</Select> | ||
</div> | ||
</React.Fragment> | ||
); | ||
} | ||
|
||
GeneralSettings.propTypes = EditorPropTypes; |
Oops, something went wrong.