-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathPropertyGrid.tsx
129 lines (112 loc) · 3.29 KB
/
PropertyGrid.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import './PropertyGrid.less';
import React, {
useMemo
} from 'react';
import { Table } from 'antd';
import { TableProps } from 'antd/lib/table';
import _get from 'lodash/get';
import { getUid } from 'ol';
import OlFeature from 'ol/Feature';
import OlGeometry from 'ol/geom/Geometry';
import { CSS_PREFIX } from '../../constants';
type AttributeNames = Record<string, string>;
interface OwnProps {
/**
* Title of the attribute name column
*/
attributeNameColumnTitle?: string;
/**
* Value in percent representing the width of the attribute name column
* The width of the attribute value column will be calculated based on this
*/
attributeNameColumnWidthInPercent?: number;
/**
* Title of the attribute value column
*/
attributeValueColumnTitle?: string;
/**
* A CSS class which should be added.
*/
className?: string;
/**
* Array of attribute names to filter
*/
attributeFilter?: string[];
/**
* Object containing a mapping of attribute names in OL feature to custom ones
*/
attributeNames?: AttributeNames;
/**
* Feature for which the properties should be shown
*/
feature: OlFeature<OlGeometry>;
}
export type PropertyGridProps<T = any> = OwnProps & TableProps<T>;
const defaultClassName = `${CSS_PREFIX}propertygrid`;
/**
* Component representing a feature grid showing the attribute values of a simple feature.
*/
const PropertyGrid: React.FC<PropertyGridProps> = ({
attributeNameColumnTitle = 'Attribute name',
attributeNameColumnWidthInPercent = 50,
attributeValueColumnTitle = 'Attribute value',
className,
attributeFilter,
attributeNames,
feature,
...passThroughProps
}) => {
const dataSource = useMemo(() => {
let filter = attributeFilter;
if (!filter) {
filter = feature.getKeys().filter((attrName: string) => attrName !== 'geometry');
}
return filter.map((attr: any) => {
const fid = getUid(feature);
return {
attributeName: (attributeNames && _get(attributeNames, attr)) ?
_get(attributeNames, attr) :
attr,
attributeValue: feature.get(attr),
key: `ATTR_${attr}_fid_${fid}`
};
});
}, [attributeFilter, attributeNames, feature]);
const isUrl = (value: string) => {
return /^(?:\w+:)?\/\/([^\s.]+\.\S{2}|localhost[:?\d]*)\S*$/.test(value);
};
const columns = useMemo(() => {
return [{
title: attributeNameColumnTitle,
dataIndex: 'attributeName',
key: 'attributeName',
width: `${attributeNameColumnWidthInPercent}%`
}, {
title: attributeValueColumnTitle,
dataIndex: 'attributeValue',
key: 'attributeValue',
width: `${100 - attributeNameColumnWidthInPercent}%`,
render: (value: any) => {
if (isUrl(value)) {
return <a href={value} target="_blank">{value}</a>;
} else {
return value;
}
}
}];
}, [attributeNameColumnTitle, attributeNameColumnWidthInPercent, attributeValueColumnTitle]);
const finalClassName = className
? `${className} ${defaultClassName}`
: defaultClassName;
return (
<Table
className={finalClassName}
rowKey={record => record.key}
dataSource={dataSource}
columns={columns}
pagination={false}
{...passThroughProps}
/>
);
};
export default PropertyGrid;