Skip to content

Commit b35879c

Browse files
authored
Merge pull request #135 from DragonFighter603/emailtype
Created email type and added it to storybook
2 parents 4a34ed4 + 6ad6feb commit b35879c

File tree

5 files changed

+70
-2
lines changed

5 files changed

+70
-2
lines changed

src/components/pid-component/pid-component.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ The component can render the following types of components:
8686
- PIDs (resolvable via [handle.net](handle.net))
8787
- ORCiDs (resolvable via [orcid.org](orcid.org))
8888
- URLs (starting with http:// or https://)
89+
- Emails (separated by comma)
8990
- Dates
9091
- Everything else is rendered as a simple string (aka. fallback).
9192

@@ -103,6 +104,10 @@ You can easily add a new type by simply implementing the abstract class `Generic
103104

104105
<Canvas of={PIDComponentStories.URL} />
105106

107+
### Email
108+
109+
<Canvas of={PIDComponentStories.Email} />
110+
106111
### Date
107112

108113
<Canvas of={PIDComponentStories.Date} />

src/components/pid-component/pid-component.stories.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,19 @@ export const URL: Story = {
267267
},
268268
};
269269

270+
export const Email: Story = {
271+
args: {
272+
value: 'someone@example.com',
273+
},
274+
parameters: {
275+
docs: {
276+
source: {
277+
code: `<pid-component value='someone@example.com'></pid-component>`,
278+
},
279+
},
280+
},
281+
};
282+
270283
export const Fallback: Story = {
271284
args: {
272285
value: 'This is a fallback test',

src/utils/EmailType.tsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import {GenericIdentifierType} from './GenericIdentifierType';
2+
import {FunctionalComponent, h} from '@stencil/core';
3+
4+
/**
5+
* This class specifies a custom renderer for Email addresses.
6+
* @extends GenericIdentifierType
7+
*/
8+
export class EmailType extends GenericIdentifierType {
9+
getSettingsKey(): string {
10+
return 'EmailType';
11+
}
12+
13+
hasCorrectFormat(): boolean {
14+
// const regex = /^((\(.*\))?(\w|-|\+|_|\.)+(\(.*\))?@((((\(.*\))?\w+(\(.*\))?\.)+(\(.*\))?\w+(\(.*\))?\w+)|(\[((IPv6:((\w+:+)+\w+))|(\d+\.)+\d+)\]))\s*(,+|$)\s*)+$/
15+
const regex = /^(([\w\-\.]+@([\w-]+\.)+[\w-]{2,})(\s*,\s*)?)*$/gm;
16+
return regex.test(this.value);
17+
}
18+
19+
init(): Promise<void> {
20+
return;
21+
}
22+
23+
isResolvable(): boolean {
24+
return false;
25+
}
26+
27+
renderPreview(): FunctionalComponent<any> {
28+
// mail icon from: https://heroicons.com/ (MIT license)
29+
return (
30+
<span class={"items-center"}>
31+
{this.value.split(new RegExp(/\s*,\s*/)).filter(email => email.length > 0).map(email => {
32+
return (
33+
<a href={'mailto:' + email} target='_blank'
34+
class={'items-center inline-flex font-mono text-sm text-blue-400 border border-slate-400 rounded-md px-1 py-0.5 mr-2'}>
35+
<svg xmlns='http://www.w3.org/2000/svg' fill='none' aria-hidden='true' viewBox='0 0 24 24'
36+
stroke-width='1' stroke='black' height='20px' class={'mr-1'}>
37+
<path stroke-linecap='round' stroke-linejoin='round'
38+
d='M21.75 6.75v10.5a2.25 2.25 0 0 1-2.25 2.25h-15a2.25 2.25 0 0 1-2.25-2.25V6.75m19.5 0A2.25 2.25 0 0 0 19.5 4.5h-15a2.25 2.25 0 0 0-2.25 2.25m19.5 0v.243a2.25 2.25 0 0 1-1.07 1.916l-7.5 4.615a2.25 2.25 0 0 1-2.36 0L3.32 8.91a2.25 2.25 0 0 1-1.07-1.916V6.75'/>
39+
</svg>
40+
<span class={'ml-2'}>
41+
{email}
42+
</span>
43+
</a>
44+
);
45+
})}
46+
</span>
47+
);
48+
}
49+
}

src/utils/Parser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { FallbackType } from './FallbackType';
44
import { ORCIDType } from './ORCIDType';
55
import { DateType } from './DateType';
66
import { URLType } from './URLType';
7+
import { EmailType } from "./EmailType";
78

89
/**
910
* Class that handles the parsing of a given value and returns the best fitting component object
@@ -20,7 +21,7 @@ export class Parser {
2021
name: string;
2122
value: any;
2223
}[],
23-
) => GenericIdentifierType)[] = [DateType, ORCIDType, HandleType, URLType, FallbackType];
24+
) => GenericIdentifierType)[] = [DateType, ORCIDType, HandleType, EmailType, URLType, FallbackType];
2425

2526
/**
2627
* Returns the priority of the best fitting component object for a given value (lower is better)

src/utils/URLType.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { FunctionalComponent, h } from '@stencil/core';
77
*/
88
export class URLType extends GenericIdentifierType {
99
getSettingsKey(): string {
10-
return 'DateType';
10+
return 'URLType';
1111
}
1212

1313
hasCorrectFormat(): boolean {

0 commit comments

Comments
 (0)