Skip to content

Commit c614f48

Browse files
(Picklist): execute extractTextContent() inside extractTextContent()
1 parent c17585e commit c614f48

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

src/scripts/Picklist.tsx

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function collectOptionValues(children: unknown): PicklistValue[] {
6767
function findSelectedItemLabel(
6868
children: unknown,
6969
selectedValue: PicklistValue
70-
): React.ReactNode | null {
70+
): string | number | null {
7171
return (
7272
React.Children.map(children, (child) => {
7373
if (!React.isValidElement(child)) {
@@ -107,21 +107,56 @@ function findSelectedItemLabel(
107107
typeof label === 'string' ||
108108
typeof label === 'number' ||
109109
React.isValidElement(label)
110-
? label
110+
? extractTextContent(label)
111111
: undefined;
112112
const childrenValue =
113113
typeof itemChildren === 'string' ||
114114
typeof itemChildren === 'number' ||
115115
React.isValidElement(itemChildren) ||
116116
Array.isArray(itemChildren)
117-
? itemChildren
117+
? extractTextContent(itemChildren)
118118
: undefined;
119119

120120
return labelValue || childrenValue;
121121
}).find((result) => result !== null) ?? null
122122
);
123123
}
124124

125+
/**
126+
* Extract text content from React node recursively
127+
*/
128+
function extractTextContent(node: unknown): string | number | null {
129+
if (node == null) {
130+
return null;
131+
}
132+
133+
if (typeof node === 'string' || typeof node === 'number') {
134+
return node;
135+
}
136+
137+
if (typeof node === 'boolean') {
138+
return String(node);
139+
}
140+
141+
if (Array.isArray(node)) {
142+
return node
143+
.map(extractTextContent)
144+
.filter((result) => result !== null)
145+
.join('');
146+
}
147+
148+
if (
149+
React.isValidElement(node) &&
150+
node.props &&
151+
typeof node.props === 'object' &&
152+
'children' in node.props
153+
) {
154+
return extractTextContent(node.props.children);
155+
}
156+
157+
return null;
158+
}
159+
125160
/**
126161
*
127162
*/

0 commit comments

Comments
 (0)