Skip to content

Commit 9c3a947

Browse files
committed
fix: contactlink
1 parent 3b5b9ae commit 9c3a947

File tree

1 file changed

+73
-54
lines changed

1 file changed

+73
-54
lines changed

src/components/ItaliaTheme/View/Commons/ContactLink.jsx

Lines changed: 73 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const messages = defineMessages({
1515
id: 'email_label',
1616
defaultMessage: 'E-mail',
1717
},
18-
1918
call: {
2019
id: 'chiama_il_numero',
2120
defaultMessage: 'Chiama il numero',
@@ -32,70 +31,90 @@ const messages = defineMessages({
3231

3332
const ContactLink = ({ tel, fax, email, label = true, strong = false }) => {
3433
const intl = useIntl();
35-
let ret_label = null;
36-
let ret = null;
37-
38-
function ReplacePhoneNumbers(str, type) {
39-
// eslint-disable-next-line no-useless-escape
40-
let newhtml = str.replace(/\+?[0-9]( ?[0-9\/-]+)+.?[0-9]*/gm, function (v) {
41-
let r =
42-
"<a href='" +
43-
type +
44-
':' +
45-
v.trim().replace(/-|\/|\s/gm, '') +
46-
"' title='" +
47-
(type === 'tel'
48-
? intl.formatMessage(messages.call)
49-
: intl.formatMessage(messages.call_fax)) +
50-
"' >" +
51-
v +
52-
'</a>';
53-
return r;
54-
});
55-
return newhtml;
56-
}
5734

58-
function ReplaceEmails(str) {
59-
let newhtml = str.replace(
60-
/([a-zA-Z0-9+._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi,
61-
function (v) {
62-
let r =
63-
"<a href='mailto:" +
64-
v.trim().replace(/|\/|\s/gm, '') +
65-
"' title='" +
66-
intl.formatMessage(messages.write_to) +
67-
"' >" +
68-
v +
69-
'</a>';
70-
return r;
71-
},
35+
const formatTel = (str, type) =>
36+
str.split(/\+?[0-9]( ?[0-9/-]+)+.?[0-9]*/gm).map((v, i) =>
37+
i % 2 === 0 ? (
38+
<span key={i}>{` ${v} `}</span>
39+
) : (
40+
<a
41+
key={i}
42+
href={`${type}:${v}`}
43+
title={
44+
type === 'tel'
45+
? intl.formatMessage(messages.call)
46+
: intl.formatMessage(messages.call_fax)
47+
}
48+
>
49+
{v}
50+
</a>
51+
),
7252
);
73-
return newhtml;
74-
}
53+
54+
const formatEmail = (str) =>
55+
str
56+
.split(/([a-zA-Z0-9+._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi)
57+
.map((v, i) =>
58+
i % 2 === 0 ? (
59+
<span key={i}>{` ${v} `}</span>
60+
) : (
61+
<a
62+
key={i}
63+
href={`mailto:${v}`}
64+
title={intl.formatMessage(messages.write_to)}
65+
>
66+
{v}
67+
</a>
68+
),
69+
);
7570

7671
if (tel) {
77-
ret_label = intl.formatMessage(messages.telefono);
78-
ret = ReplacePhoneNumbers(tel, 'tel');
72+
return (
73+
<>
74+
{label &&
75+
(strong ? (
76+
<strong>{intl.formatMessage(messages.telefono)}</strong>
77+
) : (
78+
intl.formatMessage(messages.telefono)
79+
))}
80+
{formatTel(tel, 'tel')}
81+
</>
82+
);
7983
} else if (fax) {
80-
ret_label = intl.formatMessage(messages.fax);
81-
ret = ReplacePhoneNumbers(fax, 'fax');
84+
return (
85+
<>
86+
{label &&
87+
(strong ? (
88+
<strong>{intl.formatMessage(messages.fax)}</strong>
89+
) : (
90+
intl.formatMessage(messages.fax)
91+
))}
92+
{formatTel(fax, 'fax')}
93+
</>
94+
);
8295
} else if (email) {
83-
ret_label = intl.formatMessage(messages.email_label);
84-
ret = ReplaceEmails(email);
96+
return (
97+
<>
98+
{label &&
99+
(strong ? (
100+
<strong>{intl.formatMessage(messages.email_label)}</strong>
101+
) : (
102+
intl.formatMessage(messages.email_label)
103+
))}
104+
{formatEmail(email)}
105+
</>
106+
);
107+
} else {
108+
return null;
85109
}
86-
ret_label = label ? <>{ret_label}: </> : null;
87-
ret_label = label ? strong ? <strong>{ret_label}</strong> : ret_label : null;
88-
89-
return ret ? (
90-
<>
91-
{ret_label}
92-
<span dangerouslySetInnerHTML={{ __html: ret }} />
93-
</>
94-
) : null;
95110
};
96111

97112
ContactLink.propTypes = {
98113
tel: PropTypes.string,
114+
fax: PropTypes.string,
115+
email: PropTypes.string,
116+
label: PropTypes.bool,
117+
strong: PropTypes.bool,
99118
};
100119

101120
export default ContactLink;

0 commit comments

Comments
 (0)