-
Notifications
You must be signed in to change notification settings - Fork 2
/
UrlPropertyValueRenderer.tsx
69 lines (64 loc) · 2.18 KB
/
UrlPropertyValueRenderer.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Properties
*/
import * as React from "react";
import type { LinkElementsInfo, PropertyRecord } from "@itwin/appui-abstract";
import { PropertyValueFormat, StandardTypeNames } from "@itwin/appui-abstract";
import type {
IPropertyValueRenderer,
PropertyValueRendererContext,
} from "../../ValueRendererManager";
import { PrimitivePropertyValueRendererImpl } from "./PrimitivePropertyValueRenderer";
import { convertRecordToString } from "./Common";
/**
* URL property value renderer that renders the whole value as a URL without matching it
* against URL regex.
*
* @public
*/
export class UrlPropertyValueRenderer implements IPropertyValueRenderer {
/** Checks if the renderer can handle given property */
public canRender(record: PropertyRecord) {
return (
record.value.valueFormat === PropertyValueFormat.Primitive &&
[
StandardTypeNames.URL,
StandardTypeNames.String,
StandardTypeNames.Text,
].includes(record.property.typename as StandardTypeNames)
);
}
/** Method that returns a JSX representation of PropertyRecord */
public render(
record: PropertyRecord,
context?: PropertyValueRendererContext
) {
return (
<PrimitivePropertyValueRendererImpl
record={record}
context={context}
stringValueCalculator={convertRecordToString}
linksHandler={URI_PROPERTY_LINK_HANDLER}
/>
);
}
}
/**
* Default URI onClick event, sets location.href to the whole URI value without matching it
* against URL regex.
*/
function urlOnClick(text: string) {
if (text.startsWith("mailto:") || text.startsWith("pw:"))
location.href = text;
else {
const windowOpen = window.open(text, "_blank");
if (windowOpen) windowOpen.focus();
}
}
const URI_PROPERTY_LINK_HANDLER: LinkElementsInfo = {
onClick: urlOnClick,
};