@adobe-commerce/elsie 1.6.0 → 1.7.0-beta2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,70 @@
1
+ /********************************************************************
2
+ * ADOBE CONFIDENTIAL
3
+ * __________________
4
+ *
5
+ * Copyright 2025 Adobe
6
+ * All Rights Reserved.
7
+ *
8
+ * NOTICE: All information contained herein is, and remains
9
+ * the property of Adobe and its suppliers, if any. The intellectual
10
+ * and technical concepts contained herein are proprietary to Adobe
11
+ * and its suppliers and are protected by all applicable intellectual
12
+ * property laws, including trade secret and copyright laws.
13
+ * Dissemination of this information or reproduction of this material
14
+ * is strictly forbidden unless prior written permission is obtained
15
+ * from Adobe.
16
+ *******************************************************************/
17
+
18
+ import { VNode } from 'preact';
19
+
20
+ /**
21
+ * Wraps trailing asterisk(s) in a span element for consistent styling.
22
+ * If the text ends with one or more asterisks (preceded by optional space),
23
+ * the asterisk(s) will be wrapped in a <span className="dropin-label-required">.
24
+ * If required is true and no asterisk exists, one will be added automatically.
25
+ *
26
+ * @param text - The label text that may contain asterisk(s)
27
+ * @param required - Whether the field is required
28
+ * @returns A VNode with the text and wrapped asterisk, or the original text
29
+ *
30
+ * @example
31
+ * wrapRequiredAsterisk('Email *', true) // Returns: <>Email <span className="dropin-label-required">*</span></>
32
+ * wrapRequiredAsterisk('Email', true) // Returns: <>Email <span className="dropin-label-required">*</span></>
33
+ * wrapRequiredAsterisk('Email *', false) // Returns: <>Email <span>*</span></>
34
+ * wrapRequiredAsterisk('Email', false) // Returns: 'Email'
35
+ */
36
+ export const wrapRequiredAsterisk = (
37
+ text: string | undefined,
38
+ required?: boolean
39
+ ): string | VNode | undefined => {
40
+ if (!text) return text;
41
+
42
+ // Match optional space followed by one or more asterisks at the end of the string
43
+ const asteriskPattern = /^(.+?)(\s*)(\*+)$/;
44
+ const match = text.match(asteriskPattern);
45
+
46
+ if (match) {
47
+ const [, textPart, space, asterisks] = match;
48
+ return (
49
+ <>
50
+ {textPart}
51
+ {space}
52
+ <span className={required ? 'dropin-label-required' : undefined}>
53
+ {asterisks}
54
+ </span>
55
+ </>
56
+ );
57
+ }
58
+
59
+ // If required is true and no asterisk exists, add it
60
+ if (required) {
61
+ return (
62
+ <>
63
+ {text}{' '}
64
+ <span className="dropin-label-required">*</span>
65
+ </>
66
+ );
67
+ }
68
+
69
+ return text;
70
+ };