maputnik/src/components/map/FeaturePropertyPopup.jsx

49 lines
1.4 KiB
React
Raw Normal View History

2016-12-25 17:46:18 +01:00
import React from 'react'
2017-11-08 09:47:36 +01:00
import PropTypes from 'prop-types'
2016-12-25 17:46:18 +01:00
import InputBlock from '../inputs/InputBlock'
import StringInput from '../inputs/StringInput'
2017-01-10 09:38:27 +01:00
function displayValue(value) {
if (typeof value === 'undefined' || value === null) return value;
if (value instanceof Date) return value.toLocaleString();
if (typeof value === 'object' ||
typeof value === 'number' ||
typeof value === 'string') return value.toString();
return value;
}
2016-12-25 17:46:18 +01:00
function renderProperties(feature) {
return Object.keys(feature.properties).map(propertyName => {
const property = feature.properties[propertyName]
2017-01-11 11:35:33 +01:00
return <InputBlock key={propertyName} label={propertyName}>
2017-01-10 09:38:27 +01:00
<StringInput value={displayValue(property)} style={{backgroundColor: 'transparent'}}/>
2016-12-25 17:46:18 +01:00
</InputBlock>
})
}
function renderFeature(feature) {
2017-01-10 09:38:27 +01:00
return <div key={feature.id}>
2017-01-11 11:35:33 +01:00
<div className="maputnik-popup-layer-id">{feature.layer['source-layer']}</div>
2017-01-18 10:03:15 +01:00
<InputBlock key={"property-type"} label={"$type"}>
<StringInput value={feature.geometry.type} style={{backgroundColor: 'transparent'}} />
</InputBlock>
2016-12-25 17:46:18 +01:00
{renderProperties(feature)}
</div>
}
class FeaturePropertyPopup extends React.Component {
2017-11-08 09:47:36 +01:00
static propTypes = {
features: PropTypes.array
}
2016-12-25 17:46:18 +01:00
render() {
const features = this.props.features
2017-01-11 11:35:33 +01:00
return <div className="maputnik-feature-property-popup">
2016-12-25 17:46:18 +01:00
{features.map(renderFeature)}
</div>
}
}
export default FeaturePropertyPopup