Skip to content

Commit

Permalink
Merge pull request #3849 from KaiVolland/anchor-for-urls
Browse files Browse the repository at this point in the history
Render urls as anchor element
  • Loading branch information
KaiVolland authored May 15, 2024
2 parents 745bf83 + 4b1822c commit 8caa818
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Grid/PropertyGrid/PropertyGrid.example.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const feature = new OlFeature({
const attributeObject = {
foo: 'bar',
foo2: 'bar2',
foo3: 'bar3',
foo3: 'https://terrestris.github.io/react-geo/',
foo9: 'bar9',
name: 'Point'
};
Expand Down
21 changes: 20 additions & 1 deletion src/Grid/PropertyGrid/PropertyGrid.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ describe('<PropertyGrid />', () => {
foo: 'bar',
bvb: 'yarmolenko',
mip: 'map',
name: 'Point'
name: 'Point',
link: 'https://www.example.com'
};

testFeature.setProperties(attributeObject);
Expand Down Expand Up @@ -132,4 +133,22 @@ describe('<PropertyGrid />', () => {
}
});
});

it('renders urls as links', () => {
const props = {
feature: testFeature
};
const wrapper = TestUtil.mountComponent(PropertyGrid, props);

const dataSource = wrapper.instance().getDataSource();
dataSource.forEach((dataSourceElement) => {
const attributeValue = dataSourceElement.attributeValue;
const renderedValue = wrapper.find('a').filterWhere((a) => a.text() === attributeValue);
if (dataSourceElement.attributeName === 'link') {
expect(renderedValue).toHaveLength(1);
} else {
expect(renderedValue).toHaveLength(0);
}
});
})

Check warning on line 153 in src/Grid/PropertyGrid/PropertyGrid.spec.tsx

View workflow job for this annotation

GitHub Actions / build

Missing semicolon

Check warning on line 153 in src/Grid/PropertyGrid/PropertyGrid.spec.tsx

View workflow job for this annotation

GitHub Actions / Release

Missing semicolon
});
15 changes: 13 additions & 2 deletions src/Grid/PropertyGrid/PropertyGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,15 @@ class PropertyGrid extends React.Component<PropertyGridProps> {
return dataSource;
}

isUrl(value: string) {
return /^(?:\w+:)?\/\/([^\s.]+\.\S{2}|localhost[:?\d]*)\S*$/.test(value);
}

/**
* Generates the column definition for the given feature.
*/
getColumns() {
const columns = [{
const columns: TableProps<any>["columns"] = [{

Check warning on line 107 in src/Grid/PropertyGrid/PropertyGrid.tsx

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote

Check warning on line 107 in src/Grid/PropertyGrid/PropertyGrid.tsx

View workflow job for this annotation

GitHub Actions / Release

Strings must use singlequote
title: this.props.attributeNameColumnTitle,
dataIndex: 'attributeName',
key: 'attributeName',
Expand All @@ -109,7 +113,14 @@ class PropertyGrid extends React.Component<PropertyGridProps> {
title: this.props.attributeValueColumnTitle,
dataIndex: 'attributeValue',
key: 'attributeValue',
width: `${100 - this.props.attributeNameColumnWidthInPercent}%`
width: `${100 - this.props.attributeNameColumnWidthInPercent}%`,
render: (value) => {
if (this.isUrl(value)) {
return <a href={value} target="_blank">{value}</a>;
} else {
return value;
}
}
}];

return columns;
Expand Down

0 comments on commit 8caa818

Please sign in to comment.